From ae930833ade64e2634d1c6871fbe356b151294f3 Mon Sep 17 00:00:00 2001 From: Saophalkun Ponlu Date: Tue, 3 Oct 2017 16:07:38 +1300 Subject: [PATCH 1/5] Add gridfield versioned columns --- .../GridFieldConfig_RecordEditor.php | 1 + .../GridField/GridFieldVersionedState.php | 111 ++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 src/Forms/GridField/GridFieldVersionedState.php diff --git a/src/Forms/GridField/GridFieldConfig_RecordEditor.php b/src/Forms/GridField/GridFieldConfig_RecordEditor.php index c67d116bb..5b31ddbef 100644 --- a/src/Forms/GridField/GridFieldConfig_RecordEditor.php +++ b/src/Forms/GridField/GridFieldConfig_RecordEditor.php @@ -21,6 +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 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 new file mode 100644 index 000000000..f05ebc9b2 --- /dev/null +++ b/src/Forms/GridField/GridFieldVersionedState.php @@ -0,0 +1,111 @@ +getDisplayFields()} + * @see {@link GridFieldDataColumns}. + * + * @param GridField $gridField + * @param array $columns List reference of all column names. + */ + public function augmentColumns($gridField, &$columns) + { + $model = $gridField->getModelClass(); + $isModelVersioned = $model::has_extension(Versioned::class); + + if(!$isModelVersioned) { + return; + } + + $matchedVersionedFields = array_intersect( + $columns, + $this->versionedLabelFields + ); + + if (count($matchedVersionedFields) > 0) { + $this->column = array_values($matchedVersionedFields)[0]; + } + // Use first column + else if ($columns) { + $this->column = $columns[0]; + } + } + + /** + * Names of all columns which are affected by this component. + * + * @param GridField $gridField + * @return array + */ + public function getColumnsHandled($gridField) + { + return [$this->column]; + } + + /** + * HTML for the column, content of the element. + * + * @param GridField $gridField + * @param DataObject $record - Record displayed in this row + * @param string $columnName + * @return string - HTML for the column. Return NULL to skip. + */ + public function getColumnContent($gridField, $record, $columnName) + { + + $flagContent = ''; + $flags = $record->getStatusFlags(); + foreach ($flags as $class => $data) { + if (is_string($data)) { + $data = array('text' => $data); + } + $flagContent .= sprintf( + "%s", + 'status-' . Convert::raw2xml($class), + (isset($data['title'])) ? sprintf(' title=\\"%s\\"', Convert::raw2xml($data['title'])) : '', + Convert::raw2xml($data['text']) + ); + } + return $flagContent; + } + + /** + * Attributes for the element containing the content returned by {@link getColumnContent()}. + * + * @param GridField $gridField + * @param DataObject $record displayed in this row + * @param string $columnName + * @return array + */ + public function getColumnAttributes($gridField, $record, $columnName) + { + return [ 'data-contains-version-state' => true ]; + } + + /** + * Additional metadata about the column which can be used by other components, + * e.g. to set a title for a search column header. + * + * @param GridField $gridField + * @param string $columnName + * @return array - Map of arbitrary metadata identifiers to their values. + */ + public function getColumnMetadata($gridField, $columnName) + { + return []; + } +} From 6424f4dea07049d5e02ce9facb94e04dc175cac9 Mon Sep 17 00:00:00 2001 From: Saophalkun Ponlu Date: Thu, 5 Oct 2017 13:45:00 +1300 Subject: [PATCH 2/5] 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; + } } From e07658ef50d737f60c3234222d17fe1a9a46659c Mon Sep 17 00:00:00 2001 From: Christopher Joe Date: Thu, 5 Oct 2017 15:28:58 +1300 Subject: [PATCH 3/5] Fix linting issues and fix doc --- .../GridField/GridFieldVersionedState.php | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/Forms/GridField/GridFieldVersionedState.php b/src/Forms/GridField/GridFieldVersionedState.php index 6801a8bd7..d36a1e0d1 100644 --- a/src/Forms/GridField/GridFieldVersionedState.php +++ b/src/Forms/GridField/GridFieldVersionedState.php @@ -1,8 +1,6 @@ versionedLabelFields = $versionedLabelFields; } } @@ -37,7 +36,7 @@ class GridFieldVersionedState implements GridField_ColumnProvider $model = $gridField->getModelClass(); $isModelVersioned = $model::has_extension(Versioned::class); - if(!$isModelVersioned) { + if (!$isModelVersioned) { return; } @@ -48,9 +47,8 @@ class GridFieldVersionedState implements GridField_ColumnProvider if (count($matchedVersionedFields) > 0) { $this->column = array_values($matchedVersionedFields)[0]; - } - // Use first column - else if ($columns) { + } elseif ($columns) { + // Use first column $this->column = $columns[0]; } } @@ -142,11 +140,11 @@ class GridFieldVersionedState implements GridField_ColumnProvider * ) * ``` * - * @param bool $cached Whether to serve the fields from cache; false - * regenerate them + * @param DataObject $record - the record to check status for * @return array */ - protected function getStatusFlags($record) { + protected function getStatusFlags($record) + { $flags = array(); if ($record->isOnLiveOnly()) { From 1a324d9d774bb8931925b6622c2ad9fd055fbb59 Mon Sep 17 00:00:00 2001 From: Saophalkun Ponlu Date: Thu, 5 Oct 2017 15:54:11 +1300 Subject: [PATCH 4/5] Wrap content gridfield cell in another non-td element for styling --- src/Forms/GridField/GridFieldVersionedState.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Forms/GridField/GridFieldVersionedState.php b/src/Forms/GridField/GridFieldVersionedState.php index d36a1e0d1..d668cf9b5 100644 --- a/src/Forms/GridField/GridFieldVersionedState.php +++ b/src/Forms/GridField/GridFieldVersionedState.php @@ -82,7 +82,7 @@ class GridFieldVersionedState implements GridField_ColumnProvider $data = array('text' => $data); } $flagContent .= sprintf( - "%s", + " %s", 'status-' . Convert::raw2xml($class), (isset($data['title'])) ? sprintf(' title="%s"', Convert::raw2xml($data['title'])) : '', Convert::raw2xml($data['text']) @@ -101,7 +101,7 @@ class GridFieldVersionedState implements GridField_ColumnProvider */ public function getColumnAttributes($gridField, $record, $columnName) { - return [ 'data-contains-version-state' => true ]; + return []; } /** From 578f3f208c82fd9789990fe2a986d40ea474f49c Mon Sep 17 00:00:00 2001 From: Christopher Joe Date: Fri, 6 Oct 2017 13:40:11 +1300 Subject: [PATCH 5/5] Fix behat test --- tests/behat/features/profile.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/behat/features/profile.feature b/tests/behat/features/profile.feature index 8c21656ec..af1536728 100644 --- a/tests/behat/features/profile.feature +++ b/tests/behat/features/profile.feature @@ -18,7 +18,7 @@ Feature: Manage my own settings Given I go to "admin/myprofile" Then I should not see "Joe" Then I should see "Jack" - And I should see "Johnson" + And I should not see "Johnson" Scenario: I can't reset the password without the original Given I follow "Change Password"