NEW Use autoscaffolding for SiteTree CMS fields (#2983)

This commit is contained in:
Guy Sartorelli 2024-08-15 13:14:57 +12:00 committed by GitHub
parent 63fd61716e
commit e58c388cb7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 202 additions and 163 deletions

View File

@ -3,13 +3,10 @@
namespace SilverStripe\CMS\Model;
use Page;
use SilverStripe\AssetAdmin\Forms\UploadField;
use SilverStripe\Assets\File;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\HeaderField;
use SilverStripe\Forms\OptionsetField;
use SilverStripe\Forms\TextField;
use SilverStripe\Forms\TreeDropdownField;
use SilverStripe\Versioned\Versioned;
/**
@ -45,6 +42,13 @@ class RedirectorPage extends Page
"LinkToFile" => File::class,
];
private static array $scaffold_cms_fields_settings = [
'ignoreFields' => [
'RedirectionType',
'Content',
],
];
private static $table_name = 'RedirectorPage';
/**
@ -194,18 +198,19 @@ class RedirectorPage extends Page
public function getCMSFields()
{
$this->beforeUpdateCMSFields(function (FieldList $fields) {
$fields->removeByName('Content', true);
// Remove all metadata fields, does not apply for redirector pages
$fields->removeByName('Metadata');
$fields->addFieldsToTab(
'Root.Main',
[
new HeaderField('RedirectorDescHeader', _t(__CLASS__.'.HEADER', "This page will redirect users to another page")),
new OptionsetField(
"RedirectionType",
_t(__CLASS__.'.REDIRECTTO', "Redirect to"),
HeaderField::create(
'RedirectorDescHeader',
_t(__CLASS__.'.HEADER', "This page will redirect users to another page")
),
OptionsetField::create(
'RedirectionType',
$this->fieldLabel('RedirectionType'),
[
"Internal" => _t(__CLASS__.'.REDIRECTTOPAGE', "A page on your website"),
"External" => _t(__CLASS__.'.REDIRECTTOEXTERNAL', "Another website"),
@ -213,14 +218,8 @@ class RedirectorPage extends Page
],
"Internal"
),
new TreeDropdownField(
"LinkToID",
_t(__CLASS__.'.YOURPAGE', "Page on your website"),
SiteTree::class
),
new UploadField('LinkToFile', _t(__CLASS__.'.FILE', "File")),
new TextField("ExternalURL", _t(__CLASS__.'.OTHERURL', "Other website URL"))
]
],
'ExternalURL'
);
});

View File

@ -247,8 +247,8 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi
private static $namespace_map = null;
private static $db = [
"URLSegment" => "Varchar(255)",
"Title" => "Varchar(255)",
"URLSegment" => "Varchar(255)",
"MenuTitle" => "Varchar(100)",
"Content" => "HTMLText",
"MetaDescription" => "Text",
@ -295,6 +295,25 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi
"ShowInSearch" => 1,
];
private static array $scaffold_cms_fields_settings = [
'ignoreFields' => [
'ShowInMenus',
'ShowInSearch',
'Sort',
'HasBrokenFile',
'HasBrokenLink',
'ReportClass',
'Parent',
// The metadata fields will be added back with explicit fields
'MetaDescription',
'ExtraMeta',
],
'ignoreRelations' => [
'VirtualPages',
'BackLinks',
],
];
private static $table_name = 'SiteTree';
private static $versioning = [
@ -2105,8 +2124,9 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi
*/
public function getCMSFields()
{
$this->beforeUpdateCMSFields(function (FieldList $fields) {
$dependentNote = '';
$dependentTable = new LiteralField('DependentNote', '<p></p>');
$dependentTable = LiteralField::create('DependentNote', '<p></p>');
// Create a table for showing pages linked to this one
$dependentPages = $this->DependentPages();
@ -2120,7 +2140,7 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi
$dependentColumns['Subsite.Title'] = Subsite::singleton()->i18n_singular_name();
}
$dependentNote = new LiteralField('DependentNote', '<p>' . _t(__CLASS__.'.DEPENDENT_NOTE', 'The following pages depend on this page. This includes virtual pages, redirector pages, and pages with content links.') . '</p>');
$dependentNote = LiteralField::create('DependentNote', '<p>' . _t(__CLASS__.'.DEPENDENT_NOTE', 'The following pages depend on this page. This includes virtual pages, redirector pages, and pages with content links.') . '</p>');
$dependentTable = GridField::create(
'DependentPages',
false,
@ -2172,34 +2192,21 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi
$helpText .= _t('SilverStripe\\CMS\\Forms\\SiteTreeURLSegmentField.HelpChars', ' Special characters are automatically converted or removed.');
}
$urlsegment->setHelpText($helpText);
$fields->replaceField('URLSegment', $urlsegment);
$fields = new FieldList(
$rootTab = new TabSet(
"Root",
$tabMain = new Tab(
'Main',
new TextField("Title", $this->fieldLabel('Title')),
$urlsegment,
new TextField("MenuTitle", $this->fieldLabel('MenuTitle')),
$htmlField = HTMLEditorField::create("Content", _t(__CLASS__.'.HTMLEDITORTITLE', "Content", 'HTML editor title')),
$fields->dataFieldByName('Content')?->addExtraClass('stacked');
// Metadata fields
$fields->addFieldsToTab('Root.Main', [
ToggleCompositeField::create(
'Metadata',
_t(__CLASS__.'.MetadataToggle', 'Metadata'),
[
$metaFieldDesc = new TextareaField("MetaDescription", $this->fieldLabel('MetaDescription')),
$metaFieldExtra = new TextareaField("ExtraMeta", $this->fieldLabel('ExtraMeta'))
$metaFieldDesc = TextareaField::create("MetaDescription", $this->fieldLabel('MetaDescription')),
$metaFieldExtra = TextareaField::create("ExtraMeta", $this->fieldLabel('ExtraMeta'))
]
)->setHeadingLevel(4)
),
$tabDependent = new Tab(
'Dependent',
$dependentNote,
$dependentTable
)
)
);
$htmlField->addExtraClass('stacked');
)->setHeadingLevel(4),
]);
// Help text for MetaData on page content editor
$metaFieldDesc
->setRightTitle(
@ -2220,12 +2227,15 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi
// Conditional dependent pages tab
if ($dependentPagesCount) {
$fields->addFieldsToTab('Root.Dependent', [
$dependentNote,
$dependentTable
]);
$tabDependent = $fields->findTab('Root.Dependent');
$tabDependent->setTitle(_t(__CLASS__.'.TABDEPENDENT', "Dependent pages") . " ($dependentPagesCount)");
} else {
$fields->removeFieldFromTab('Root', 'Dependent');
}
$tabMain->setTitle(_t(__CLASS__.'.TABCONTENT', "Main content"));
$fields->findTab('Root.Main')->setTitle(_t(__CLASS__ . '.TABCONTENT', 'Main content'));
if ($this->ObsoleteClassName) {
$obsoleteWarning = _t(
@ -2240,12 +2250,9 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi
"Title"
);
}
});
if (SiteTree::$runCMSFieldsExtensions) {
$this->extend('updateCMSFields', $fields);
}
return $fields;
return parent::getCMSFields();
}

View File

@ -7,6 +7,8 @@ use SilverStripe\Core\Convert;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\LiteralField;
use SilverStripe\Forms\ReadonlyTransformation;
use SilverStripe\Forms\TextareaField;
use SilverStripe\Forms\TextField;
use SilverStripe\Forms\TreeDropdownField;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\ValidationResult;
@ -77,8 +79,23 @@ class VirtualPage extends Page
private static $db = [
"VersionID" => "Int",
'CustomMetaDescription' => 'Text',
'CustomExtraMeta' => 'HTMLText'
];
private static array $scaffold_cms_fields_settings = [
'ignoreFields' => [
'VersionID',
'CustomMetaDescription',
'CustomExtraMeta',
],
];
/**
* Whether to allow overriding the meta description and extra meta tags.
*/
private static bool $allow_meta_overrides = true;
private static $table_name = 'VirtualPage';
/**
@ -216,21 +233,18 @@ class VirtualPage extends Page
public function getCMSFields()
{
$this->beforeUpdateCMSFields(function (FieldList $fields) {
// Setup the linking to the original page.
$copyContentFromField = TreeDropdownField::create(
'CopyContentFromID',
_t(VirtualPage::class . '.CHOOSE', "Linked Page"),
SiteTree::class
);
$copyContentFromField = $fields->dataFieldByName('CopyContentFromID');
$fields->addFieldToTab('Root.Main', $copyContentFromField, 'Title');
// Setup virtual fields
if ($virtualFields = $this->getVirtualFields()) {
$roTransformation = new ReadonlyTransformation();
foreach ($virtualFields as $virtualField) {
if ($fields->dataFieldByName($virtualField)) {
foreach ($virtualFields as $virtualFieldName) {
$virtualField = $fields->dataFieldByName($virtualFieldName);
if ($virtualField) {
$fields->replaceField(
$virtualField,
$fields->dataFieldByName($virtualField)->transform($roTransformation)
$virtualFieldName,
$virtualField->transform($roTransformation)
);
}
}
@ -238,8 +252,6 @@ class VirtualPage extends Page
$msgs = [];
$fields->addFieldToTab('Root.Main', $copyContentFromField, 'Title');
// Create links back to the original object in the CMS
if ($this->CopyContentFrom()->exists()) {
$link = HTML::createTag(
@ -280,6 +292,25 @@ class VirtualPage extends Page
'VirtualPageMessage',
'<div class="alert alert-info">' . implode('. ', $msgs) . '.</div>'
), 'CopyContentFromID');
if (static::config()->get('allow_meta_overrides')) {
$fields->addFieldToTab(
'Root.Main',
TextareaField::create(
'CustomMetaDescription',
$this->fieldLabel('CustomMetaDescription')
)->setDescription(_t(__CLASS__ . '.OverrideNote', 'Overrides inherited value from the source')),
'MetaDescription'
);
$fields->addFieldToTab(
'Root.Main',
TextField::create(
'CustomExtraMeta',
$this->fieldLabel('CustomExtraMeta')
)->setDescription(_t(__CLASS__ . '.OverrideNote', 'Overrides inherited value from the source')),
'ExtraMeta'
);
}
});
return parent::getCMSFields();

View File

@ -163,10 +163,10 @@ en:
REDIRECTTOPAGE: 'A page on your website'
SINGULARNAME: 'Redirector Page'
YOURPAGE: 'Page on your website'
db_ExternalURL: 'External URL'
db_RedirectionType: 'Redirection type'
has_one_LinkTo: 'Link to'
has_one_LinkToFile: 'Link to file'
db_ExternalURL: 'Other website URL'
db_RedirectionType: 'Redirect to'
has_one_LinkTo: 'Page on your website'
has_one_LinkToFile: 'File'
SilverStripe\CMS\Model\RedirectorPageController:
HASBEENSETUP: 'A redirector page has been set up without anywhere to redirect to.'
SilverStripe\CMS\Model\SiteTree:
@ -341,8 +341,10 @@ en:
other: '{count} Base Pages'
PageTypNotAllowedOnRoot: 'Original page type "{type}" is not allowed on the root level for this virtual page'
SINGULARNAME: 'Virtual Page'
db_CustomMetaDescription: 'Custom Meta Description'
db_CustomExtraMeta: 'Custom Meta Tags'
db_VersionID: 'Version ID'
has_one_CopyContentFrom: 'Copy content from'
has_one_CopyContentFrom: 'Linked Page'
SilverStripe\CMS\Reports\BrokenFilesReport:
BROKENFILES: 'Pages with broken files'
BrokenLinksGroupTitle: 'Broken links reports'