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

FIX Update CMS fields now that they're being scaffolded #775

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
11 changes: 11 additions & 0 deletions src/Model/Blog.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,17 @@ class Blog extends Page implements PermissionProvider
'PostsPerPage' => 10
];

private static array $scaffold_cms_fields_settings = [
'ignoreFields' => [
'PostsPerPage',
],
'ignoreRelations' => [
'Editors',
'Writers',
'Contributors',
],
];

/**
* @var string
*/
Expand Down
105 changes: 46 additions & 59 deletions src/Model/BlogPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@
namespace SilverStripe\Blog\Model;

use Page;
use SilverStripe\AssetAdmin\Forms\UploadField;
use SilverStripe\Assets\Image;
use SilverStripe\Control\Controller;
use SilverStripe\Core\Config\Config;
use SilverStripe\Forms\DatetimeField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\HTMLEditor\HTMLEditorField;
use SilverStripe\Forms\ListboxField;
use SilverStripe\Forms\TextField;
use SilverStripe\Forms\ToggleCompositeField;
Expand All @@ -23,7 +19,6 @@
use SilverStripe\Security\Member;
use SilverStripe\Security\Permission;
use SilverStripe\Security\Security;
use SilverStripe\TagField\TagField;
use SilverStripe\Versioned\Versioned;
use SilverStripe\View\ArrayData;
use SilverStripe\View\Requirements;
Expand Down Expand Up @@ -100,6 +95,15 @@ class BlogPost extends Page
'Authors' => Member::class
];

private static array $scaffold_cms_fields_settings = [
'ignoreFields' => [
'AuthorNames',
],
'ignoreRelations' => [
'Authors',
],
];

/**
* The ProvideComments db column is defined in the silverstripe/comments module
* The InheritSideBar db column is defined in the silverstripe/widgets module
Expand Down Expand Up @@ -252,42 +256,40 @@ public function getCMSFields()
Requirements::javascript('silverstripe/blog:client/dist/js/main.bundle.js');

$this->beforeUpdateCMSFields(function ($fields) {
$uploadField = UploadField::create('FeaturedImage', _t(__CLASS__ . '.FeaturedImage', 'Featured Image'));
$uploadField->getValidator()->setAllowedExtensions(['jpg', 'jpeg', 'png', 'gif']);
$uploadField = $fields->dataFieldByName('FeaturedImage');
$uploadField?->getValidator()->setAllowedExtensions(['jpg', 'jpeg', 'png', 'gif']);

$uploadDirectory = $this->config()->get('featured_images_directory');
if ($uploadDirectory != '') {
$uploadField->setFolderName($uploadDirectory);
$uploadField?->setFolderName($uploadDirectory);
}

/**
* @var FieldList $fields
*/
$fields->insertAfter('Content', $uploadField);

$summary = HtmlEditorField::create('Summary', false);
$summary->setRows(5);
$summary->setDescription(_t(
$summaryField = $fields->dataFieldByName('Summary');
$fields->removeByName('Summary');
$summaryField?->setTitle('');
$summaryField?->setRows(5);
$summaryField?->setDescription(_t(
__CLASS__ . '.SUMMARY_DESCRIPTION',
'If no summary is specified the first 30 words will be used.'
));

$summaryHolder = ToggleCompositeField::create(
'CustomSummary',
_t(__CLASS__ . '.CUSTOMSUMMARY', 'Add A Custom Summary'),
[
$summary,
]
);
$summaryHolder->setHeadingLevel(4);
$summaryHolder->addExtraClass('custom-summary');
if ($summaryField) {
$summaryHolder = ToggleCompositeField::create(
'CustomSummary',
_t(__CLASS__ . '.CUSTOMSUMMARY', 'Add A Custom Summary'),
[
$summaryField,
]
);
$summaryHolder->setHeadingLevel(4);
$summaryHolder->addExtraClass('custom-summary');

if ($this->Summary) {
$summaryHolder->setStartClosed(false);
if ($this->Summary) {
$summaryHolder->setStartClosed(false);
}
$fields->insertAfter($uploadField ? 'FeaturedImage' : 'Content', $summaryHolder);
}

$fields->insertAfter('FeaturedImage', $summaryHolder);

$authorField = ListboxField::create(
'Authors',
_t(__CLASS__ . '.Authors', 'Authors'),
Expand All @@ -312,46 +314,33 @@ public function getCMSFields()
$authorNames = $authorNames->performDisabledTransformation();
}

$publishDate = DatetimeField::create('PublishDate', _t(__CLASS__ . '.PublishDate', 'Publish Date'));

$publishDate = $fields->dataFieldByName('PublishDate');
if (!$this->PublishDate) {
$publishDate->setDescription(
$publishDate?->setDescription(
_t(
__CLASS__ . '.PublishDate_Description',
'Will be set to "now" if published without a value.'
)
);
}

// Get categories and tags
$parent = $this->Parent();
$categories = $parent instanceof Blog
? $parent->Categories()
: BlogCategory::get();
$tags = $parent instanceof Blog
? $parent->Tags()
: BlogTag::get();
$postOptions = [];
if ($publishDate) {
$postOptions[] = $publishDate;
}
$categories = $fields->dataFieldByName('Categories');
if ($categories) {
$postOptions[] = $categories;
}
$tags = $fields->dataFieldByName('Tags');
if ($tags) {
$postOptions[] = $tags;
}

$fields->addFieldsToTab(
'Root.PostOptions',
[
$publishDate,
TagField::create(
'Categories',
_t(__CLASS__ . '.Categories', 'Categories'),
$categories,
$this->Categories()
)
->setCanCreate($this->canCreateCategories())
->setShouldLazyLoad(true),
TagField::create(
'Tags',
_t(__CLASS__ . '.Tags', 'Tags'),
$tags,
$this->Tags()
)
->setCanCreate($this->canCreateTags())
->setShouldLazyLoad(true),
...$postOptions,
$authorField,
$authorNames
]
Expand All @@ -363,8 +352,6 @@ public function getCMSFields()

$fields = parent::getCMSFields();

$fields->fieldByName('Root')->setTemplate('TabSet_holder');
Copy link
Member Author

Choose a reason for hiding this comment

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

The template name was wrong so this hasn't been used since namespaces were introduced. Just remove altogether at this point.


return $fields;
}

Expand Down
4 changes: 0 additions & 4 deletions templates/SilverStripe/Blog/Forms/TabSet_holder.ss

This file was deleted.

Loading