Skip to content

Commit

Permalink
Merge pull request #838 from tractorcow-farm/bugfix/cms-links
Browse files Browse the repository at this point in the history
ENH: Localisation manager links available for CMS edit link capable models.
  • Loading branch information
GuySartorelli authored Apr 8, 2024
2 parents 7bf314a + 07f2821 commit 55c3681
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 59 deletions.
58 changes: 0 additions & 58 deletions src/Extension/FluentSiteTreeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@
use SilverStripe\Control\Controller;
use SilverStripe\Control\Director;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Core\Convert;
use SilverStripe\Forms\CompositeField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\LiteralField;
use SilverStripe\ORM\FieldType\DBField;
use SilverStripe\ORM\FieldType\DBHTMLText;
use TractorCow\Fluent\Extension\Traits\FluentAdminTrait;
use TractorCow\Fluent\Model\Locale;
Expand Down Expand Up @@ -528,60 +526,4 @@ public function actionComplete($form, $message)
{
return null;
}

/**
* Augment Localisation tab with clickable locale links to allow easy navigation between page localisations
*
* @param $summaryColumns
* @see FluentExtension::updateFluentCMSFields()
*/
public function updateLocalisationTabColumns(&$summaryColumns)
{
parent::updateLocalisationTabColumns($summaryColumns);

if (!array_key_exists('Title', $summaryColumns)) {
return;
}

$controller = Controller::curr();

if (!$controller) {
return;
}

$request = $controller->getRequest();

if (!$request) {
return;
}

// This is to get URL only, getVars are not part of the URL
$url = $this->owner->CMSEditLink();

if (!$url) {
return;
}

// Pass getVars separately so we can process them later
$params = $request->getVars();
$url = Director::makeRelative($url);

$summaryColumns['Title'] = [
'title' => 'Title',
'callback' => function (Locale $object) use ($url, $params) {
if (!$object->RecordLocale()) {
return null;
}

$recordLocale = $object->RecordLocale();
$locale = $recordLocale->getLocale();
$params['l'] = $locale;
$localeLink = Controller::join_links($url, '?' . http_build_query($params));
$localeTitle = Convert::raw2xml($recordLocale->getTitle());
$render = sprintf('<a href="%s" target="_top">%s</a>', $localeLink, $localeTitle);

return DBField::create_field('HTMLVarchar', $render);
}
];
}
}
40 changes: 39 additions & 1 deletion src/Extension/Traits/FluentObjectTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace TractorCow\Fluent\Extension\Traits;

use SilverStripe\Admin\CMSEditLinkExtension;
use SilverStripe\Control\Controller;
use SilverStripe\Control\Director;
use SilverStripe\Core\Convert;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldConfig;
Expand All @@ -10,6 +14,7 @@
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\DataQuery;
use SilverStripe\ORM\FieldType\DBField;
use SilverStripe\ORM\Queries\SQLSelect;
use TractorCow\Fluent\Model\Locale;
use TractorCow\Fluent\State\FluentState;
Expand Down Expand Up @@ -88,7 +93,7 @@ public function augmentDataQueryCreation(
*/
protected function updateFluentCMSFields(FieldList $fields)
{
/** @var DataObject $owner */
/** @var DataObject|CMSEditLinkExtension $owner */
$owner = $this->owner;
if (!$owner->ID) {
return;
Expand All @@ -108,6 +113,39 @@ protected function updateFluentCMSFields(FieldList $fields)
'Locale' => 'Locale'
];

// Augment Localisation tab with clickable locale links to allow easy navigation between model localisations
if ($owner->hasExtension(CMSEditLinkExtension::class)) {
$controller = Controller::has_curr() ? Controller::curr() : null;
$request = $controller?->getRequest();

// Pass getVars separately so we can process them later
$params = $request?->getVars() ?? [];

// This is to get URL only, getVars are not part of the URL
$url = $owner->CMSEditLink();
$url = Director::makeRelative($url);

if ($url) {
$summaryColumns['Title'] = [
'title' => 'Title',
'callback' => function (Locale $object) use ($url, $params): ?DBField {
if (!$object->RecordLocale()) {
return null;
}

$recordLocale = $object->RecordLocale();
$locale = $recordLocale->getLocale();
$params['l'] = $locale;
$localeLink = Controller::join_links($url, '?' . http_build_query($params));
$localeTitle = Convert::raw2xml($recordLocale->getTitle());
$render = sprintf('<a href="%s" target="_top">%s</a>', $localeLink, $localeTitle);

return DBField::create_field('HTMLVarchar', $render);
}
];
}
}

// Let extensions override columns
$owner->extend('updateLocalisationTabColumns', $summaryColumns);
$columns->setDisplayFields($summaryColumns);
Expand Down

0 comments on commit 55c3681

Please sign in to comment.