-
Notifications
You must be signed in to change notification settings - Fork 822
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
ENH UI updated for versioned objects and all its relations #11101
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
<?php | ||
|
||
namespace SilverStripe\Forms\GridField; | ||
|
||
use SilverStripe\Forms\GridField\GridField; | ||
use SilverStripe\Forms\GridField\GridField_ColumnProvider; | ||
use SilverStripe\ORM\DataObject; | ||
use SilverStripe\Versioned\Versioned; | ||
use SilverStripe\Core\Convert; | ||
use SilverStripe\Core\Injector\Injector; | ||
use SilverStripe\Versioned\RecursiveStagesInterface; | ||
use SilverStripe\Versioned\VersionedGridFieldState\VersionedGridFieldState; | ||
use SilverStripe\View\HTML; | ||
|
||
class GridFieldVersionTag extends AbstractGridFieldComponent implements GridField_ColumnProvider | ||
{ | ||
protected ?string $column = null; | ||
|
||
protected array $versionedLabelFields = []; | ||
|
||
public function __construct($versionedLabelFields = ['Name', 'Title']) | ||
{ | ||
$this->setVersionedLabelFields($versionedLabelFields); | ||
} | ||
|
||
public function getColumn(): ?string | ||
{ | ||
return $this->column; | ||
} | ||
|
||
public function setColumn(string $column): static | ||
{ | ||
$this->column = $column; | ||
return $this; | ||
} | ||
|
||
public function getVersionedLabelFields(): array | ||
{ | ||
return $this->versionedLabelFields; | ||
} | ||
|
||
public function setVersionedLabelFields(array $versionedLabelFields): static | ||
{ | ||
$this->versionedLabelFields = $versionedLabelFields; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Modify the list of columns displayed in the table. | ||
* | ||
* @see {@link GridFieldDataColumns->getDisplayFields()} | ||
* @see {@link GridFieldDataColumns}. | ||
* | ||
* @param GridField $gridField | ||
* @param array $columns List reference of all column names. | ||
*/ | ||
public function augmentColumns($gridField, &$columns): void | ||
{ | ||
// Skip if not versioned, or column already set | ||
if ($this->getColumn()) { | ||
return; | ||
} | ||
|
||
$matchedVersionedFields = array_intersect( | ||
$columns ?? [], | ||
$this->versionedLabelFields | ||
); | ||
|
||
if (count($matchedVersionedFields ?? []) > 0) { | ||
// Get first matched column | ||
$this->setColumn(reset($matchedVersionedFields)); | ||
} elseif ($columns) { | ||
// Use first column if none of preferred matches | ||
$this->setColumn(reset($columns)); | ||
} | ||
} | ||
|
||
/** | ||
* Names of all columns which are affected by this component. | ||
* | ||
* @param GridField $gridField | ||
* @return array | ||
*/ | ||
public function getColumnsHandled($gridField): array | ||
{ | ||
return $this->getColumn() ? [$this->getColumn()] : []; | ||
} | ||
|
||
/** | ||
* HTML for the column, content of the <td> element. | ||
* | ||
* @param GridField $gridField | ||
* @param DataObject $record Record displayed in this row | ||
* @param string $columnName | ||
* @return string HTML for the column. | ||
*/ | ||
public function getColumnContent($gridField, $record, $columnName): string | ||
{ | ||
$flagContent = ''; | ||
$flags = $this->getStatusFlags($record); | ||
foreach ($flags as $class => $data) { | ||
$flagAttributes = [ | ||
'class' => "ss-gridfield-badge badge status-{$class}", | ||
]; | ||
if (isset($data['title'])) { | ||
$flagAttributes['title'] = $data['title']; | ||
} | ||
$flagContent .= ' ' . HTML::createTag('span', $flagAttributes, Convert::raw2xml($data['text'])); | ||
} | ||
return $flagContent; | ||
} | ||
|
||
/** | ||
* Attributes for the column | ||
*/ | ||
public function getColumnAttributes($gridField, $record, $columnName): array | ||
{ | ||
return []; | ||
} | ||
|
||
/** | ||
* Metadata for the column | ||
*/ | ||
public function getColumnMetadata($gridField, $columnName): array | ||
{ | ||
return []; | ||
} | ||
|
||
/** | ||
* Get status flags for a given record | ||
*/ | ||
private function getStatusFlags(DataObject $record): array | ||
{ | ||
if ($record->hasExtension(Versioned::class)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This check that this class does not have the Versioned extension is needed in order, firstly, not to have a duplicate badge and, secondly, not to override the badge of the current state of the current object. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So there is already a versioned badge, and this PR is adding a new one? Is the original status badge wrong? Where is the code for the original status badge. Would it make sense to move this code there, or at least deprecate the old code since it's presumably wrong? |
||
return []; | ||
} | ||
|
||
if ($this->stagesDifferRecursive($record)) { | ||
return [ | ||
'modified' => [ | ||
'text' => _t(__CLASS__ . '.MODIFIEDONDRAFTSHORT', 'Modified'), | ||
'title' => _t(__CLASS__ . '.MODIFIEDONDRAFTHELP', 'Item has unpublished changes'), | ||
] | ||
]; | ||
} | ||
|
||
return []; | ||
} | ||
|
||
/** | ||
* Check if stages differ for a given record and all its relations | ||
*/ | ||
private function stagesDifferRecursive(DataObject $record): bool | ||
{ | ||
/** @var RecursiveStagesInterface $service */ | ||
$service = Injector::inst()->get(RecursiveStagesInterface::class); | ||
|
||
return $service->stagesDifferRecursive($record); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
SilverStripe\Forms\Tests\GridField\GridFieldTest\Team: | ||
team1: | ||
Name: Team 1 | ||
City: Cologne | ||
SilverStripe\Forms\Tests\GridField\GridFieldTest\Cheerleader: | ||
cheerleader1_team1: | ||
Name: Heather | ||
Team: =>SilverStripe\Forms\Tests\GridField\GridFieldTest\Team.team1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check is needed to highlight “Save” if child versioned objects have changes