Skip to content

Commit

Permalink
improve getRecord handling for cms utils
Browse files Browse the repository at this point in the history
  • Loading branch information
lekoala committed Jul 5, 2024
1 parent d612a20 commit 7ea6559
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/ActionsGridFieldItemRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace LeKoala\CmsActions;

use Exception;
use ReflectionMethod;
use SilverStripe\Forms\Tab;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\TabSet;
Expand Down Expand Up @@ -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);
Expand Down

1 comment on commit 7ea6559

@sunnysideup
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome. THANK YOU. MERCI

Please sign in to comment.