Skip to content

Commit

Permalink
feat: add ability to change field tab name using config API
Browse files Browse the repository at this point in the history
  • Loading branch information
wilr committed Aug 10, 2023
1 parent 8a8c937 commit 98cecef
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
17 changes: 15 additions & 2 deletions src/Extensions/PageHealthExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ class PageHealthExtension extends DataExtension
{
const EMPTY_HTML = '<p></p>';

private static $tab_name = 'Root.Seo';

private static $move_default_meta_fields = true;

/**
* @var string|null
*/
Expand Down Expand Up @@ -103,14 +107,14 @@ public function updateCMSFields(FieldList $fields)
return;
}

if ($this->owner instanceof \SilverStripe\ErrorPage\ErrorPage) {
if (class_exists('\SilverStripe\ErrorPage\ErrorPage') && $this->owner instanceof \SilverStripe\ErrorPage\ErrorPage) {
return;
}

$dom = $this->getRenderedHtmlDomParser();

if ($dom) {
$fields->addFieldsToTab('Root.Main', [
$fields->addFieldsToTab($this->owner->config()->get('tab_name'), [
ToggleCompositeField::create('SEOHealthAnalysis', 'SEO Health Analysis', [
GoogleSearchPreview::create(
'GoogleSearchPreview',
Expand All @@ -122,6 +126,15 @@ public function updateCMSFields(FieldList $fields)
HealthAnalysisField::create('ContentAnalysis', 'Content Analysis', $this->getOwner()),
])
], 'Metadata');

if ($this->owner->config()->get('move_default_meta_fields')) {
$meta = $fields->fieldByName('Root.Main.Metadata');

if ($meta) {
$fields->removeByName('Metadata');
$fields->addFieldToTab($this->owner->config()->get('tab_name'), $meta);
}
}
}
}
}
8 changes: 5 additions & 3 deletions src/Extensions/PageSeoExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class PageSeoExtension extends DataExtension
{
use Configurable;

private static $tab_name = 'Root.Seo';

private static $cascade_deletes = [
'FacebookPageImage',
'TwitterPageImage'
Expand Down Expand Up @@ -108,7 +110,7 @@ public function updateCMSFields(FieldList $fields)
$suppressMessaging = true;
}

$fields->addFieldsToTab('Root.Main', [
$fields->addFieldsToTab($this->config()->get('tab_name'), [
ToggleCompositeField::create('FacebookSeoComposite', 'Facebook SEO', [
DropdownField::create('FacebookPageType', 'Type', FacebookMetaGenerator::getValidTypes()),
TextField::create('FacebookPageTitle', 'Title')
Expand All @@ -124,7 +126,7 @@ public function updateCMSFields(FieldList $fields)
->setFolderName('seo'),
TextareaField::create('FacebookPageDescription', 'Description')
->setAttribute('placeholder', $this->getOwner()->MetaDescription ?:
$this->getOwner()->dbObject('Content')->LimitCharacters(297))
$this->getOwner()->dbObject('Content')->LimitCharacters(297))
->setRightTitle($suppressMessaging
? ''
: 'If blank, inherits meta description if it exists ' .
Expand All @@ -145,7 +147,7 @@ public function updateCMSFields(FieldList $fields)
->setRightTitle($suppressMessaging
? ''
: 'If blank, inherits meta description if it exists ' .
'or gets the first 297 characters from content')
'or gets the first 297 characters from content')
->setTargetLength(200, 160, 320),
])
], 'Metadata');
Expand Down

0 comments on commit 98cecef

Please sign in to comment.