BUG Make GridFieldConfig less susceptible to error when versioned isn't installed

This commit is contained in:
Damian Mooyman 2018-01-18 16:43:51 +13:00
parent 24e6794352
commit 16ad7e8fea
No known key found for this signature in database
GPG Key ID: 78B823A10DE27D1A
2 changed files with 25 additions and 6 deletions

View File

@ -1,6 +1,8 @@
<?php
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
* the GridField.
@ -21,7 +23,10 @@ class GridFieldConfig_RecordEditor extends GridFieldConfig
$this->addComponent($sort = new GridFieldSortableHeader());
$this->addComponent($filter = new GridFieldFilterHeader());
$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 GridFieldDeleteAction());
$this->addComponent(new GridFieldPageCount('toolbar-header-right'));

View File

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