Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom actions for non-DataObject based GridFields #42

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 24 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,8 @@
use SilverStripe\Forms\GridField\GridFieldDetailForm_ItemRequest;
use ReflectionObject;
use SilverStripe\Admin\ModelAdmin;
use SilverStripe\Core\Extension;
use SilverStripe\View\ViewableData;

/**
* Decorates GridDetailForm_ItemRequest to use new form actions and buttons.
Expand All @@ -42,7 +43,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 +334,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 +351,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 +395,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 +407,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 +421,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 +464,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 +535,10 @@ public function getPublicEditLinkForAdjacentRecord(int $offset): ?string

/**
* @param FieldList $actions
* @param DataObject $record
* @param ViewableData $record
* @return void
*/
public function addSaveAndClose(FieldList $actions, DataObject $record)
public function addSaveAndClose(FieldList $actions, ViewableData $record)
{
if (!$this->checkCan($record)) {
return;
Expand Down Expand Up @@ -562,10 +566,10 @@ public function addSaveAndClose(FieldList $actions, DataObject $record)
/**
* New and existing records have different classes
*
* @param DataObject $record
* @param ViewableData $record
* @return string
*/
protected function getBtnClassForRecord(DataObject $record)
protected function getBtnClassForRecord(ViewableData $record)
{
if ($record->ID) {
return 'btn-outline-primary';
Expand Down Expand Up @@ -606,7 +610,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 +1029,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
Loading