From 98a8cd8a7e2a9e38f0496855ef0932b97739eafa Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Sat, 12 Feb 2022 19:49:05 +1300 Subject: [PATCH] NEW Extend new AbstractGridFieldComponent class This makes the `GridFieldExportAction` and `GridFieldWorkflowRestrictedEditButton` component `Injectable`, and allows any future enhancements in the new abstract class to automatically apply without requiring additional changes in this module. The class is introduced in silverstripe/framework 4.11.0 so the dependency constraint needs to be updated. Also use dependency injection instead of the `new` keyword when instantiating GridFields, their components, and configs. --- composer.json | 2 +- src/Admin/AdvancedWorkflowAdmin.php | 10 +++++----- src/DataObjects/WorkflowDefinition.php | 16 ++++++++-------- src/DataObjects/WorkflowInstance.php | 2 +- src/Extensions/WorkflowApplicable.php | 8 ++++---- src/Forms/gridfield/GridFieldExportAction.php | 3 ++- .../GridFieldWorkflowRestrictedEditButton.php | 3 ++- 7 files changed, 23 insertions(+), 21 deletions(-) diff --git a/composer.json b/composer.json index 63f1092c..ecd1b730 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ "require": { "php": "^7.4 || ^8.0", "silverstripe/cms": "^4", - "silverstripe/framework": "^4.10", + "silverstripe/framework": "^4.11", "silverstripe/admin": "^1", "silverstripe/versioned": "^1", "symfony/yaml": "^3 || ^4" diff --git a/src/Admin/AdvancedWorkflowAdmin.php b/src/Admin/AdvancedWorkflowAdmin.php index 40f5ccad..1ad36eee 100644 --- a/src/Admin/AdvancedWorkflowAdmin.php +++ b/src/Admin/AdvancedWorkflowAdmin.php @@ -119,9 +119,9 @@ public function getEditForm($id = null, $fields = null) } // Pending/Submitted items GridField Config - $config = new GridFieldConfig_Base(); - $config->addComponent(new GridFieldEditButton()); - $config->addComponent(new GridFieldDetailForm()); + $config = GridFieldConfig_Base::create(); + $config->addComponent(GridFieldEditButton::create()); + $config->addComponent(GridFieldDetailForm::create()); $config->getComponentByType(GridFieldPaginator::class)->setItemsPerPage(5); $columns = $config->getComponentByType(GridFieldDataColumns::class); $columns->setFieldFormatting($this->setFieldFormatting($config)); @@ -173,7 +173,7 @@ public function getEditForm($id = null, $fields = null) $formFieldBottom->setForm($form); $formFieldBottom->getConfig()->removeComponentsByType(GridFieldEditButton::class); - $formFieldBottom->getConfig()->addComponent(new GridFieldWorkflowRestrictedEditButton()); + $formFieldBottom->getConfig()->addComponent(GridFieldWorkflowRestrictedEditButton::create()); $form->Fields()->insertBefore($definitionGridFieldName, $formFieldBottom); } @@ -189,7 +189,7 @@ public function getEditForm($id = null, $fields = null) $grid->getConfig()->getComponentByType(GridFieldDetailForm::class) ->setItemRequestClass(WorkflowDefinitionItemRequestClass::class); - $grid->getConfig()->addComponent(new GridFieldExportAction()); + $grid->getConfig()->addComponent(GridFieldExportAction::create()); $grid->getConfig()->removeComponentsByType(GridFieldExportButton::class); $grid->getConfig()->removeComponentsByType(GridFieldImportButton::class); } diff --git a/src/DataObjects/WorkflowDefinition.php b/src/DataObjects/WorkflowDefinition.php index 8651c67c..e8818f7a 100644 --- a/src/DataObjects/WorkflowDefinition.php +++ b/src/DataObjects/WorkflowDefinition.php @@ -342,11 +342,11 @@ public function getCMSFields() 'WorkflowStatus' => ['Active', 'Paused'] ]); - $active = new GridField( + $active = GridField::create( 'Active', _t('WorkflowDefinition.WORKFLOWACTIVEIINSTANCES', 'Active Workflow Instances'), $active, - new GridFieldConfig_RecordEditor() + GridFieldConfig_RecordEditor::create() ); $active->getConfig()->removeComponentsByType(GridFieldAddNewButton::class); @@ -354,19 +354,19 @@ public function getCMSFields() if (!Permission::check('REASSIGN_ACTIVE_WORKFLOWS')) { $active->getConfig()->removeComponentsByType(GridFieldEditButton::class); - $active->getConfig()->addComponent(new GridFieldViewButton()); - $active->getConfig()->addComponent(new GridFieldDetailForm()); + $active->getConfig()->addComponent(GridFieldViewButton::create()); + $active->getConfig()->addComponent(GridFieldDetailForm::create()); } $completed = $this->Instances()->filter([ 'WorkflowStatus' => ['Complete', 'Cancelled'] ]); - $config = new GridFieldConfig_Base(); - $config->addComponent(new GridFieldEditButton()); - $config->addComponent(new GridFieldDetailForm()); + $config = GridFieldConfig_Base::create(); + $config->addComponent(GridFieldEditButton::create()); + $config->addComponent(GridFieldDetailForm::create()); - $completed = new GridField( + $completed = GridField::create( 'Completed', _t('WorkflowDefinition.WORKFLOWCOMPLETEDIINSTANCES', 'Completed Workflow Instances'), $completed, diff --git a/src/DataObjects/WorkflowInstance.php b/src/DataObjects/WorkflowInstance.php index e306f847..340cf7f7 100644 --- a/src/DataObjects/WorkflowInstance.php +++ b/src/DataObjects/WorkflowInstance.php @@ -157,7 +157,7 @@ public function getCMSFields() 'WorkflowID' => $this->ID )); - $grid = new GridField( + $grid = GridField::create( 'Actions', _t('WorkflowInstance.ActionLogTitle', 'Log'), $items diff --git a/src/Extensions/WorkflowApplicable.php b/src/Extensions/WorkflowApplicable.php index 063d7087..6debcbd3 100644 --- a/src/Extensions/WorkflowApplicable.php +++ b/src/Extensions/WorkflowApplicable.php @@ -161,12 +161,12 @@ public function updateFields(FieldList $fields) } if ($this->owner->ID) { - $config = new GridFieldConfig_Base(); - $config->addComponent(new GridFieldEditButton()); - $config->addComponent(new GridFieldDetailForm()); + $config = GridFieldConfig_Base::create(); + $config->addComponent(GridFieldEditButton::create()); + $config->addComponent(GridFieldDetailForm::create()); $insts = $this->owner->WorkflowInstances(); - $log = new GridField( + $log = GridField::create( 'WorkflowLog', _t('WorkflowApplicable.WORKFLOWLOG', 'Workflow Log'), $insts, diff --git a/src/Forms/gridfield/GridFieldExportAction.php b/src/Forms/gridfield/GridFieldExportAction.php index fd81f9d2..941f647a 100644 --- a/src/Forms/gridfield/GridFieldExportAction.php +++ b/src/Forms/gridfield/GridFieldExportAction.php @@ -5,6 +5,7 @@ use SilverStripe\Control\Controller; use SilverStripe\Control\Director; use SilverStripe\Core\Config\Config; +use SilverStripe\Forms\GridField\AbstractGridFieldComponent; use SilverStripe\Forms\GridField\GridField; use SilverStripe\Forms\GridField\GridField_ActionProvider; use SilverStripe\Forms\GridField\GridField_ColumnProvider; @@ -22,7 +23,7 @@ * @license BSD License (http://silverstripe.org/bsd-license/) * @package advancedworkflow */ -class GridFieldExportAction implements GridField_ColumnProvider, GridField_ActionProvider +class GridFieldExportAction extends AbstractGridFieldComponent implements GridField_ColumnProvider, GridField_ActionProvider { /** * Add a column 'Delete' diff --git a/src/Forms/gridfield/GridFieldWorkflowRestrictedEditButton.php b/src/Forms/gridfield/GridFieldWorkflowRestrictedEditButton.php index e741224c..a78992da 100644 --- a/src/Forms/gridfield/GridFieldWorkflowRestrictedEditButton.php +++ b/src/Forms/gridfield/GridFieldWorkflowRestrictedEditButton.php @@ -3,6 +3,7 @@ namespace Symbiote\AdvancedWorkflow\Forms\GridField; use SilverStripe\Control\Controller; +use SilverStripe\Forms\GridField\AbstractGridFieldComponent; use SilverStripe\Forms\GridField\GridField_ColumnProvider; use SilverStripe\Forms\GridField\GridFieldEditButton; use SilverStripe\Security\Permission; @@ -14,7 +15,7 @@ * * @package advancedworkflow */ -class GridFieldWorkflowRestrictedEditButton implements GridField_ColumnProvider +class GridFieldWorkflowRestrictedEditButton extends AbstractGridFieldComponent implements GridField_ColumnProvider { /** * Add a column