Skip to content
This repository has been archived by the owner on Feb 21, 2023. It is now read-only.

Commit

Permalink
BUG: GridField buttons UI improvements for versioned models.
Browse files Browse the repository at this point in the history
  • Loading branch information
mfendeksilverstripe committed Apr 29, 2021
1 parent 5efbbd3 commit 1666329
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 67 deletions.
9 changes: 9 additions & 0 deletions src/Extension/FluentExtension.php
Expand Up @@ -188,6 +188,15 @@ class FluentExtension extends DataExtension
*/
private static $localised_copy = [];

/**
* Enable localise actions (copy to draft, copy & publish and Localise actions)
* these actions can be used to localise page content directly via main page actions
*
* @config
* @var bool
*/
private static $localise_actions_enabled = true;

/**
* Cache of localised fields for this model
*/
Expand Down
58 changes: 1 addition & 57 deletions src/Extension/FluentSiteTreeExtension.php
Expand Up @@ -13,11 +13,8 @@
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\LiteralField;
use SilverStripe\Forms\Tab;
use SilverStripe\ORM\FieldType\DBField;
use SilverStripe\ORM\FieldType\DBHTMLText;
use SilverStripe\Versioned\Versioned;
use SilverStripe\View\SSViewer;
use TractorCow\Fluent\Extension\Traits\FluentAdminTrait;
use TractorCow\Fluent\Model\Locale;
use TractorCow\Fluent\State\FluentState;
Expand All @@ -44,15 +41,6 @@ class FluentSiteTreeExtension extends FluentVersionedExtension
*/
private static $locale_published_status_message = true;

/**
* Enable localise actions (copy to draft and copy & publish actions)
* these actions can be used to localise page content directly via main page actions
*
* @config
* @var bool
*/
private static $localise_actions_enabled = true;

/**
* Add alternate links to metatags
*
Expand Down Expand Up @@ -218,7 +206,6 @@ public function updateCMSActions(FieldList $actions)
}

$this->updateSavePublishActions($actions);
$this->updateInformationPanel($actions);
$this->updateRestoreAction($actions);
$this->updateFluentActions($actions, $this->owner);
}
Expand Down Expand Up @@ -451,49 +438,6 @@ protected function updateRestoreAction(FieldList $actions): void
$actions->removeByName('action_restore');
}

/**
* Information panel shows published state of a base record by default
* this overrides the display with the published state of the localised record
*
* @param FieldList $actions
*/
protected function updateInformationPanel(FieldList $actions): void
{
$owner = $this->owner;

/** @var Tab $moreOptions */
$moreOptions = $actions->fieldByName('ActionMenus.MoreOptions');

if (!$moreOptions) {
return;
}

/** @var LiteralField $information */
$information = $moreOptions->fieldByName('Information');

if (!$information) {
return;
}

$liveRecord = Versioned::withVersionedMode(function () use ($owner) {
Versioned::set_stage(Versioned::LIVE);

return SiteTree::get()->byID($owner->ID);
});

$infoTemplate = SSViewer::get_templates_by_class(
$owner->ClassName,
'_Information',
SiteTree::class
);

// show published info of localised record, not base record (this is framework's default)
$information->setValue($owner->customise([
'Live' => $liveRecord,
'ExistsOnLive' => $owner->isPublishedInLocale(),
])->renderWith($infoTemplate));
}

/**
* Update modified flag to reflect localised record instead of base record
* It doesn't make sense to have modified flag if page is not localised in current locale
Expand Down Expand Up @@ -620,7 +564,7 @@ public function updateLocalisationTabColumns(&$summaryColumns)
'title' => 'Title',
'callback' => function (Locale $object) use ($url, $params) {
if (!$object->RecordLocale()) {
return;
return null;
}

$recordLocale = $object->RecordLocale();
Expand Down
79 changes: 69 additions & 10 deletions src/Extension/Traits/FluentAdminTrait.php
Expand Up @@ -2,20 +2,24 @@

namespace TractorCow\Fluent\Extension\Traits;

use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Control\HTTPResponse_Exception;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\LiteralField;
use SilverStripe\Forms\Tab;
use SilverStripe\Forms\TabSet;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\FieldType\DBHTMLText;
use SilverStripe\ORM\ValidationException;
use SilverStripe\Security\Permission;
use SilverStripe\Versioned\Versioned;
use SilverStripe\View\SSViewer;
use TractorCow\Fluent\Extension\FluentExtension;
use TractorCow\Fluent\Extension\FluentFilteredExtension;
use TractorCow\Fluent\Extension\FluentVersionedExtension;
use TractorCow\Fluent\Model\Delete\ArchiveRecordPolicy;
use TractorCow\Fluent\Model\Delete\DeleteFilterPolicy;
use TractorCow\Fluent\Model\Delete\DeleteLocalisationPolicy;
Expand Down Expand Up @@ -63,12 +67,19 @@ protected function updateFluentActions(
// Flush data before checking actions
$record->flushCache(true);

// These actions need to be executed before we bail out on archived check
// as the UI needs to be updated even if such case
$this->updateSaveAction($actions, $record);
$this->updateDeleteAction($actions, $record);
$this->updateInformationPanel($actions, $record);

// Skip if record is archived
$results = $record->invokeWithExtensions('isArchived');
$results = array_filter($results, function ($v) {
return !is_null($v);
});
$isArchived = $results ? min($results) : false;

if ($isArchived) {
return;
}
Expand All @@ -80,9 +91,6 @@ protected function updateFluentActions(
return;
}

$this->updateSaveAction($actions, $record);
$this->updateDeleteAction($actions, $record);

if (!$record->config()->get('batch_actions_enabled')) {
return;
}
Expand Down Expand Up @@ -589,12 +597,15 @@ protected function inEveryStage($doSomething)
*/
protected function updateSaveAction(FieldList $actions, DataObject $record): void
{
if ($record->hasExtension(Versioned::class)) {
if ($record->existsInLocale()) {
// keep the action unchanged as the record is localised
return;
}

if ($record->existsInLocale()) {
// keep the action unchanged as the record is localised
if (!$record->config()->get('localise_actions_enabled')) {
// Save action UI is disabled
$actions->removeByName('action_doSave');

return;
}

Expand Down Expand Up @@ -623,10 +634,6 @@ protected function updateSaveAction(FieldList $actions, DataObject $record): voi
*/
protected function updateDeleteAction(FieldList $actions, DataObject $record): void
{
if ($record->hasExtension(Versioned::class)) {
return;
}

if (!$record->existsInLocale()) {
// record is not localised - remove the action
$actions->removeByName('action_doDelete');
Expand Down Expand Up @@ -663,4 +670,56 @@ protected function updateDeleteAction(FieldList $actions, DataObject $record): v
)
);
}

/**
* Information panel shows published state of a base record by default
* this overrides the display with the published state of the localised record
*
* @param FieldList $actions
* @param DataObject|FluentVersionedExtension $record
*/
protected function updateInformationPanel(FieldList $actions, DataObject $record): void
{
if (!$record->hasExtension(Versioned::class)) {
// This is only relevant for versioned records (base)
return;
}

if (!$record->hasExtension(FluentVersionedExtension::class)) {
// This is only relevant for versioned records (fluent)
return;
}

/** @var Tab $moreOptions */
$moreOptions = $actions->fieldByName('ActionMenus.MoreOptions');

if (!$moreOptions) {
return;
}

/** @var LiteralField $information */
$information = $moreOptions->fieldByName('Information');

if (!$information) {
return;
}

$liveRecord = Versioned::withVersionedMode(function () use ($record) {
Versioned::set_stage(Versioned::LIVE);

return DataObject::get_by_id($record->ClassName, $record->ID);
});

$infoTemplate = SSViewer::get_templates_by_class(
$record->ClassName,
'_Information',
$record instanceof SiteTree ? SiteTree::class : DataObject::class
);

// show published info of localised record, not base record (this is framework's default)
$information->setValue($record->customise([
'Live' => $liveRecord,
'ExistsOnLive' => $record->isPublishedInLocale(),
])->renderWith($infoTemplate));
}
}

0 comments on commit 1666329

Please sign in to comment.