From 07c97e45b2631e5ef7b6e2db5afbcb3115b2e7f5 Mon Sep 17 00:00:00 2001 From: josephlewisnz Date: Thu, 17 Feb 2022 12:53:13 +1300 Subject: [PATCH 1/3] Added the ability to autopublish items that are already live --- docs/en/index.md | 12 +++++++++++ src/GridFieldOrderableRows.php | 39 ++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/docs/en/index.md b/docs/en/index.md index 151d804..3d7ab99 100644 --- a/docs/en/index.md +++ b/docs/en/index.md @@ -106,6 +106,18 @@ class Item extends DataObject { } ``` +### Versioning +By default `GridFieldOrderableRows` will handle versioning but won't automatically publish any records. The user will need to go into each record and publish them manually which could get cumbersome for large lists. + +You can configure the list to automatically publish a record if the record is the latest version and is already published. This won't publish any records which have draft changes. + +```php +$orderable = new GridFieldOrderableRows() + ->setRepublishLiveRecords(true); +``` + +There are caveats with both approaches so consideration should be made for which approach best suits the requirements. + **Please NOTE:** There is a limitation when using `GridFieldOrderableRows` on unsaved data objects; namely, that it doesn't work as without data being saved, the list of related objects has no context. Please check `$this->ID` before adding the `GridFieldOrderableRows` component to the grid field config (or even, before adding the gridfield at all). Configurable Paginator diff --git a/src/GridFieldOrderableRows.php b/src/GridFieldOrderableRows.php index 70c2f66..8f4c603 100755 --- a/src/GridFieldOrderableRows.php +++ b/src/GridFieldOrderableRows.php @@ -82,6 +82,15 @@ class GridFieldOrderableRows extends RequestHandler implements */ protected $extraSortFields = null; + /** + * If the items in the list are versioned and this is set to true, then + * we will check to see if the version we're sorting is the latest published + * version and if so then we will re-publish the item. + * + * @var boolean + */ + protected $republishLiveRecords = false; + /** * The number of the column containing the reorder handles * @@ -147,6 +156,28 @@ public function getExtraSortFields() return $this->extraSortFields; } + /** + * @see $republishLiveRecords + * + * @return boolean + */ + public function getRepublishLiveRecords() + { + return $this->republishLiveRecords; + } + + /** + * @see $republishLiveRecords + * + * @param boolean $bool + * @return GridFieldOrderableRows $this + */ + public function setRepublishLiveRecords($bool) + { + $this->republishLiveRecords = $bool; + return $this; + } + /** * Checks to see if the relationship list is for a type of many_many * @@ -658,7 +689,15 @@ protected function reorderItems($list, array $values, array $sortedIDs) $record = $currentSortList[$targetRecordID]; if ($record->$sortField != $newSortValue) { $record->$sortField = $newSortValue; + + // We need to do this before writing otherwith isLiveVersion() will always be false + $shouldRepublish = $this->getRepublishLiveRecords() && $record->isLiveVersion(); + + // Write our staged record and publish if required $record->write(); + if ($shouldRepublish) { + $record->copyVersionToStage(Versioned::DRAFT, Versioned::LIVE, true); + } } } } From 8e77095de01e53419da27b7c7ec292279b5cda7a Mon Sep 17 00:00:00 2001 From: josephlewisnz Date: Thu, 17 Feb 2022 13:10:44 +1300 Subject: [PATCH 2/3] updated to create syntax --- docs/en/index.md | 3 +-- src/GridFieldOrderableRows.php | 3 ++- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/en/index.md b/docs/en/index.md index 3d7ab99..d649be2 100644 --- a/docs/en/index.md +++ b/docs/en/index.md @@ -112,8 +112,7 @@ By default `GridFieldOrderableRows` will handle versioning but won't automatical You can configure the list to automatically publish a record if the record is the latest version and is already published. This won't publish any records which have draft changes. ```php -$orderable = new GridFieldOrderableRows() - ->setRepublishLiveRecords(true); +$orderable = GridFieldOrderableRows::create()->setRepublishLiveRecords(true); ``` There are caveats with both approaches so consideration should be made for which approach best suits the requirements. diff --git a/src/GridFieldOrderableRows.php b/src/GridFieldOrderableRows.php index 8f4c603..7787d5c 100755 --- a/src/GridFieldOrderableRows.php +++ b/src/GridFieldOrderableRows.php @@ -101,6 +101,7 @@ class GridFieldOrderableRows extends RequestHandler implements /** * @param string $sortField + * @param boolean $republishLiveRecords */ public function __construct($sortField = 'Sort') { @@ -689,7 +690,7 @@ protected function reorderItems($list, array $values, array $sortedIDs) $record = $currentSortList[$targetRecordID]; if ($record->$sortField != $newSortValue) { $record->$sortField = $newSortValue; - + // We need to do this before writing otherwith isLiveVersion() will always be false $shouldRepublish = $this->getRepublishLiveRecords() && $record->isLiveVersion(); From 2928504b3c69acc21c7065246b4150d9d1c19476 Mon Sep 17 00:00:00 2001 From: josephlewisnz Date: Fri, 18 Feb 2022 08:39:59 +1300 Subject: [PATCH 3/3] removed param --- src/GridFieldOrderableRows.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/GridFieldOrderableRows.php b/src/GridFieldOrderableRows.php index 7787d5c..815a3a4 100755 --- a/src/GridFieldOrderableRows.php +++ b/src/GridFieldOrderableRows.php @@ -101,7 +101,6 @@ class GridFieldOrderableRows extends RequestHandler implements /** * @param string $sortField - * @param boolean $republishLiveRecords */ public function __construct($sortField = 'Sort') {