Skip to content

Feature Request: Ability to select a default category in CP #10397

Answered by brandonkelly
adrianjean asked this question in Ideas
Discussion options

You must be logged in to vote

We can’t add a setting for this, because categories are content and not stored in the project config.

You could do it with a little custom code in a module, though:

use craft\elements\Entry;
use craft\events\ModelEvent;
use yii\base\Event;

Event::on(
    Entry::class,
    Entry::EVENT_BEFORE_SAVE,
    function (ModelEvent $event) {
        /** @var Entry $entry */
        $entry = $event->sender;

        // Is this a brand new provisional draft?
        if ($entry->getIsUnpublishedDraft() && $event->isNew && !$entry->duplicateOf) {
            // Set a default category relation
            $entry->categories = [2360];
        }
    }
);

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by brandonkelly
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Ideas
Labels
None yet
2 participants