From 7ea6559d52efbe739481cbdf49c885711b621dd4 Mon Sep 17 00:00:00 2001 From: Thomas Date: Fri, 5 Jul 2024 12:01:53 +0200 Subject: [PATCH] improve getRecord handling for cms utils --- src/ActionsGridFieldItemRequest.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/ActionsGridFieldItemRequest.php b/src/ActionsGridFieldItemRequest.php index 0dac016..b7c7f72 100644 --- a/src/ActionsGridFieldItemRequest.php +++ b/src/ActionsGridFieldItemRequest.php @@ -3,6 +3,7 @@ namespace LeKoala\CmsActions; use Exception; +use ReflectionMethod; use SilverStripe\Forms\Tab; use SilverStripe\Forms\Form; use SilverStripe\Forms\TabSet; @@ -142,7 +143,14 @@ public function updateEditForm(Form $form) */ public function recordCmsUtils() { - $record = $this->owner->getRecord(0); + /** @var \SilverStripe\Versioned\VersionedGridFieldItemRequest|\SilverStripe\Admin\LeftAndMain $owner */ + $owner = $this->owner; + + // At this stage, the get record could be from a gridfield item request, or from a more general left and main which requires an id + // maybe we could simply do: + // $record = DataObject::singleton($controller->getModelClass()); + $reflectionMethod = new ReflectionMethod($owner, 'getRecord'); + $record = count($reflectionMethod->getParameters()) > 0 ? $owner->getRecord(0) : $owner->getRecord(); if ($record && $record->hasMethod('getCMSUtils')) { $utils = $record->getCMSUtils(); $this->extend('onCMSUtils', $utils, $record);