Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NEW Use autoscaffolding for SiteTree CMS fields #2983

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions code/Model/RedirectorPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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';

/**
Expand Down Expand Up @@ -194,33 +198,28 @@ protected function onBeforeWrite()
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');
Comment on lines 201 to 202
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a composite field that gets manually added, so we can't use ignoreFields for it.


$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"),
"File" => _t(__CLASS__.'.REDIRECTTOFILE', "A file on your website"),
],
"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'
);
});

Expand Down
267 changes: 137 additions & 130 deletions code/Model/SiteTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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 = [
Expand Down Expand Up @@ -2105,147 +2124,135 @@ public function VirtualPages()
*/
public function getCMSFields()
{
$dependentNote = '';
$dependentTable = new LiteralField('DependentNote', '<p></p>');
$this->beforeUpdateCMSFields(function (FieldList $fields) {
$dependentNote = '';
$dependentTable = LiteralField::create('DependentNote', '<p></p>');

// Create a table for showing pages linked to this one
$dependentPages = $this->DependentPages();
$dependentPagesCount = $dependentPages->count();
if ($dependentPagesCount) {
$dependentColumns = [
'Title' => $this->fieldLabel('Title'),
'DependentLinkType' => _t(__CLASS__.'.DependtPageColumnLinkType', 'Link type'),
];
if (class_exists(Subsite::class)) {
$dependentColumns['Subsite.Title'] = Subsite::singleton()->i18n_singular_name();
}

// Create a table for showing pages linked to this one
$dependentPages = $this->DependentPages();
$dependentPagesCount = $dependentPages->count();
if ($dependentPagesCount) {
$dependentColumns = [
'Title' => $this->fieldLabel('Title'),
'DependentLinkType' => _t(__CLASS__.'.DependtPageColumnLinkType', 'Link type'),
];
if (class_exists(Subsite::class)) {
$dependentColumns['Subsite.Title'] = Subsite::singleton()->i18n_singular_name();
$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,
$dependentPages
);
$dataColumns = $dependentTable->getConfig()->getComponentByType(GridFieldDataColumns::class);
$dataColumns
->setDisplayFields($dependentColumns)
->setFieldFormatting([
'Title' => function ($value, &$item) {
$title = $item->Title;
$untitled = _t(
__CLASS__ . '.UntitledDependentObject',
'Untitled {instanceType}',
['instanceType' => $item->i18n_singular_name()]
);
$tag = $item->hasMethod('CMSEditLink') ? 'a' : 'span';
return sprintf(
'<%s%s class="dependent-content__edit-link %s">%s</%s>',
$tag,
$tag === 'a' ? sprintf(' href="%s"', $item->CMSEditLink()) : '',
$title ? '' : 'dependent-content__edit-link--untitled',
$title ? Convert::raw2xml($title) : $untitled,
$tag
);
}
]);
$dependentTable->getConfig()->addComponent(Injector::inst()->create(GridFieldLazyLoader::class));
}

$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>');
$dependentTable = GridField::create(
'DependentPages',
false,
$dependentPages
$baseLink = Controller::join_links(
Director::absoluteBaseURL(),
(static::config()->get('nested_urls') && $this->ParentID ? $this->Parent()->RelativeLink(true) : null)
);
$dataColumns = $dependentTable->getConfig()->getComponentByType(GridFieldDataColumns::class);
$dataColumns
->setDisplayFields($dependentColumns)
->setFieldFormatting([
'Title' => function ($value, &$item) {
$title = $item->Title;
$untitled = _t(
__CLASS__ . '.UntitledDependentObject',
'Untitled {instanceType}',
['instanceType' => $item->i18n_singular_name()]
);
$tag = $item->hasMethod('CMSEditLink') ? 'a' : 'span';
return sprintf(
'<%s%s class="dependent-content__edit-link %s">%s</%s>',
$tag,
$tag === 'a' ? sprintf(' href="%s"', $item->CMSEditLink()) : '',
$title ? '' : 'dependent-content__edit-link--untitled',
$title ? Convert::raw2xml($title) : $untitled,
$tag
);
}
]);
$dependentTable->getConfig()->addComponent(Injector::inst()->create(GridFieldLazyLoader::class));
}

$baseLink = Controller::join_links(
Director::absoluteBaseURL(),
(static::config()->get('nested_urls') && $this->ParentID ? $this->Parent()->RelativeLink(true) : null)
);

$urlsegment = SiteTreeURLSegmentField::create("URLSegment", $this->fieldLabel('URLSegment'))
->setURLPrefix($baseLink)
->setURLSuffix('?stage=Stage')
->setDefaultURL($this->generateURLSegment(_t(
'SilverStripe\\CMS\\Controllers\\CMSMain.NEWPAGE',
'New {pagetype}',
['pagetype' => $this->i18n_singular_name()]
)))
->addExtraClass(($this->isHomePage() ? 'homepage-warning' : ''));
$helpText = (static::config()->get('nested_urls') && $this->numChildren())
? $this->fieldLabel('LinkChangeNote')
: '';
if (!URLSegmentFilter::create()->getAllowMultibyte()) {
$helpText .= _t('SilverStripe\\CMS\\Forms\\SiteTreeURLSegmentField.HelpChars', ' Special characters are automatically converted or removed.');
}
$urlsegment->setHelpText($helpText);

$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')),
ToggleCompositeField::create(
'Metadata',
_t(__CLASS__.'.MetadataToggle', 'Metadata'),
[
$metaFieldDesc = new TextareaField("MetaDescription", $this->fieldLabel('MetaDescription')),
$metaFieldExtra = new TextareaField("ExtraMeta", $this->fieldLabel('ExtraMeta'))
]
)->setHeadingLevel(4)
),
$tabDependent = new Tab(
'Dependent',
$dependentNote,
$dependentTable
)
)
);
$htmlField->addExtraClass('stacked');

// Help text for MetaData on page content editor
$metaFieldDesc
->setRightTitle(
_t(
'SilverStripe\\CMS\\Model\\SiteTree.METADESCHELP',
"Search engines use this content for displaying search results (although it will not influence their ranking)."
$urlsegment = SiteTreeURLSegmentField::create("URLSegment", $this->fieldLabel('URLSegment'))
->setURLPrefix($baseLink)
->setURLSuffix('?stage=Stage')
->setDefaultURL($this->generateURLSegment(_t(
'SilverStripe\\CMS\\Controllers\\CMSMain.NEWPAGE',
'New {pagetype}',
['pagetype' => $this->i18n_singular_name()]
)))
->addExtraClass(($this->isHomePage() ? 'homepage-warning' : ''));
$helpText = (static::config()->get('nested_urls') && $this->numChildren())
? $this->fieldLabel('LinkChangeNote')
: '';
if (!URLSegmentFilter::create()->getAllowMultibyte()) {
$helpText .= _t('SilverStripe\\CMS\\Forms\\SiteTreeURLSegmentField.HelpChars', ' Special characters are automatically converted or removed.');
}
$urlsegment->setHelpText($helpText);
$fields->replaceField('URLSegment', $urlsegment);

$fields->dataFieldByName('Content')?->addExtraClass('stacked');

// Metadata fields
$fields->addFieldsToTab('Root.Main', [
ToggleCompositeField::create(
'Metadata',
_t(__CLASS__.'.MetadataToggle', 'Metadata'),
[
$metaFieldDesc = TextareaField::create("MetaDescription", $this->fieldLabel('MetaDescription')),
$metaFieldExtra = TextareaField::create("ExtraMeta", $this->fieldLabel('ExtraMeta'))
]
)->setHeadingLevel(4),
]);
// Help text for MetaData on page content editor
$metaFieldDesc
->setRightTitle(
_t(
'SilverStripe\\CMS\\Model\\SiteTree.METADESCHELP',
"Search engines use this content for displaying search results (although it will not influence their ranking)."
)
)
)
->addExtraClass('help');
$metaFieldExtra
->setRightTitle(
_t(
'SilverStripe\\CMS\\Model\\SiteTree.METAEXTRAHELP',
"HTML tags for additional meta information. For example <meta name=\"customName\" content=\"your custom content here\">"
->addExtraClass('help');
$metaFieldExtra
->setRightTitle(
_t(
'SilverStripe\\CMS\\Model\\SiteTree.METAEXTRAHELP',
"HTML tags for additional meta information. For example <meta name=\"customName\" content=\"your custom content here\">"
)
)
)
->addExtraClass('help');

// Conditional dependent pages tab
if ($dependentPagesCount) {
$tabDependent->setTitle(_t(__CLASS__.'.TABDEPENDENT', "Dependent pages") . " ($dependentPagesCount)");
} else {
$fields->removeFieldFromTab('Root', 'Dependent');
}
->addExtraClass('help');

$tabMain->setTitle(_t(__CLASS__.'.TABCONTENT', "Main content"));
// 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)");
}

if ($this->ObsoleteClassName) {
$obsoleteWarning = _t(
'SilverStripe\\CMS\\Model\\SiteTree.OBSOLETECLASS',
"This page is of obsolete type {type}. Saving will reset its type and you may lose data",
['type' => $this->ObsoleteClassName]
);
$fields->findTab('Root.Main')->setTitle(_t(__CLASS__ . '.TABCONTENT', 'Main content'));

$fields->addFieldToTab(
"Root.Main",
LiteralField::create("ObsoleteWarningHeader", "<p class=\"alert alert-warning\">$obsoleteWarning</p>"),
"Title"
);
}
if ($this->ObsoleteClassName) {
$obsoleteWarning = _t(
'SilverStripe\\CMS\\Model\\SiteTree.OBSOLETECLASS',
"This page is of obsolete type {type}. Saving will reset its type and you may lose data",
['type' => $this->ObsoleteClassName]
);

if (SiteTree::$runCMSFieldsExtensions) {
$this->extend('updateCMSFields', $fields);
}
$fields->addFieldToTab(
"Root.Main",
LiteralField::create("ObsoleteWarningHeader", "<p class=\"alert alert-warning\">$obsoleteWarning</p>"),
"Title"
);
}
});

return $fields;
return parent::getCMSFields();
}


Expand Down
Loading
Loading