Feature Request: Ability to select a default category in CP #10397
Answered
by
brandonkelly
adrianjean
asked this question in
Ideas
-
For the Category Field Type, it would be great to be able to select a default category in the CP. This way when users are filling out entry forms they can have a category pre-selected for them that they can change if they want. |
Beta Was this translation helpful? Give feedback.
Answered by
brandonkelly
Jan 21, 2022
Replies: 1 comment
-
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];
}
}
); |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
brandonkelly
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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: