1

I'm creating a cron job to automatically import new entries (vacancies).
I have an entry that I use as a template with some standard data fields and matrix fields. If it's an entry that has not been imported before I create a new entry as a duplicate of the template entry.

$entry = Craft::$app->getElements()->duplicateElement($entryTemplate);

The template already has the matrix blocks but only placeholder data that I want to overwrite like this:

// existing matrix fields (already created or from template)
$matrixBlocks = $entry->myMatrixBlock;

/** @var MatrixBlock $matrixBlock */
foreach($matrixBlocks as $matrixBlock) {
    $matrixBlock->setFieldValues(['contactText' => 'random text']);

    $success = Craft::$app->elements->saveElement($matrixBlock);
}

The strange thing is that the ownerId of the matrixBlock isn't the value of the $entry->id nor is it the value of the template id. It generated a new ownerId. It works correctly when I import it a second time because the owner id is now correct.

1

This sounds suspiciously like a bug that was fixed in today's Craft 3.6.10 release.

Fixed a bug where entries could return the wrong Matrix/Neo/Super Table blocks if the blocks were queried immediately after the entry was created via craft\services\Elements::duplicateElement().

My guess is if you update to that release, it'll behave like you expect now.

1
  • Yes I contacted craft support with this question and it was indeed this bug. They fixed it right away.
    – schnitzels
    Mar 10 at 13:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.