From 6424f4dea07049d5e02ce9facb94e04dc175cac9 Mon Sep 17 00:00:00 2001 From: Saophalkun Ponlu Date: Thu, 5 Oct 2017 13:45:00 +1300 Subject: [PATCH] Changes based on peer review feedbacks --- .../GridFieldConfig_RecordEditor.php | 2 +- .../GridField/GridFieldVersionedState.php | 71 ++++++++++++++++++- 2 files changed, 69 insertions(+), 4 deletions(-) diff --git a/src/Forms/GridField/GridFieldConfig_RecordEditor.php b/src/Forms/GridField/GridFieldConfig_RecordEditor.php index 5b31ddbef..30fbd0f4b 100644 --- a/src/Forms/GridField/GridFieldConfig_RecordEditor.php +++ b/src/Forms/GridField/GridFieldConfig_RecordEditor.php @@ -21,7 +21,7 @@ class GridFieldConfig_RecordEditor extends GridFieldConfig $this->addComponent($sort = new GridFieldSortableHeader()); $this->addComponent($filter = new GridFieldFilterHeader()); $this->addComponent(new GridFieldDataColumns()); - $this->addComponent(new GridFieldVersionedState()); + $this->addComponent(new GridFieldVersionedState([ 'Name', 'Title' ])); $this->addComponent(new GridFieldEditButton()); $this->addComponent(new GridFieldDeleteAction()); $this->addComponent(new GridFieldPageCount('toolbar-header-right')); diff --git a/src/Forms/GridField/GridFieldVersionedState.php b/src/Forms/GridField/GridFieldVersionedState.php index f05ebc9b2..6801a8bd7 100644 --- a/src/Forms/GridField/GridFieldVersionedState.php +++ b/src/Forms/GridField/GridFieldVersionedState.php @@ -11,7 +11,17 @@ class GridFieldVersionedState implements GridField_ColumnProvider { protected $column = null; - protected $versionedLabelFields = ['Name', 'Title']; + /** + * Fields/columns to display version states. We can specifies more than one + * field but states only show in the first column found. + */ + protected $versionedLabelFields = ['Title']; + + public function __construct($versionedLabelFields = null) { + if($versionedLabelFields) { + $this->versionedLabelFields = $versionedLabelFields; + } + } /** * Modify the list of columns displayed in the table. @@ -68,7 +78,7 @@ class GridFieldVersionedState implements GridField_ColumnProvider { $flagContent = ''; - $flags = $record->getStatusFlags(); + $flags = $this->getStatusFlags($record); foreach ($flags as $class => $data) { if (is_string($data)) { $data = array('text' => $data); @@ -76,7 +86,7 @@ class GridFieldVersionedState implements GridField_ColumnProvider $flagContent .= sprintf( "%s", 'status-' . Convert::raw2xml($class), - (isset($data['title'])) ? sprintf(' title=\\"%s\\"', Convert::raw2xml($data['title'])) : '', + (isset($data['title'])) ? sprintf(' title="%s"', Convert::raw2xml($data['title'])) : '', Convert::raw2xml($data['text']) ); } @@ -108,4 +118,59 @@ class GridFieldVersionedState implements GridField_ColumnProvider { return []; } + + + /** + * A flag provides the user with additional data about the current item + * status, for example a "removed from draft" status. Each item can have + * more than one status flag. Returns a map of a unique key to a + * (localized) title for the flag. The unique key can be reused as a CSS + * class. + * + * Example (simple): + * + * ```php + * "deletedonlive" => "Deleted" + * ``` + * + * Example (with optional title attribute): + * + * ```php + * "deletedonlive" => array( + * 'text' => "Deleted", + * 'title' => 'This page has been deleted' + * ) + * ``` + * + * @param bool $cached Whether to serve the fields from cache; false + * regenerate them + * @return array + */ + protected function getStatusFlags($record) { + $flags = array(); + + if ($record->isOnLiveOnly()) { + $flags['removedfromdraft'] = array( + 'text' => _t(__CLASS__.'.ONLIVEONLYSHORT', 'On live only'), + 'title' => _t(__CLASS__.'.ONLIVEONLYSHORTHELP', 'Item is published, but has been deleted from draft'), + ); + } elseif ($record->isArchived()) { + $flags['archived'] = array( + 'text' => _t(__CLASS__.'.ARCHIVEDPAGESHORT', 'Archived'), + 'title' => _t(__CLASS__.'.ARCHIVEDPAGEHELP', 'Item is removed from draft and live'), + ); + } elseif ($record->isOnDraftOnly()) { + $flags['addedtodraft'] = array( + 'text' => _t(__CLASS__.'.ADDEDTODRAFTSHORT', 'Draft'), + 'title' => _t(__CLASS__.'.ADDEDTODRAFTHELP', "Item has not been published yet") + ); + } elseif ($record->isModifiedOnDraft()) { + $flags['modified'] = array( + 'text' => _t(__CLASS__.'.MODIFIEDONDRAFTSHORT', 'Modified'), + 'title' => _t(__CLASS__.'.MODIFIEDONDRAFTHELP', 'Item has unpublished changes'), + ); + } + + return $flags; + } }