Skip to content

Commit

Permalink
NEW Use autoscaffolding for SiteTree CMS fields (silverstripe#2983)
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli authored and emteknetnz committed Aug 19, 2024
1 parent 406e241 commit 6120e89
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 34 deletions.
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');

$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
55 changes: 43 additions & 12 deletions code/Model/VirtualPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
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;
Expand Down Expand Up @@ -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';

/**
Expand Down Expand Up @@ -216,30 +233,25 @@ public function isPublishable()
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)
);
}
}
}

$msgs = [];

$fields->addFieldToTab('Root.Main', $copyContentFromField, 'Title');

// Create links back to the original object in the CMS
if ($this->CopyContentFrom()->exists()) {
$link = HTML::createTag(
Expand Down Expand Up @@ -280,6 +292,25 @@ public function getCMSFields()
'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();
Expand Down
12 changes: 7 additions & 5 deletions lang/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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'
Expand Down

0 comments on commit 6120e89

Please sign in to comment.