mirror of
https://github.com/a2nt/silverstripe-elemental-basics.git
synced 2024-10-22 17:05:54 +02:00
[IMPR] SEO and UX Friendly page title and nav label
This commit is contained in:
parent
a1c007cf3f
commit
f1dd6da7d4
@ -14,6 +14,8 @@ use SilverStripe\Forms\TreeDropdownField;
|
|||||||
use SilverStripe\ORM\DataExtension;
|
use SilverStripe\ORM\DataExtension;
|
||||||
use SilverStripe\Forms\FieldList;
|
use SilverStripe\Forms\FieldList;
|
||||||
use SilverStripe\Forms\LiteralField;
|
use SilverStripe\Forms\LiteralField;
|
||||||
|
use DNADesign\Elemental\Models\ElementalArea;
|
||||||
|
use SilverStripe\ORM\DataObject;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class \A2nt\ElementalBasics\Extensions\BaseElementEx
|
* Class \A2nt\ElementalBasics\Extensions\BaseElementEx
|
||||||
@ -22,8 +24,6 @@ use SilverStripe\Forms\LiteralField;
|
|||||||
*/
|
*/
|
||||||
class BaseElementEx extends DataExtension
|
class BaseElementEx extends DataExtension
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
public function updateCMSFields(FieldList $fields)
|
public function updateCMSFields(FieldList $fields)
|
||||||
{
|
{
|
||||||
$obj = $this->owner;
|
$obj = $this->owner;
|
||||||
@ -38,18 +38,18 @@ class BaseElementEx extends DataExtension
|
|||||||
.'</div>'
|
.'</div>'
|
||||||
));
|
));
|
||||||
|
|
||||||
if($this->owner->ID) {
|
if ($this->owner->ID) {
|
||||||
$tab->push(TreeDropdownField::create(
|
$tab->push(TreeDropdownField::create(
|
||||||
'MoveElementIDToPage',
|
'MoveElementIDToPage',
|
||||||
'Move an element to page',
|
'Move an element to page',
|
||||||
SiteTree::class
|
SiteTree::class
|
||||||
)->setEmptyString('(select an element to move)'));
|
)->setEmptyString('(select an element to move)'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function MoveElementToPage($ID, $moveToID)
|
public static function MoveElementToPage($ID, $moveToID)
|
||||||
{
|
{
|
||||||
$el = BaseElement::get()->byID($ID);
|
$el = BaseElement::get()->byID($ID);
|
||||||
$page = SiteTree::get()->byID($moveToID);
|
$page = SiteTree::get()->byID($moveToID);
|
||||||
if (!$page || !$el) {
|
if (!$page || !$el) {
|
||||||
return false;
|
return false;
|
||||||
@ -62,33 +62,48 @@ class BaseElementEx extends DataExtension
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function updateCMSEditLink(&$link): void
|
public function updateCMSEditLink(&$link): void
|
||||||
{
|
{
|
||||||
if (!$this->owner->inlineEditable()) {
|
if (!$this->owner->inlineEditable()) {
|
||||||
$page = $this->owner->getPage();
|
$page = $this->owner->getPage();
|
||||||
|
|
||||||
if (!$page || $page instanceof SiteTree) {
|
if (!$page || $page instanceof SiteTree) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// As non-page DataObject's are managed via GridFields, we have to grab their CMS edit URL
|
// As non-page DataObject's are managed via GridFields, we have to grab their CMS edit URL
|
||||||
// and replace the trailing /edit/ with a link to the nested ElementalArea edit form
|
// and replace the trailing /edit/ with a link to the nested ElementalArea edit form
|
||||||
$relationName = $this->owner->getAreaRelationName();
|
$relationName = $this->owner->getAreaRelationName();
|
||||||
$link = preg_replace(
|
$link = preg_replace(
|
||||||
'/edit\/?$/',
|
'/edit\/?$/',
|
||||||
"ItemEditForm/field/{$relationName}/item/{$this->owner->ID}/edit/",
|
"ItemEditForm/field/{$relationName}/item/{$this->owner->ID}/edit/",
|
||||||
$page->CMSEditLink()
|
$page->CMSEditLink()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function onBeforeWrite()
|
public function onBeforeWrite()
|
||||||
{
|
{
|
||||||
parent::onBeforeWrite();
|
parent::onBeforeWrite();
|
||||||
|
$el = $this->owner;
|
||||||
|
|
||||||
$moveID = $this->owner->getField('MoveElementIDToPage');
|
// clone page title to first element title
|
||||||
if($this->owner->ID && $moveID) {
|
$class = DataObject::getSchema()->hasOneComponent($el, 'Parent');
|
||||||
self::MoveElementToPage($this->owner->ID, $moveID);
|
$area = ($el->ParentID) ? DataObject::get_by_id($class, $el->ParentID) : null;
|
||||||
}
|
if ($area instanceof ElementalArea && $area->exists()) {
|
||||||
|
if ($area->Elements()->count() === 0 && !$el->getField('Title') && $el->hasMethod('getPage')) {
|
||||||
|
$page = $el->getPage();
|
||||||
|
if ($page) {
|
||||||
|
$el->setField('Title', $page->getField('Title'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
$moveID = $el->getField('MoveElementIDToPage');
|
||||||
|
if ($el->ID && $moveID) {
|
||||||
|
self::MoveElementToPage($el->ID, $moveID);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user