Skip to content
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 Update status flags generally, not just for SiteTree #909

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions src/Extension/FluentExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use SilverStripe\Security\Permission;
use SilverStripe\Versioned\Versioned;
use SilverStripe\View\HTML;
use TractorCow\Fluent\Extension\Traits\FluentBadgeTrait;
use TractorCow\Fluent\Extension\Traits\FluentObjectTrait;
use TractorCow\Fluent\Forms\CopyLocaleAction;
use TractorCow\Fluent\Forms\GroupActionMenu;
Expand Down Expand Up @@ -1286,6 +1287,114 @@ protected function findRecordInLocale($locale, $table, $id)
return $query->firstRow()->execute()->value() !== null;
}

/**
* Remove fluent status flags from site tree display
*/
protected function updateStatusFlagsForTreeTitle(array &$flags): void
{
foreach (array_keys($flags) as $key) {
if ($key === 'fluent' || str_starts_with($key, 'fluent ')) {
unset($flags[$key]);
}
}
}

/**
* Update status flags based on whether the current record is exists in the current locale.
*/
protected function updateStatusFlags(array &$flags): void
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See FluentSiteTreeExtension for where this code originated.

{
// If there is no current FluentState, then we shouldn't update.
if (!FluentState::singleton()->getLocale()) {
return;
}
$this->addLocaleFlags($flags);
$this->updateNoSourceFlag($flags);
}

/**
* Add a flag based on the record's status in the current locale.
*/
private function addLocaleFlags(array &$flags): void
{
$locale = Locale::getCurrentLocale();
$record = $this->getOwner();
$info = RecordLocale::create($record, $locale);

// Build new badge
if ($info->IsDraft()) {
// If the object has been localised in the current locale, show a "localised" state
$flags['fluent fluent-badge fluent-badge--default'] = [
'title' => _t(
FluentBadgeTrait::class . '.BadgeLocalised',
'Localised in {locale}',
[
'locale' => $locale->getTitle()
]
),
'text' => $locale->getLocale(),
];
} elseif ($info->getSourceLocale()) {
// If object is inheriting content from another locale show the source
$flags['fluent fluent-badge fluent-badge--localised'] = [
'title' => _t(
FluentBadgeTrait::class . '.BadgeLocalised',
'Localised in {locale}',
[
'locale' => $info->getSourceLocale()->getTitle()
]
),
'text' => $info->getSourceLocale()->getLocale(),
];
} else {
// Otherwise the object is missing a content source and needs to be remedied
// by either localising or seting up a locale fallback
$flags['fluent fluent-badge fluent-badge--invisible'] = [
'title' => _t(
FluentBadgeTrait::class . '.BaggeInvisible',
'{type} has no available content in {locale}, localise the {type} or provide a locale fallback',
[
'type' => $record->i18n_singular_name(),
'locale' => $locale->getTitle(),
]
),
'text' => $locale->getLocale(),
];
}
}

/**
* Add a flag which indicates that a record has content in other locale but the content is not being inherited
*/
protected function updateNoSourceFlag(array &$flags): void
{
if (array_key_exists('archived', $flags)) {
return;
}

$locale = FluentState::singleton()->getLocale();

if (!$locale) {
return;
}

$owner = $this->getOwner();
$info = $owner->LocaleInformation($locale);

if ($info->getSourceLocale()) {
return;
}

if (!$owner->getLocaleInstances()) {
return;
}

$flags['removedfromdraft'] = [
'text' => 'No source',
'title' => 'This page exists in a different locale but the content is not inherited',
];
}

/**
* @param $summaryColumns
* @see FluentObjectTrait::updateFluentCMSFields()
Expand Down
15 changes: 0 additions & 15 deletions src/Extension/FluentGridFieldExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\GridField\GridFieldDetailForm_ItemRequest;
use SilverStripe\ORM\FieldType\DBField;
use SilverStripe\ORM\FieldType\DBHTMLText;
use SilverStripe\Core\Validation\ValidationResult;
use SilverStripe\Versioned\VersionedGridFieldItemRequest;
use TractorCow\Fluent\Extension\Traits\FluentAdminTrait;
use TractorCow\Fluent\Extension\Traits\FluentBadgeTrait;

/**
* Supports GridFieldDetailForm_ItemRequest with extra actions
Expand All @@ -23,19 +21,6 @@
class FluentGridFieldExtension extends Extension
{
use FluentAdminTrait;
use FluentBadgeTrait;

/**
* Push a badge to indicate the language that owns the current item
*
* @param DBField|null $badgeField
* @see VersionedGridFieldItemRequest::Breadcrumbs()
*/
protected function updateBadge(&$badgeField)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That hook doesn't exist anymore - this is now handled through generalised status flag rendering in core.

{
$record = $this->owner->getRecord();
$badgeField = $this->addFluentBadge($badgeField, $record);
}

protected function updateFormActions(FieldList $actions)
{
Expand Down
22 changes: 0 additions & 22 deletions src/Extension/FluentLeftAndMainExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,23 @@
use SilverStripe\Control\HTTPResponse_Exception;
use SilverStripe\Core\Extension;
use SilverStripe\Forms\Form;
use SilverStripe\Model\List\ArrayList;
use SilverStripe\ORM\FieldType\DBHTMLText;
use SilverStripe\View\Requirements;
use TractorCow\Fluent\Extension\Traits\FluentAdminTrait;
use TractorCow\Fluent\Extension\Traits\FluentBadgeTrait;

/**
* @extends Extension<LeftAndMain>
*/
class FluentLeftAndMainExtension extends Extension
{
use FluentAdminTrait;
use FluentBadgeTrait;

protected function onInit()
{
Requirements::javascript("tractorcow/silverstripe-fluent:client/dist/js/fluent.js");
Requirements::css("tractorcow/silverstripe-fluent:client/dist/styles/fluent.css");
}

/**
* @param ArrayList $breadcrumbs
* @see CMSMain::Breadcrumbs()
*/
protected function updateBreadcrumbs(ArrayList $breadcrumbs)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is now handled through generalised status flag rendering in core.

{
$record = $this->owner->currentPage();
if (!$record) {
return;
}

// Get a possibly existing badge field from the last item in the breadcrumbs list
$lastItem = $breadcrumbs->last();
$badgeField = $lastItem->hasField('Extra') ? $lastItem->getField('Extra') : null;
$newBadge = $this->addFluentBadge($badgeField, $record);

$lastItem->setField('Extra', $newBadge);
}

/**
* @param Form $form
* @param string $message
Expand Down
104 changes: 0 additions & 104 deletions src/Extension/FluentSiteTreeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,33 +158,6 @@ protected function updateLink(&$link, &$action, &$relativeLink)
$link = Controller::join_links($domain->Link(), $link);
}

/**
* Check whether the current page is exists in the current locale.
*
* If it is invisible then we add a class to show it slightly greyed out in the site tree.
*
* @param array $flags
*/
protected function updateStatusFlags(&$flags)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing here is specific to SiteTree anymore. This is now split up between FluentExtension and FluentVersionedExtension along with the methods called from here.

{
// If there is no current FluentState, then we shouldn't update.
if (!FluentState::singleton()->getLocale()) {
return;
}

$this->updateModifiedFlag($flags);
$this->updateArchivedFlag($flags);
$this->updateNoSourceFlag($flags);

// If this page does not exist it should be "invisible"
if (!$this->isDraftedInLocale() && !$this->isPublishedInLocale()) {
$flags['fluentinvisible'] = [
'text' => '',
'title' => '',
];
}
}

/**
* @param FieldList $fields
*/
Expand Down Expand Up @@ -443,83 +416,6 @@ protected function updateRestoreAction(FieldList $actions): void
$actions->removeByName('action_restore');
}

/**
* 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
*
* @param array $flags
*/
protected function updateModifiedFlag(array &$flags): void
{
if (!array_key_exists('modified', $flags)) {
return;
}

if ($this->owner->isDraftedInLocale()) {
return;
}

unset($flags['modified']);
}

/**
* Localise archived flag - remove archived flag if there is content on other locales
*
* @param array $flags
*/
protected function updateArchivedFlag(array &$flags): void
{
if (!array_key_exists('archived', $flags)) {
return;
}

$locale = FluentState::singleton()->getLocale();

if (!$locale) {
return;
}

if (!$this->owner->getLocaleInstances()) {
return;
}

unset($flags['archived']);
}

/**
* Add a flag which indicates that a page has content in other locale but the content is not being inherited
*
* @param array $flags
*/
protected function updateNoSourceFlag(array &$flags): void
{
if (array_key_exists('archived', $flags)) {
return;
}

$locale = FluentState::singleton()->getLocale();

if (!$locale) {
return;
}

$owner = $this->owner;
$info = $owner->LocaleInformation($locale);

if ($info->getSourceLocale()) {
return;
}

if (!$owner->getLocaleInstances()) {
return;
}

$flags['removedfromdraft'] = [
'text' => 'No source',
'title' => 'This page exists in a different locale but the content is not inherited',
];
}

/**
* @param Form $form
* @param string $message
Expand Down
70 changes: 70 additions & 0 deletions src/Extension/FluentVersionedExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,76 @@ public static function prepoulateIdsInLocale($locale, $dataObjectClass, $populat
}
}

/**
* Check whether the current record is exists in the current locale.
*
* If it is invisible then we add a class to show it slightly greyed out in the site tree.
*
* @param array $flags
*/
protected function updateStatusFlags(array &$flags): void
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See FluentSiteTreeExtension for where this code originated.

{
// If there is no current FluentState, then we shouldn't update.
if (!FluentState::singleton()->getLocale()) {
return;
}

$this->updateModifiedFlag($flags);
$this->updateArchivedFlag($flags);
parent::updateStatusFlags($flags);

// If this page does not exist it should be "invisible"
if (!$this->isDraftedInLocale() && !$this->isPublishedInLocale()) {
$flags['fluentinvisible'] = [
'text' => '',
'title' => '',
];
}
}

/**
* 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
*
* @param array $flags
*/
protected function updateModifiedFlag(array &$flags): void
{
if (!array_key_exists('modified', $flags)) {
return;
}

if ($this->owner->isDraftedInLocale()) {
return;
}

unset($flags['modified']);
}

/**
* Localise archived flag - remove archived flag if there is content on other locales
*
* @param array $flags
*/
protected function updateArchivedFlag(array &$flags): void
{
if (!array_key_exists('archived', $flags)) {
return;
}

$locale = FluentState::singleton()->getLocale();

if (!$locale) {
return;
}

if (!$this->owner->getLocaleInstances()) {
return;
}

unset($flags['archived']);
}

protected function updateLocalisationTabColumns(&$summaryColumns)
{
$summaryColumns['Status'] = [
Expand Down
Loading