Skip to content

Add default matrix blocks when creating an entry #15638

Closed Answered by brandonkelly
svondervoort asked this question in Ideas
Discussion options

You must be logged in to vote

Doing this with a field/field layout setting isn’t possible, because the default values could include relational fields, etc., which reference other content in the system. And we can’t store content references in any settings that get stored in the project config, since content varies from environment to environment.

That said, you can pull it off with a little custom code in a module:

<?php

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

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

    if (
        $event->isNew &&
        $entry->getIsUnpub…

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@svondervoort
Comment options

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
3 participants