diff --git a/src/Extension/FluentExtension.php b/src/Extension/FluentExtension.php index 1c02164d..f1be4143 100644 --- a/src/Extension/FluentExtension.php +++ b/src/Extension/FluentExtension.php @@ -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 */ diff --git a/src/Extension/FluentSiteTreeExtension.php b/src/Extension/FluentSiteTreeExtension.php index 9a208bed..42bb4936 100644 --- a/src/Extension/FluentSiteTreeExtension.php +++ b/src/Extension/FluentSiteTreeExtension.php @@ -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; @@ -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 * @@ -218,7 +206,6 @@ public function updateCMSActions(FieldList $actions) } $this->updateSavePublishActions($actions); - $this->updateInformationPanel($actions); $this->updateRestoreAction($actions); $this->updateFluentActions($actions, $this->owner); } @@ -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 @@ -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(); diff --git a/src/Extension/Traits/FluentAdminTrait.php b/src/Extension/Traits/FluentAdminTrait.php index 31880776..e433458c 100644 --- a/src/Extension/Traits/FluentAdminTrait.php +++ b/src/Extension/Traits/FluentAdminTrait.php @@ -2,11 +2,13 @@ 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; @@ -14,8 +16,10 @@ 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; @@ -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; } @@ -80,9 +91,6 @@ protected function updateFluentActions( return; } - $this->updateSaveAction($actions, $record); - $this->updateDeleteAction($actions, $record); - if (!$record->config()->get('batch_actions_enabled')) { return; } @@ -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; } @@ -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'); @@ -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)); + } }