Merge pull request #7773 from open-sausages/pulls/4.0/safer-gridfield-version

BUG Make GridFieldConfig less susceptible to error when versioned isn't installed
This commit is contained in:
Loz Calver 2018-01-18 09:23:06 +00:00 committed by GitHub
commit 4a8f9a8da8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 6 deletions

View File

@ -1,6 +1,8 @@
<?php <?php
namespace SilverStripe\Forms\GridField; namespace SilverStripe\Forms\GridField;
use SilverStripe\Versioned\Versioned;
/** /**
* Allows editing of records contained within the GridField, instead of only allowing the ability to view records in * Allows editing of records contained within the GridField, instead of only allowing the ability to view records in
* the GridField. * the GridField.
@ -21,7 +23,10 @@ class GridFieldConfig_RecordEditor extends GridFieldConfig
$this->addComponent($sort = new GridFieldSortableHeader()); $this->addComponent($sort = new GridFieldSortableHeader());
$this->addComponent($filter = new GridFieldFilterHeader()); $this->addComponent($filter = new GridFieldFilterHeader());
$this->addComponent(new GridFieldDataColumns()); $this->addComponent(new GridFieldDataColumns());
$this->addComponent(new GridFieldVersionedState([ 'Name', 'Title' ])); // @todo Move to versioned module, add via extension instead
if (class_exists(Versioned::class)) {
$this->addComponent(new GridFieldVersionedState(['Name', 'Title']));
}
$this->addComponent(new GridFieldEditButton()); $this->addComponent(new GridFieldEditButton());
$this->addComponent(new GridFieldDeleteAction()); $this->addComponent(new GridFieldDeleteAction());
$this->addComponent(new GridFieldPageCount('toolbar-header-right')); $this->addComponent(new GridFieldPageCount('toolbar-header-right'));

View File

@ -1,12 +1,21 @@
<?php <?php
namespace SilverStripe\Forms\GridField; namespace SilverStripe\Forms\GridField;
use SilverStripe\ORM\DataObject; use SilverStripe\ORM\DataObject;
use SilverStripe\Versioned\Versioned; use SilverStripe\Versioned\Versioned;
use SilverStripe\Core\Convert; use SilverStripe\Core\Convert;
/**
* @todo Move to siverstripe/versioned module
*/
class GridFieldVersionedState implements GridField_ColumnProvider class GridFieldVersionedState implements GridField_ColumnProvider
{ {
/**
* Column name for versioned state
*
* @var string
*/
protected $column = null; protected $column = null;
/** /**
@ -33,9 +42,12 @@ class GridFieldVersionedState implements GridField_ColumnProvider
*/ */
public function augmentColumns($gridField, &$columns) public function augmentColumns($gridField, &$columns)
{ {
if (!class_exists(Versioned::class)) {
return;
}
$model = $gridField->getModelClass(); $model = $gridField->getModelClass();
$isModelVersioned = $model::has_extension(Versioned::class); $isModelVersioned = $model::has_extension(Versioned::class);
if (!$isModelVersioned) { if (!$isModelVersioned) {
return; return;
} }
@ -61,7 +73,7 @@ class GridFieldVersionedState implements GridField_ColumnProvider
*/ */
public function getColumnsHandled($gridField) public function getColumnsHandled($gridField)
{ {
return [$this->column]; return $this->column ? [$this->column] : [];
} }
/** /**
@ -74,7 +86,6 @@ class GridFieldVersionedState implements GridField_ColumnProvider
*/ */
public function getColumnContent($gridField, $record, $columnName) public function getColumnContent($gridField, $record, $columnName)
{ {
$flagContent = ''; $flagContent = '';
$flags = $this->getStatusFlags($record); $flags = $this->getStatusFlags($record);
foreach ($flags as $class => $data) { foreach ($flags as $class => $data) {
@ -140,13 +151,16 @@ class GridFieldVersionedState implements GridField_ColumnProvider
* ) * )
* ``` * ```
* *
* @param DataObject $record - the record to check status for * @param Versioned|DataObject $record - the record to check status for
* @return array * @return array
*/ */
protected function getStatusFlags($record) protected function getStatusFlags($record)
{ {
$flags = array(); if (!$record->hasExtension(Versioned::class)) {
return [];
}
$flags = [];
if ($record->isOnLiveOnly()) { if ($record->isOnLiveOnly()) {
$flags['removedfromdraft'] = array( $flags['removedfromdraft'] = array(
'text' => _t(__CLASS__ . '.ONLIVEONLYSHORT', 'On live only'), 'text' => _t(__CLASS__ . '.ONLIVEONLYSHORT', 'On live only'),