silverstripe-4blog-post

User firendly category selector in post options in Silverstripe cms


I would like to change the behaviour of the select box for selecting Post categories within the Post Options in Silverstripe cms. Currently I can find and select the categories only if I know them beforehand. That is the when I enter a letter it starts to populate the select box with the categories that contain that letter. I would like to populate the select box with all categories by default. And still maintain multi select option. Has this option been considered yet or should I develop my custom category selector?


Solution

  • It sounds like you're specifically referring to the silverstripe/blog module.

    The categories field is a TagField from the silverstripe/tagfield module. In the BlogPost class, the field is set to lazy load: https://github.com/silverstripe/silverstripe-blog/blob/0f8aefb0920812f4f9cc03bba5a0ed4b7b6df205/src/Model/BlogPost.php#L350

    You can override that in your project by applying an extension to BlogPost and turn lazyloading off for that field:

    app/_config/extensions.yml

    SilverStripe\Blog\Model\BlogPost:
      extensions:
        - App\Extension\BlogPostExtension
    

    app/src/Extension/BlogPostExtension.php

    <?php
    
    namespace App\Extension;
    
    use SilverStripe\Core\Extension;
    use SilverStripe\Forms\FieldList;
    
    class BlogPostExtension extends Extension
    {
        public function updateCMSFields(FieldList $fields)
        {
            $fields->dataFieldByName('Categories')->setShouldLazyLoad(false);
        }
    }
    

    You might want to do the same for the Tags field while you're at it.