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 publishing of already published versioned objects for 3.3 branch #339

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
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
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@
"expose": [
"css",
"javascript"
]
],
"branch-alias": {
"dev-master": "4.x-dev"
}
},
"replace": {
"ajshort/silverstripe-gridfieldextensions": "self.version",
Expand Down
21 changes: 21 additions & 0 deletions docs/en/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ $grid->getConfig()->getComponentByType(GridFieldEditableColumns::class)->setDisp
Editing data contained in `many_many_extraFields` is supported - just treat it as you would any
other field.

**Please note:** If you are using a GridFieldConfig that is scaffolded by GridField by default,
and adding GridFieldEditableColumns into it, you will probably want to add it before the edit and/or
delete buttons:

```php
$gridFieldConfig->addComponent($editableColumns, GridFieldEditButton::class);
```

Multi Class Adding
------------------

Expand Down Expand Up @@ -106,6 +114,19 @@ 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
Expand Down
40 changes: 40 additions & 0 deletions src/GridFieldOrderableRows.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,18 @@ class GridFieldOrderableRows extends RequestHandler implements
*/
protected $reorderColumnNumber = 0;

/**
* 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;

/**
* @param string $sortField
* @param boolean $republishLiveRecords
*/
public function __construct($sortField = 'Sort')
{
Expand Down Expand Up @@ -138,6 +148,28 @@ public function setImmediateUpdate($bool)
return $this;
}

/**
* @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;
}

/**
* @return string|array
*/
Expand Down Expand Up @@ -647,7 +679,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);
}
}
}
}
Expand Down