Skip to content

Commit

Permalink
Merge pull request #334 from josephlewisnz/feature/republish-live-rec…
Browse files Browse the repository at this point in the history
…ords-3

Feature/republish live records 3
  • Loading branch information
Maxime Rainville authored Nov 18, 2022
2 parents e9f202f + 2928504 commit d5d438c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
11 changes: 11 additions & 0 deletions docs/en/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,17 @@ 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 = GridFieldOrderableRows::create()->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
Expand Down
39 changes: 39 additions & 0 deletions src/GridFieldOrderableRows.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -628,7 +659,15 @@ protected function reorderItems($list, array $values, array $sortedIDs)

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);
}
}
}
}
Expand Down

0 comments on commit d5d438c

Please sign in to comment.