From fb56c5a4e1e5bfae7b1b3f2e71a6b8c724959fd8 Mon Sep 17 00:00:00 2001 From: Luke Fromhold Date: Mon, 5 Apr 2021 09:36:03 +1000 Subject: [PATCH 1/7] Added methods to get plural and singular names of ListedRoot child common class. --- CHANGELOG.md | 4 ++++ src/Extensions/ListingsSiteTreeExtension.php | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ce355a..c754e68 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). +## [1.0.4] + +Added methods to get plural and singular names of ListedRoot child common class. + ## [1.0.0] Initial project release diff --git a/src/Extensions/ListingsSiteTreeExtension.php b/src/Extensions/ListingsSiteTreeExtension.php index 642323b..4d64ed6 100644 --- a/src/Extensions/ListingsSiteTreeExtension.php +++ b/src/Extensions/ListingsSiteTreeExtension.php @@ -73,4 +73,16 @@ public function getListedPagesCommonClass() return $class; } + + public function getListedPagesCommonSingularName() + { + $class = $this->getOwner()->getListedPagesCommonClass(); + return $class::singleton()->i18n_singular_name(); + } + + public function getListedPagesCommonPluralName() + { + $class = $this->getOwner()->getListedPagesCommonClass(); + return $class::singleton()->i18n_plural_name(); + } } From d61094d5a7d471d1e4cbca8bddca6a6e2e51f907 Mon Sep 17 00:00:00 2001 From: Luke Fromhold Date: Wed, 6 Apr 2022 07:57:21 +1000 Subject: [PATCH 2/7] Add ability to show empty string in dropdown --- src/Forms/GridFieldListedPagesAddNewButton.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Forms/GridFieldListedPagesAddNewButton.php b/src/Forms/GridFieldListedPagesAddNewButton.php index 43f9feb..d335b8f 100644 --- a/src/Forms/GridFieldListedPagesAddNewButton.php +++ b/src/Forms/GridFieldListedPagesAddNewButton.php @@ -31,6 +31,8 @@ */ class GridFieldListedPagesAddNewButton extends GridFieldAddNewButton implements GridField_ActionProvider { + private static $showEmptyString = false; + /** * Determine the list of classnames and titles allowed for a given parent object * @@ -72,6 +74,10 @@ public function getHTMLFragments($gridField) ->setFieldHolderTemplate(__CLASS__ . '_holder') ->addExtraClass('gridfield-dropdown gridfield-listedpages no-change-track'); + if (Config::inst()->get(__CLASS__, 'showEmptyString')) { + $pageTypes->setEmptyString(_t(__CLASS__ . '.SELECTTYPETOCREATE', '(Select type to create)')); + } + $state->pageType = $parent->defaultChild(); if (!$this->buttonName) { From 4365470b6ff808070f25858c13a570b033251b1c Mon Sep 17 00:00:00 2001 From: Luke Fromhold Date: Mon, 2 May 2022 16:55:14 +1000 Subject: [PATCH 3/7] Fix apparent issue with AddNewButton by showing empty string by default --- _config/config.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/_config/config.yml b/_config/config.yml index 024f5ca..22543ce 100644 --- a/_config/config.yml +++ b/_config/config.yml @@ -1,3 +1,8 @@ --- Name: fromholdio-listings +After: + - gridfieldextensions --- + +Fromholdio\Listings\Forms\GridFieldListedPagesAddNewButton: + showEmptyString: true From 2fccfc8c3b1a55c171ee9be8677d50b7fde30012 Mon Sep 17 00:00:00 2001 From: Luke Fromhold Date: Mon, 4 Jul 2022 05:04:19 +1000 Subject: [PATCH 4/7] Display page cmsfields as Left-side tabs in ListedPagesAdmin detail view; optionally append page Settings as additional right-side tab --- src/Forms/ListedPageGridFieldItemRequest.php | 56 ++++++++++++++++++-- 1 file changed, 53 insertions(+), 3 deletions(-) diff --git a/src/Forms/ListedPageGridFieldItemRequest.php b/src/Forms/ListedPageGridFieldItemRequest.php index db8df84..12b2f32 100644 --- a/src/Forms/ListedPageGridFieldItemRequest.php +++ b/src/Forms/ListedPageGridFieldItemRequest.php @@ -2,6 +2,9 @@ namespace Fromholdio\Listings\Forms; +use SilverStripe\Forms\FieldList; +use SilverStripe\Forms\TabSet; +use SilverStripe\Security\Permission; use SilverStripe\Versioned\VersionedGridFieldItemRequest; class ListedPageGridFieldItemRequest extends VersionedGridFieldItemRequest @@ -12,11 +15,58 @@ class ListedPageGridFieldItemRequest extends VersionedGridFieldItemRequest public function ItemEditForm() { - if (!empty($this->getRecord()) && !$this->getRecord()->ParentID) { - if ($this->getDefaultParentID() !== null) { - $this->getRecord()->ParentID = $this->getDefaultParentID(); + $record = $this->getRecord(); + $fields = $this->component->getFields(); + + $doPlaceCMSFieldsUnderRootTabSet = empty($fields) && !empty($record) + && $record->hasMethod('doPlaceCMSFieldsUnderListedPagesAdminRootTabSet') + && $record->doPlaceCMSFieldsUnderListedPagesAdminRootTabSet(); + + if ($doPlaceCMSFieldsUnderRootTabSet) + { + $singularName = empty($record) ? 'Page' : $record->i18n_singular_name(); + $cmsTabSet = TabSet::create('PageTabSet', $singularName); + + $fields = FieldList::create( + TabSet::create('Root', + $cmsTabSet = TabSet::create('CMSFieldsTabSet', $singularName), + $settingsTabSet = TabSet::create( + 'SettingsTabSet', _t(self::class . '.SETTINGSTABSET', 'Settings') + ) + ) + ); + + $cmsFields = $this->record->getCMSFields(); + $rootTabSet = $cmsFields->fieldByName('Root'); + foreach ($rootTabSet->Tabs() as $tab) { + $cmsTabSet->push($tab); + } + + if ( + !empty($record) + && $record->hasMethod('getSettingsFields') + && $record->hasMethod('doAddSettingsFieldsAsListedPagesAdminTab') + && $record->doAddSettingsFieldsAsListedPagesAdminTab() + ) { + $settingsFields = $record->getSettingsFields(); + $settingsRootTabSet = $settingsFields->fieldByName('Root'); + foreach ($settingsRootTabSet->Tabs() as $tab) { + $settingsTabSet->push($tab); + } + } + else { + $fields->removeByName('SettingsTabSet'); + } + + $this->component->setFields($fields); + } + + if (!empty($record) && empty($record->getField('ParentID'))) { + if (!empty($this->getDefaultParentID())) { + $record->setField('ParentID', $this->getDefaultParentID()); } } + return parent::ItemEditForm(); } From 8a54f639f31da7b3e1a7c250e586c93c8c1bcb9a Mon Sep 17 00:00:00 2001 From: Luke Fromhold <576903+dizzystuff@users.noreply.github.com> Date: Mon, 4 Jul 2022 05:09:19 +1000 Subject: [PATCH 5/7] Update README.md --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 5b8a38b..486aa43 100644 --- a/README.md +++ b/README.md @@ -31,3 +31,11 @@ More thorough docs to come. In the meantime please submit questions as issues. ## To Do * Documentation and usage examples + +## When using ListedPagesAdmin (ModelAdmin subclass) to manage pages + +Add `doPlaceCMSFieldsUnderListedPagesAdminRootTabSet():bool` to your ListedPage class, and when displayed inside a ListedPagesAdmin the page fields' TabSets and Tabs will be displayed on the left side (like regularly viewed pages) rather than the top right. + +Further add `doAddSettingsFieldsAsListedPagesAdminTab():bool` to ListedPage class, and the page's settings fields will be displayed per regularly viewed pages as a Settings tab on the top right. This may/may not work for your specific class, where the same field name exists in your pages' getCMSFields and getSettingsFields. You'll need to manage that. + + From 39fd1631585a9db042badba0848b285bb71f1216 Mon Sep 17 00:00:00 2001 From: Luke Fromhold Date: Tue, 5 Jul 2022 12:12:11 +1000 Subject: [PATCH 6/7] initial commit of playground changes --- composer.json | 1 + src/Admin/ListedPagesAdmin.php | 55 ++++++++++-- src/Extensions/ListedPageExtension.php | 3 +- src/Extensions/ListingsRootPageExtension.php | 39 +++++---- src/Forms/GridFieldConfig_ListedPages.php | 84 +++++++++++++++---- src/Forms/GridFieldListedPagesEditButton.php | 24 ++---- src/Forms/ListedPageGridFieldDetailForm.php | 3 +- .../Forms/GridFieldListedPagesAddNewButton.ss | 6 +- 8 files changed, 156 insertions(+), 59 deletions(-) diff --git a/composer.json b/composer.json index 5da0288..6aa627b 100644 --- a/composer.json +++ b/composer.json @@ -17,6 +17,7 @@ "silverstripe/vendor-plugin": "^1.0", "silverstripe/framework": "^4.0", "fromholdio/silverstripe-commonancestor": "^1.0", + "fromholdio/silverstripe-helpers-gridfields": "^1.0.0", "symbiote/silverstripe-gridfieldextensions": "^3.0" }, "autoload": { diff --git a/src/Admin/ListedPagesAdmin.php b/src/Admin/ListedPagesAdmin.php index 1e91082..5af7f79 100644 --- a/src/Admin/ListedPagesAdmin.php +++ b/src/Admin/ListedPagesAdmin.php @@ -10,6 +10,7 @@ use Fromholdio\Listings\ListedPages; use SilverStripe\Admin\ModelAdmin; use SilverStripe\CMS\Model\SiteTree; +use SilverStripe\Core\ClassInfo; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\Form; use SilverStripe\Forms\GridField\GridField; @@ -17,6 +18,7 @@ use SilverStripe\Forms\GridField\GridFieldDetailForm; use SilverStripe\Forms\Tab; use SilverStripe\Forms\TabSet; +use SilverStripe\Listings\Forms\GridFieldConfig_ListedPages; use SilverStripe\Listings\Forms\GridFieldConfig_ListedPagesAdmin; use SilverStripe\View\Requirements; use Symbiote\GridFieldExtensions\GridFieldAddNewMultiClass; @@ -309,20 +311,24 @@ protected function getEditFormForSiteTrees($id = null, $fields = null) { $form = parent::getEditForm($id, $fields); - if (singleton($this->modelClass)->hasExtension(ListedPageExtension::class)) { - + if (singleton($this->modelClass)->hasExtension(ListedPageExtension::class)) + { + /** @var GridField $gridField */ $gridField = $form->Fields()->fieldByName( $this->sanitiseClassName($this->modelClass) ); - $gridConfig = $gridField->getConfig(); + $multiClasses = $this->getManagedAddNewMultiClasses($this->modelClass); + $showAdd = empty($multiClasses); - $detailForm = $gridConfig->getComponentByType(GridFieldDetailForm::class); - if ($detailForm !== null) { - $detailForm->setItemRequestClass( - ListedPageGridFieldItemRequest::class - ); + $gridConfig = GridFieldConfig_ListedPages::create( + null, 20, true, $showAdd, null, true + ); + if (!empty($multiClasses)) { + $gridConfig->addMultiAdder($multiClasses); } + + $gridField->setConfig($gridConfig); } $this->extend('updateEditFormForSiteTrees', $form); @@ -356,11 +362,44 @@ protected function getListForListingsRootPage($root) protected function getListForSiteTrees() { $list = parent::getList(); + $multiClasses = $this->getManagedAddNewMultiClasses($this->modelClass); + if (!empty($multiClasses)) + { + $classesFilter = []; + foreach ($multiClasses as $class) + { + $subclasses = array_values(ClassInfo::subclassesFor($class)); + $classesFilter = [...$classesFilter, ...$subclasses]; + } + $list = $list->filter('ClassName', $classesFilter); + } + $sort = $this->getManagedModelSort($this->modelClass); + if (!is_null($sort)) { + $list = $list->sort($sort); + } $this->extend('updateListForSiteTrees', $list); return $list; } + protected function getManagedAddNewMultiClasses(string $modelClass): ?array + { + $multiClasses = static::config()->get('managed_add_new_multi'); + if (empty($multiClasses)) return null; + $modelMultiClasses = $multiClasses[$modelClass] ?? null; + if (empty($modelMultiClasses)) return null; + return $modelMultiClasses; + } + + protected function getManagedModelSort(string $modelClass): ?string + { + $sorts = static::config()->get('managed_model_sort'); + if (empty($sorts)) return null; + $modelSort = $sorts[$modelClass] ?? null; + if (empty($modelSort)) return null; + return $modelSort; + } + /** * Return null for export/import/search -related functions diff --git a/src/Extensions/ListedPageExtension.php b/src/Extensions/ListedPageExtension.php index 56910c6..12a08de 100644 --- a/src/Extensions/ListedPageExtension.php +++ b/src/Extensions/ListedPageExtension.php @@ -106,7 +106,8 @@ public function getDefaultListingsRoot() public function getAvailableListingsRoots() { - return ListingsRoots::get($this->getOwner()->getAvailableListingsRootsClasses()); + $classes = $this->getOwner()->getAvailableListingsRootsClasses(); + return empty($classes) ? null : ListingsRoots::get($classes); } public function getAvailableListingsRootsClasses() diff --git a/src/Extensions/ListingsRootPageExtension.php b/src/Extensions/ListingsRootPageExtension.php index e58c970..906705c 100644 --- a/src/Extensions/ListingsRootPageExtension.php +++ b/src/Extensions/ListingsRootPageExtension.php @@ -2,12 +2,17 @@ namespace Fromholdio\Listings\Extensions; +use Fromholdio\Listings\Forms\GridFieldListedPagesAddNewButton; +use Fromholdio\Listings\Forms\GridFieldListedPagesEditButton; use Fromholdio\Listings\ListingsRoots; use SilverStripe\Admin\LeftAndMain; use SilverStripe\Control\Controller; use SilverStripe\Core\ClassInfo; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\GridField\GridField; +use SilverStripe\Forms\GridField\GridField_ActionMenu; +use SilverStripe\Forms\GridField\GridFieldAddNewButton; +use SilverStripe\Forms\GridField\GridFieldEditButton; use SilverStripe\Forms\GridField\GridFieldPageCount; use SilverStripe\Forms\GridField\GridFieldPaginator; use SilverStripe\Forms\HeaderField; @@ -107,32 +112,36 @@ public function updateCMSFields(FieldList $fields) // Else build the GridField else { + $classes = $this->getOwner()->getListedPagesClasses(); + $showAdd = count($classes) === 1; + + $orderableSort = $this->getOwner()->getListedPagesOrderableSort(); + $sort = empty($orderableSort) ? null : $orderableSort; + $listedPagesField = GridField::create( 'ListedPages', $pluralName, $this->getListedPages(), - $listedPagesFieldConfig = GridFieldConfig_ListedPages::create() + $listedPagesFieldConfig = GridFieldConfig_ListedPages::create( + $sort, 20, true, $showAdd, ['ParentID' => $this->ID], true + ) ); + $listedPagesFieldConfig + ->removeComponentsByType([ + GridFieldAddNewButton::class, + GridFieldEditButton::class, + GridField_ActionMenu::class + ]) + ->addComponent(new GridFieldListedPagesAddNewButton()) + ->addComponent(new GridFieldListedPagesEditButton()) + ->addComponent(new GridField_ActionMenu()); + // In the absence of a real has_many/many_many relation defined, // ensure GridField has a model class name to get summary_fields from. $listedPagesField->setModelClass( $this->owner->getListedPagesCommonClass() ); - - // If orderable sort field name is supplied, apply GridFieldOrderableRows. - $orderableSort = $this->owner->getListedPagesOrderableSort(); - if ($orderableSort) { - $listedPagesFieldConfig - ->addComponent(new GridFieldOrderableRows($orderableSort)); - } - - $listedPagesFieldConfig - ->removeComponentsByType([ - GridFieldPageCount::class, - GridFieldPaginator::class - ]) - ->addComponent(new GridFieldConfigurablePaginator()); } // Add field to new Tab, named using $pluralName with spaces removed diff --git a/src/Forms/GridFieldConfig_ListedPages.php b/src/Forms/GridFieldConfig_ListedPages.php index c0b745e..e1efed3 100644 --- a/src/Forms/GridFieldConfig_ListedPages.php +++ b/src/Forms/GridFieldConfig_ListedPages.php @@ -2,39 +2,93 @@ namespace SilverStripe\Listings\Forms; +use Fromholdio\Helpers\GridFields\Forms\GridFieldConfig_Core; +use Fromholdio\Helpers\GridFields\Forms\GridFieldExtraDataDetailForm; use Fromholdio\Listings\Forms\GridFieldListedPagesAddNewButton; use Fromholdio\Listings\Forms\GridFieldListedPagesEditButton; +use Fromholdio\Listings\Forms\ListedPageGridFieldAddNewMultiClassHandler; +use Fromholdio\Listings\Forms\ListedPageGridFieldDetailForm; +use Fromholdio\Listings\Forms\ListedPageGridFieldItemRequest; +use SilverStripe\Forms\GridField\GridField_ActionMenu; +use SilverStripe\Forms\GridField\GridFieldAddNewButton; use SilverStripe\Forms\GridField\GridFieldButtonRow; use SilverStripe\Forms\GridField\GridFieldConfig; use SilverStripe\Forms\GridField\GridFieldDataColumns; +use SilverStripe\Forms\GridField\GridFieldDeleteAction; +use SilverStripe\Forms\GridField\GridFieldDetailForm; +use SilverStripe\Forms\GridField\GridFieldEditButton; +use SilverStripe\Forms\GridField\GridFieldExportButton; use SilverStripe\Forms\GridField\GridFieldFilterHeader; use SilverStripe\Forms\GridField\GridFieldPageCount; use SilverStripe\Forms\GridField\GridFieldPaginator; +use SilverStripe\Forms\GridField\GridFieldPrintButton; use SilverStripe\Forms\GridField\GridFieldSortableHeader; use SilverStripe\Forms\GridField\GridFieldToolbarHeader; +use SilverStripe\Versioned\GridFieldArchiveAction; use SilverStripe\Versioned\VersionedGridFieldState\VersionedGridFieldState; +use Symbiote\GridFieldExtensions\GridFieldAddNewMultiClass; +use Symbiote\GridFieldExtensions\GridFieldTitleHeader; -class GridFieldConfig_ListedPages extends GridFieldConfig +class GridFieldConfig_ListedPages extends GridFieldConfig_Core { /** * @param int|null $itemsPerPage */ - public function __construct($itemsPerPage = null) - { - parent::__construct($itemsPerPage); + public function __construct( + ?string $orderableField = null, + ?int $itemsPerPage = 20, + bool $showPagination = true, + bool $showAdd = true, + ?array $extraData = null, + bool $doWrite = false, + ?bool $removeRelation = true + ) { + parent::__construct( + $orderableField, + $itemsPerPage, + $showPagination, + $showAdd, + $extraData, + $doWrite + ); + + $this->removeComponentsByType([ + GridFieldPrintButton::class, + GridFieldExportButton::class, + GridFieldDetailForm::class, + GridFieldDeleteAction::class, + GridFieldTitleHeader::class + ]); - $this->addComponent(new GridFieldButtonRow('before')); - $this->addComponent(new GridFieldListedPagesAddNewButton('buttons-before-left')); - $this->addComponent(new GridFieldToolbarHeader()); - $this->addComponent(new GridFieldSortableHeader()); - $this->addComponent(new GridFieldFilterHeader()); - $this->addComponent(new GridFieldDataColumns()); + $detailForm = new ListedPageGridFieldDetailForm( + null, + $showPagination, + $showAdd, + $extraData, + $doWrite + ); + $detailForm->setItemRequestClass(ListedPageGridFieldItemRequest::class); + $this->addComponent($detailForm); $this->addComponent(new VersionedGridFieldState()); - $this->addComponent(new GridFieldListedPagesEditButton()); - $this->addComponent(new GridFieldPageCount('toolbar-header-right')); - $this->addComponent($pagination = new GridFieldPaginator($itemsPerPage)); -// $this->addComponent(new GridFieldSiteTreeState()); + $this->addComponent(new GridFieldArchiveAction()); + $this->addComponent(new GridField_ActionMenu()); +// $this->addComponent(GridFieldToolbarHeader::create()); + $this->addComponent($sort = GridFieldSortableHeader::create()); + $this->addComponent($filter = GridFieldFilterHeader::create()); - $pagination->setThrowExceptionOnBadDataType(true); + $sort->setThrowExceptionOnBadDataType(false); + $filter->setThrowExceptionOnBadDataType(false); + } + + public function addMultiAdder(array $classes): self + { + $adder = new GridFieldAddNewMultiClass('buttons-before-left'); + $adder->setClasses($classes); + $adder->setItemRequestClass( + ListedPageGridFieldAddNewMultiClassHandler::class + ); + $this->addComponent($adder); + $this->removeComponentsByType(GridFieldAddNewButton::class); + return $this; } } diff --git a/src/Forms/GridFieldListedPagesEditButton.php b/src/Forms/GridFieldListedPagesEditButton.php index e4cd26b..374da64 100644 --- a/src/Forms/GridFieldListedPagesEditButton.php +++ b/src/Forms/GridFieldListedPagesEditButton.php @@ -2,6 +2,7 @@ namespace Fromholdio\Listings\Forms; +use SilverStripe\Control\Controller; use SilverStripe\Forms\GridField\GridField; use SilverStripe\Forms\GridField\GridFieldEditButton; use SilverStripe\ORM\DataObject; @@ -16,22 +17,13 @@ **/ class GridFieldListedPagesEditButton extends GridFieldEditButton { - /** - * @param GridField $gridField - * @param DataObject $record - * @param string $columnName - * @return string - the HTML for the column - */ - public function getColumnContent($gridField, $record, $columnName) - { - // No permission checks - handled through GridFieldDetailForm - // which can make the form readonly if no edit permissions are available. - - $data = ArrayData::create([ - 'Link' => $record->CMSEditLink(), - 'ExtraClass' => $this->getExtraClass(), - ]); - return $data->renderWith(GridFieldEditButton::class); + public function getUrl($gridField, $record, $columnName, $addState = true) + { + $link = $record->CMSEditLink(); + if ($addState) { + $link = $this->getStateManager()->addStateToURL($gridField, $link); + } + return $link; } } diff --git a/src/Forms/ListedPageGridFieldDetailForm.php b/src/Forms/ListedPageGridFieldDetailForm.php index f47f20e..db14048 100644 --- a/src/Forms/ListedPageGridFieldDetailForm.php +++ b/src/Forms/ListedPageGridFieldDetailForm.php @@ -2,10 +2,11 @@ namespace Fromholdio\Listings\Forms; +use Fromholdio\Helpers\GridFields\Forms\GridFieldExtraDataDetailForm; use SilverStripe\Forms\GridField\GridField_URLHandler; use SilverStripe\Forms\GridField\GridFieldDetailForm; -class ListedPageGridFieldDetailForm extends GridFieldDetailForm implements GridField_URLHandler +class ListedPageGridFieldDetailForm extends GridFieldExtraDataDetailForm implements GridField_URLHandler { protected $defaultParentID; diff --git a/templates/Fromholdio/Listings/Forms/GridFieldListedPagesAddNewButton.ss b/templates/Fromholdio/Listings/Forms/GridFieldListedPagesAddNewButton.ss index 6202e8f..870e1c2 100644 --- a/templates/Fromholdio/Listings/Forms/GridFieldListedPagesAddNewButton.ss +++ b/templates/Fromholdio/Listings/Forms/GridFieldListedPagesAddNewButton.ss @@ -1,3 +1,3 @@ -<% loop Fields %> - $FieldHolder -<% end_loop %> \ No newline at end of file +<% loop $Fields %> + $FieldHolder +<% end_loop %> From 7aeb1febb82633fc0fd9601f6351847aa258163c Mon Sep 17 00:00:00 2001 From: Luke Fromhold Date: Tue, 5 Jul 2022 12:25:33 +1000 Subject: [PATCH 7/7] Revert "initial commit of playground changes" off of master This reverts commit 39fd1631585a9db042badba0848b285bb71f1216. --- composer.json | 1 - src/Admin/ListedPagesAdmin.php | 55 ++---------- src/Extensions/ListedPageExtension.php | 3 +- src/Extensions/ListingsRootPageExtension.php | 39 ++++----- src/Forms/GridFieldConfig_ListedPages.php | 84 ++++--------------- src/Forms/GridFieldListedPagesEditButton.php | 24 ++++-- src/Forms/ListedPageGridFieldDetailForm.php | 3 +- .../Forms/GridFieldListedPagesAddNewButton.ss | 6 +- 8 files changed, 59 insertions(+), 156 deletions(-) diff --git a/composer.json b/composer.json index 6aa627b..5da0288 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,6 @@ "silverstripe/vendor-plugin": "^1.0", "silverstripe/framework": "^4.0", "fromholdio/silverstripe-commonancestor": "^1.0", - "fromholdio/silverstripe-helpers-gridfields": "^1.0.0", "symbiote/silverstripe-gridfieldextensions": "^3.0" }, "autoload": { diff --git a/src/Admin/ListedPagesAdmin.php b/src/Admin/ListedPagesAdmin.php index 5af7f79..1e91082 100644 --- a/src/Admin/ListedPagesAdmin.php +++ b/src/Admin/ListedPagesAdmin.php @@ -10,7 +10,6 @@ use Fromholdio\Listings\ListedPages; use SilverStripe\Admin\ModelAdmin; use SilverStripe\CMS\Model\SiteTree; -use SilverStripe\Core\ClassInfo; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\Form; use SilverStripe\Forms\GridField\GridField; @@ -18,7 +17,6 @@ use SilverStripe\Forms\GridField\GridFieldDetailForm; use SilverStripe\Forms\Tab; use SilverStripe\Forms\TabSet; -use SilverStripe\Listings\Forms\GridFieldConfig_ListedPages; use SilverStripe\Listings\Forms\GridFieldConfig_ListedPagesAdmin; use SilverStripe\View\Requirements; use Symbiote\GridFieldExtensions\GridFieldAddNewMultiClass; @@ -311,24 +309,20 @@ protected function getEditFormForSiteTrees($id = null, $fields = null) { $form = parent::getEditForm($id, $fields); - if (singleton($this->modelClass)->hasExtension(ListedPageExtension::class)) - { - /** @var GridField $gridField */ + if (singleton($this->modelClass)->hasExtension(ListedPageExtension::class)) { + $gridField = $form->Fields()->fieldByName( $this->sanitiseClassName($this->modelClass) ); - $multiClasses = $this->getManagedAddNewMultiClasses($this->modelClass); - $showAdd = empty($multiClasses); + $gridConfig = $gridField->getConfig(); - $gridConfig = GridFieldConfig_ListedPages::create( - null, 20, true, $showAdd, null, true - ); - if (!empty($multiClasses)) { - $gridConfig->addMultiAdder($multiClasses); + $detailForm = $gridConfig->getComponentByType(GridFieldDetailForm::class); + if ($detailForm !== null) { + $detailForm->setItemRequestClass( + ListedPageGridFieldItemRequest::class + ); } - - $gridField->setConfig($gridConfig); } $this->extend('updateEditFormForSiteTrees', $form); @@ -362,44 +356,11 @@ protected function getListForListingsRootPage($root) protected function getListForSiteTrees() { $list = parent::getList(); - $multiClasses = $this->getManagedAddNewMultiClasses($this->modelClass); - if (!empty($multiClasses)) - { - $classesFilter = []; - foreach ($multiClasses as $class) - { - $subclasses = array_values(ClassInfo::subclassesFor($class)); - $classesFilter = [...$classesFilter, ...$subclasses]; - } - $list = $list->filter('ClassName', $classesFilter); - } - $sort = $this->getManagedModelSort($this->modelClass); - if (!is_null($sort)) { - $list = $list->sort($sort); - } $this->extend('updateListForSiteTrees', $list); return $list; } - protected function getManagedAddNewMultiClasses(string $modelClass): ?array - { - $multiClasses = static::config()->get('managed_add_new_multi'); - if (empty($multiClasses)) return null; - $modelMultiClasses = $multiClasses[$modelClass] ?? null; - if (empty($modelMultiClasses)) return null; - return $modelMultiClasses; - } - - protected function getManagedModelSort(string $modelClass): ?string - { - $sorts = static::config()->get('managed_model_sort'); - if (empty($sorts)) return null; - $modelSort = $sorts[$modelClass] ?? null; - if (empty($modelSort)) return null; - return $modelSort; - } - /** * Return null for export/import/search -related functions diff --git a/src/Extensions/ListedPageExtension.php b/src/Extensions/ListedPageExtension.php index 12a08de..56910c6 100644 --- a/src/Extensions/ListedPageExtension.php +++ b/src/Extensions/ListedPageExtension.php @@ -106,8 +106,7 @@ public function getDefaultListingsRoot() public function getAvailableListingsRoots() { - $classes = $this->getOwner()->getAvailableListingsRootsClasses(); - return empty($classes) ? null : ListingsRoots::get($classes); + return ListingsRoots::get($this->getOwner()->getAvailableListingsRootsClasses()); } public function getAvailableListingsRootsClasses() diff --git a/src/Extensions/ListingsRootPageExtension.php b/src/Extensions/ListingsRootPageExtension.php index 906705c..e58c970 100644 --- a/src/Extensions/ListingsRootPageExtension.php +++ b/src/Extensions/ListingsRootPageExtension.php @@ -2,17 +2,12 @@ namespace Fromholdio\Listings\Extensions; -use Fromholdio\Listings\Forms\GridFieldListedPagesAddNewButton; -use Fromholdio\Listings\Forms\GridFieldListedPagesEditButton; use Fromholdio\Listings\ListingsRoots; use SilverStripe\Admin\LeftAndMain; use SilverStripe\Control\Controller; use SilverStripe\Core\ClassInfo; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\GridField\GridField; -use SilverStripe\Forms\GridField\GridField_ActionMenu; -use SilverStripe\Forms\GridField\GridFieldAddNewButton; -use SilverStripe\Forms\GridField\GridFieldEditButton; use SilverStripe\Forms\GridField\GridFieldPageCount; use SilverStripe\Forms\GridField\GridFieldPaginator; use SilverStripe\Forms\HeaderField; @@ -112,36 +107,32 @@ public function updateCMSFields(FieldList $fields) // Else build the GridField else { - $classes = $this->getOwner()->getListedPagesClasses(); - $showAdd = count($classes) === 1; - - $orderableSort = $this->getOwner()->getListedPagesOrderableSort(); - $sort = empty($orderableSort) ? null : $orderableSort; - $listedPagesField = GridField::create( 'ListedPages', $pluralName, $this->getListedPages(), - $listedPagesFieldConfig = GridFieldConfig_ListedPages::create( - $sort, 20, true, $showAdd, ['ParentID' => $this->ID], true - ) + $listedPagesFieldConfig = GridFieldConfig_ListedPages::create() ); - $listedPagesFieldConfig - ->removeComponentsByType([ - GridFieldAddNewButton::class, - GridFieldEditButton::class, - GridField_ActionMenu::class - ]) - ->addComponent(new GridFieldListedPagesAddNewButton()) - ->addComponent(new GridFieldListedPagesEditButton()) - ->addComponent(new GridField_ActionMenu()); - // In the absence of a real has_many/many_many relation defined, // ensure GridField has a model class name to get summary_fields from. $listedPagesField->setModelClass( $this->owner->getListedPagesCommonClass() ); + + // If orderable sort field name is supplied, apply GridFieldOrderableRows. + $orderableSort = $this->owner->getListedPagesOrderableSort(); + if ($orderableSort) { + $listedPagesFieldConfig + ->addComponent(new GridFieldOrderableRows($orderableSort)); + } + + $listedPagesFieldConfig + ->removeComponentsByType([ + GridFieldPageCount::class, + GridFieldPaginator::class + ]) + ->addComponent(new GridFieldConfigurablePaginator()); } // Add field to new Tab, named using $pluralName with spaces removed diff --git a/src/Forms/GridFieldConfig_ListedPages.php b/src/Forms/GridFieldConfig_ListedPages.php index e1efed3..c0b745e 100644 --- a/src/Forms/GridFieldConfig_ListedPages.php +++ b/src/Forms/GridFieldConfig_ListedPages.php @@ -2,93 +2,39 @@ namespace SilverStripe\Listings\Forms; -use Fromholdio\Helpers\GridFields\Forms\GridFieldConfig_Core; -use Fromholdio\Helpers\GridFields\Forms\GridFieldExtraDataDetailForm; use Fromholdio\Listings\Forms\GridFieldListedPagesAddNewButton; use Fromholdio\Listings\Forms\GridFieldListedPagesEditButton; -use Fromholdio\Listings\Forms\ListedPageGridFieldAddNewMultiClassHandler; -use Fromholdio\Listings\Forms\ListedPageGridFieldDetailForm; -use Fromholdio\Listings\Forms\ListedPageGridFieldItemRequest; -use SilverStripe\Forms\GridField\GridField_ActionMenu; -use SilverStripe\Forms\GridField\GridFieldAddNewButton; use SilverStripe\Forms\GridField\GridFieldButtonRow; use SilverStripe\Forms\GridField\GridFieldConfig; use SilverStripe\Forms\GridField\GridFieldDataColumns; -use SilverStripe\Forms\GridField\GridFieldDeleteAction; -use SilverStripe\Forms\GridField\GridFieldDetailForm; -use SilverStripe\Forms\GridField\GridFieldEditButton; -use SilverStripe\Forms\GridField\GridFieldExportButton; use SilverStripe\Forms\GridField\GridFieldFilterHeader; use SilverStripe\Forms\GridField\GridFieldPageCount; use SilverStripe\Forms\GridField\GridFieldPaginator; -use SilverStripe\Forms\GridField\GridFieldPrintButton; use SilverStripe\Forms\GridField\GridFieldSortableHeader; use SilverStripe\Forms\GridField\GridFieldToolbarHeader; -use SilverStripe\Versioned\GridFieldArchiveAction; use SilverStripe\Versioned\VersionedGridFieldState\VersionedGridFieldState; -use Symbiote\GridFieldExtensions\GridFieldAddNewMultiClass; -use Symbiote\GridFieldExtensions\GridFieldTitleHeader; -class GridFieldConfig_ListedPages extends GridFieldConfig_Core +class GridFieldConfig_ListedPages extends GridFieldConfig { /** * @param int|null $itemsPerPage */ - public function __construct( - ?string $orderableField = null, - ?int $itemsPerPage = 20, - bool $showPagination = true, - bool $showAdd = true, - ?array $extraData = null, - bool $doWrite = false, - ?bool $removeRelation = true - ) { - parent::__construct( - $orderableField, - $itemsPerPage, - $showPagination, - $showAdd, - $extraData, - $doWrite - ); - - $this->removeComponentsByType([ - GridFieldPrintButton::class, - GridFieldExportButton::class, - GridFieldDetailForm::class, - GridFieldDeleteAction::class, - GridFieldTitleHeader::class - ]); + public function __construct($itemsPerPage = null) + { + parent::__construct($itemsPerPage); - $detailForm = new ListedPageGridFieldDetailForm( - null, - $showPagination, - $showAdd, - $extraData, - $doWrite - ); - $detailForm->setItemRequestClass(ListedPageGridFieldItemRequest::class); - $this->addComponent($detailForm); + $this->addComponent(new GridFieldButtonRow('before')); + $this->addComponent(new GridFieldListedPagesAddNewButton('buttons-before-left')); + $this->addComponent(new GridFieldToolbarHeader()); + $this->addComponent(new GridFieldSortableHeader()); + $this->addComponent(new GridFieldFilterHeader()); + $this->addComponent(new GridFieldDataColumns()); $this->addComponent(new VersionedGridFieldState()); - $this->addComponent(new GridFieldArchiveAction()); - $this->addComponent(new GridField_ActionMenu()); -// $this->addComponent(GridFieldToolbarHeader::create()); - $this->addComponent($sort = GridFieldSortableHeader::create()); - $this->addComponent($filter = GridFieldFilterHeader::create()); + $this->addComponent(new GridFieldListedPagesEditButton()); + $this->addComponent(new GridFieldPageCount('toolbar-header-right')); + $this->addComponent($pagination = new GridFieldPaginator($itemsPerPage)); +// $this->addComponent(new GridFieldSiteTreeState()); - $sort->setThrowExceptionOnBadDataType(false); - $filter->setThrowExceptionOnBadDataType(false); - } - - public function addMultiAdder(array $classes): self - { - $adder = new GridFieldAddNewMultiClass('buttons-before-left'); - $adder->setClasses($classes); - $adder->setItemRequestClass( - ListedPageGridFieldAddNewMultiClassHandler::class - ); - $this->addComponent($adder); - $this->removeComponentsByType(GridFieldAddNewButton::class); - return $this; + $pagination->setThrowExceptionOnBadDataType(true); } } diff --git a/src/Forms/GridFieldListedPagesEditButton.php b/src/Forms/GridFieldListedPagesEditButton.php index 374da64..e4cd26b 100644 --- a/src/Forms/GridFieldListedPagesEditButton.php +++ b/src/Forms/GridFieldListedPagesEditButton.php @@ -2,7 +2,6 @@ namespace Fromholdio\Listings\Forms; -use SilverStripe\Control\Controller; use SilverStripe\Forms\GridField\GridField; use SilverStripe\Forms\GridField\GridFieldEditButton; use SilverStripe\ORM\DataObject; @@ -17,13 +16,22 @@ **/ class GridFieldListedPagesEditButton extends GridFieldEditButton { - - public function getUrl($gridField, $record, $columnName, $addState = true) + /** + * @param GridField $gridField + * @param DataObject $record + * @param string $columnName + * @return string - the HTML for the column + */ + public function getColumnContent($gridField, $record, $columnName) { - $link = $record->CMSEditLink(); - if ($addState) { - $link = $this->getStateManager()->addStateToURL($gridField, $link); - } - return $link; + // No permission checks - handled through GridFieldDetailForm + // which can make the form readonly if no edit permissions are available. + + $data = ArrayData::create([ + 'Link' => $record->CMSEditLink(), + 'ExtraClass' => $this->getExtraClass(), + ]); + + return $data->renderWith(GridFieldEditButton::class); } } diff --git a/src/Forms/ListedPageGridFieldDetailForm.php b/src/Forms/ListedPageGridFieldDetailForm.php index db14048..f47f20e 100644 --- a/src/Forms/ListedPageGridFieldDetailForm.php +++ b/src/Forms/ListedPageGridFieldDetailForm.php @@ -2,11 +2,10 @@ namespace Fromholdio\Listings\Forms; -use Fromholdio\Helpers\GridFields\Forms\GridFieldExtraDataDetailForm; use SilverStripe\Forms\GridField\GridField_URLHandler; use SilverStripe\Forms\GridField\GridFieldDetailForm; -class ListedPageGridFieldDetailForm extends GridFieldExtraDataDetailForm implements GridField_URLHandler +class ListedPageGridFieldDetailForm extends GridFieldDetailForm implements GridField_URLHandler { protected $defaultParentID; diff --git a/templates/Fromholdio/Listings/Forms/GridFieldListedPagesAddNewButton.ss b/templates/Fromholdio/Listings/Forms/GridFieldListedPagesAddNewButton.ss index 870e1c2..6202e8f 100644 --- a/templates/Fromholdio/Listings/Forms/GridFieldListedPagesAddNewButton.ss +++ b/templates/Fromholdio/Listings/Forms/GridFieldListedPagesAddNewButton.ss @@ -1,3 +1,3 @@ -<% loop $Fields %> - $FieldHolder -<% end_loop %> +<% loop Fields %> + $FieldHolder +<% end_loop %> \ No newline at end of file