Question: Can I (or should I) manage a “featured posts” list using ModelAdmin?
I would like to use ModelAdmin so that I have a central location to see and add/remove existing blog posts to a limited list of, say, 3 posts. I am using the Silverstripe Blog Module.
I’m familiar with adding a ModelAdmin page to add/edit/delete DataObjects, but here, I only want to be able to create a UI to exclusively select, using GridFieldAddExistingSearchButton, to identify up to 3 BlogPosts, and have that selection saved and retrievable from my templates.
I’ve gotten as far as adding BlogPost to $managed_models
But this only results in listing all the blog posts.
I realize I could add a “featured” checkbox to the BlogPost objects (and I have), but it has been a mess when many more posts than intended get this flag and I have to go looking for which ones have it… With ModelAdmin I thought I’d try this from the other direction and create a UI with 3 slots to fill. Then everything would be centralized and ONLY 3 posts would ever be featured.
you could e.g. create a “featured blog posts” elemental block that holds a manymany relation to BlogPosts (or if you don’t use Elemental, add a relation to SiteConfig). Then you can have a GridField for editing this relations in a central place.
I once made this kind of block for https://www.s2-hub.com, the main parts of that block are:
private static $many_many = [
'RelatedArticles' => BlogPost::class
];
private static $many_many_extraFields = [
'RelatedArticles' => [
'SortOrder' => 'Int'
]
];
/* Set to false to prevent an in-line edit form from showing in an elemental area. Instead the element will be
* clickable and a GridFieldDetailForm will be used.
*
* @config
* @var bool
*/
private static $inline_editable = false;
public function getCMSFields()
{
$fields = parent::getCMSFields();
/** @var GridField $relationGrid */
$relationGrid = $fields->dataFieldByName('RelatedArticles');
if ($relationGrid) {
$orderableRows = GridFieldOrderableRows::create()
->setSortField('SortOrder');
$relationGrid->getConfig()->addComponent($orderableRows);
}
return $fields;
}
I hope that helps.
Of course, with ModelAdmin you could do something similar using filters (for showing all blog posts or just featured ones, filter by dates etc…), bulk actions, gridfield-inline-edit checkbox etc. It’s possible, depending on your needs another solution might be much easier.