2021-06-19 21:30:03 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace A2nt\CMSNiceties\Extensions;
|
|
|
|
|
2023-09-24 18:51:04 +02:00
|
|
|
use A2nt\ElementalBasics\Elements\SidebarElement;
|
2021-06-19 21:30:03 +02:00
|
|
|
use SilverStripe\Forms\TextareaField;
|
|
|
|
use SilverStripe\ORM\DataExtension;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
|
2022-05-10 13:09:06 +02:00
|
|
|
/**
|
|
|
|
* Class \A2nt\CMSNiceties\Extensions\SiteTreeExtension
|
|
|
|
*
|
|
|
|
* @property \A2nt\CMSNiceties\Extensions\SiteTreeExtension $owner
|
|
|
|
* @property string $ExtraCode
|
|
|
|
*/
|
2021-06-19 21:30:03 +02:00
|
|
|
class SiteTreeExtension extends DataExtension
|
|
|
|
{
|
|
|
|
private static $db = [
|
|
|
|
'ExtraCode' => 'Text',
|
|
|
|
];
|
|
|
|
|
|
|
|
public function updateSettingsFields(FieldList $fields)
|
|
|
|
{
|
|
|
|
$fields->addFieldsToTab('Root.Settings', [
|
|
|
|
TextareaField::create(
|
|
|
|
'ExtraCode',
|
|
|
|
'Extra page specific HTML code'
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
2023-06-20 18:50:49 +02:00
|
|
|
|
|
|
|
public function updateCMSFields(FieldList $fields)
|
|
|
|
{
|
|
|
|
$f = $fields->dataFieldByName('MenuTitle');
|
|
|
|
// Elements has own Title field to be used at content (h1 can be hidden),
|
|
|
|
// while Menu Title (h1 page title) and Navigation label should be equal for SEO
|
|
|
|
if ($f) {
|
|
|
|
// name page name as navigation label to be more clear for CMS admin
|
|
|
|
$fields->dataFieldByName('Title')->setTitle($f->Title());
|
|
|
|
$fields->removeByName('MenuTitle');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-24 18:51:04 +02:00
|
|
|
public function ShowSidebar()
|
|
|
|
{
|
|
|
|
$obj = $this->owner;
|
2023-09-29 20:27:01 +02:00
|
|
|
$area = $obj->ElementalArea();
|
|
|
|
if(!$area) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
$els = $area->Elements();
|
|
|
|
if(!$els) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
$els = $els->find('ClassName', SidebarElement::class);
|
|
|
|
if(!$els) {
|
|
|
|
return true;
|
|
|
|
}
|
2023-09-24 18:51:04 +02:00
|
|
|
|
2023-09-29 20:27:01 +02:00
|
|
|
if ($els->first()) {
|
2023-09-24 18:51:04 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($obj->SideBarContent) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (method_exists($obj, 'SideBarView')) {
|
|
|
|
$view = $obj->SideBarView();
|
|
|
|
|
|
|
|
if ($view && $view->Widgets()->count()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-06-20 18:50:49 +02:00
|
|
|
public function onBeforeWrite()
|
|
|
|
{
|
|
|
|
parent::onBeforeWrite();
|
|
|
|
|
|
|
|
// h1 page title and navigation label should be equal for SEO
|
|
|
|
$obj = $this->owner;
|
|
|
|
$obj->setField('MenuTitle', $obj->getField('Title'));
|
|
|
|
}
|
2021-06-19 21:30:03 +02:00
|
|
|
}
|