[IMPR] SEO and UX Friendly page title and nav label

This commit is contained in:
Tony Air 2023-06-20 18:51:04 +02:00
parent a1c007cf3f
commit f1dd6da7d4

View File

@ -14,6 +14,8 @@ use SilverStripe\Forms\TreeDropdownField;
use SilverStripe\ORM\DataExtension;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\LiteralField;
use DNADesign\Elemental\Models\ElementalArea;
use SilverStripe\ORM\DataObject;
/**
* Class \A2nt\ElementalBasics\Extensions\BaseElementEx
@ -22,8 +24,6 @@ use SilverStripe\Forms\LiteralField;
*/
class BaseElementEx extends DataExtension
{
public function updateCMSFields(FieldList $fields)
{
$obj = $this->owner;
@ -38,7 +38,7 @@ class BaseElementEx extends DataExtension
.'</div>'
));
if($this->owner->ID) {
if ($this->owner->ID) {
$tab->push(TreeDropdownField::create(
'MoveElementIDToPage',
'Move an element to page',
@ -85,10 +85,25 @@ class BaseElementEx extends DataExtension
public function onBeforeWrite()
{
parent::onBeforeWrite();
$el = $this->owner;
$moveID = $this->owner->getField('MoveElementIDToPage');
if($this->owner->ID && $moveID) {
self::MoveElementToPage($this->owner->ID, $moveID);
// clone page title to first element title
$class = DataObject::getSchema()->hasOneComponent($el, 'Parent');
$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);
}
}
}