Skip to content

Commit

Permalink
feat: allow actions for non-DataObject based GridFields
Browse files Browse the repository at this point in the history
  • Loading branch information
gurucomkz committed Jan 22, 2025
1 parent fb02f39 commit 01a59f1
Showing 1 changed file with 25 additions and 20 deletions.
45 changes: 25 additions & 20 deletions src/ActionsGridFieldItemRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use SilverStripe\Forms\FormAction;
use SilverStripe\Admin\LeftAndMain;
use SilverStripe\Forms\HiddenField;
use SilverStripe\ORM\DataExtension;
use SilverStripe\Control\Controller;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
Expand All @@ -28,6 +27,9 @@
use SilverStripe\Forms\GridField\GridFieldDetailForm_ItemRequest;
use ReflectionObject;
use SilverStripe\Admin\ModelAdmin;
use SilverStripe\Core\Extension;
use SilverStripe\ORM\DataObjectInterface;
use SilverStripe\View\ViewableData;

/**
* Decorates GridDetailForm_ItemRequest to use new form actions and buttons.
Expand All @@ -42,7 +44,7 @@
* @link https://github.com/unclecheese/silverstripe-gridfield-betterbuttons/blob/master/src/Extensions/GridFieldBetterButtonsItemRequest.php
* @property LeftAndMain&GridFieldDetailForm_ItemRequest&ActionsGridFieldItemRequest $owner
*/
class ActionsGridFieldItemRequest extends DataExtension
class ActionsGridFieldItemRequest extends Extension
{
use Configurable;
use Extensible;
Expand Down Expand Up @@ -333,12 +335,15 @@ protected function createDropUpContainer($actions)

/**
* Check if a record can be edited/created/exists
* @param DataObject $record
* @param ViewableData $record
* @return bool
*/
protected function checkCan($record)
{
if (!$record->canEdit() || (!$record->ID && !$record->canCreate())) {
$canEdit = $record->hasMethod('canEdit') ? $record->canEdit() : false;
$canCreate = $record->hasMethod('canCreate') ? $record->canCreate() : false;

if (!$canEdit || (!$record->ID && !$canCreate)) {
return false;
}

Expand All @@ -347,10 +352,10 @@ protected function checkCan($record)

/**
* @param FieldList $actions
* @param DataObject $record
* @param ViewableData $record
* @return void
*/
public function moveCancelAndDelete(FieldList $actions, DataObject $record)
public function moveCancelAndDelete(FieldList $actions, ViewableData $record)
{
// We have a 4.4 setup, before that there was no RightGroup
$RightGroup = $actions->fieldByName('RightGroup');
Expand Down Expand Up @@ -391,10 +396,10 @@ public function moveCancelAndDelete(FieldList $actions, DataObject $record)
}

/**
* @param DataObject $record
* @param ViewableData $record
* @return bool
*/
public function useCustomPrevNext(DataObject $record): bool
public function useCustomPrevNext(ViewableData $record): bool
{
if (self::config()->enable_custom_prevnext) {
return $record->hasMethod('PrevRecord') && $record->hasMethod('NextRecord');
Expand All @@ -403,10 +408,10 @@ public function useCustomPrevNext(DataObject $record): bool
}

/**
* @param DataObject $record
* @param ViewableData $record
* @return int
*/
public function getCustomPreviousRecordID(DataObject $record)
public function getCustomPreviousRecordID(ViewableData $record)
{
// This will overwrite state provided record
if ($this->useCustomPrevNext($record)) {
Expand All @@ -417,10 +422,10 @@ public function getCustomPreviousRecordID(DataObject $record)
}

/**
* @param DataObject $record
* @param ViewableData $record
* @return int
*/
public function getCustomNextRecordID(DataObject $record)
public function getCustomNextRecordID(ViewableData $record)
{

// This will overwrite state provided record
Expand Down Expand Up @@ -460,10 +465,10 @@ protected function getRightGroupActions(FieldList $actions)

/**
* @param FieldList $actions
* @param DataObject $record
* @param ViewableData $record
* @return void
*/
public function addSaveNextAndPrevious(FieldList $actions, DataObject $record)
public function addSaveNextAndPrevious(FieldList $actions, ViewableData $record)
{
if (!$record->canEdit() || !$record->ID) {
return;
Expand Down Expand Up @@ -531,10 +536,10 @@ public function getPublicEditLinkForAdjacentRecord(int $offset): ?string

/**
* @param FieldList $actions
* @param DataObject $record
* @param DataObjectInterface $record
* @return void
*/
public function addSaveAndClose(FieldList $actions, DataObject $record)
public function addSaveAndClose(FieldList $actions, DataObjectInterface $record)
{
if (!$this->checkCan($record)) {
return;
Expand Down Expand Up @@ -562,10 +567,10 @@ public function addSaveAndClose(FieldList $actions, DataObject $record)
/**
* New and existing records have different classes
*
* @param DataObject $record
* @param DataObjectInterface $record
* @return string
*/
protected function getBtnClassForRecord(DataObject $record)
protected function getBtnClassForRecord(DataObjectInterface $record)
{
if ($record->ID) {
return 'btn-outline-primary';
Expand Down Expand Up @@ -606,7 +611,7 @@ protected static function findAction($action, $definedActions)
}

/**
* Forward a given action to a DataObject
* Forward a given action to a DataObject/ViewableData
*
* Action must be declared in getCMSActions to be called
*
Expand Down Expand Up @@ -1025,7 +1030,7 @@ public function getBackLink()
* Response object for this request after a successful save
*
* @param bool $isNewRecord True if this record was just created
* @param DataObject $record
* @param ViewableData $record
* @return HTTPResponse|DBHTMLText|string
* @todo This had to be directly copied from {@link GridFieldDetailForm_ItemRequest}
* because it is a protected method and not visible to a decorator!
Expand Down

0 comments on commit 01a59f1

Please sign in to comment.