diff --git a/src/services/Matrix.php b/src/services/Matrix.php index 932996910f8..e50def5c192 100644 --- a/src/services/Matrix.php +++ b/src/services/Matrix.php @@ -633,6 +633,8 @@ public function saveField(MatrixField $field, ElementInterface $owner) $blocks = $query->all(); } + $elementsService = Craft::$app->getElements(); + $transaction = Craft::$app->getDb()->beginTransaction(); try { // If this is a preexisting element, make sure that the blocks for this field/owner respect the field's translation setting @@ -647,7 +649,6 @@ public function saveField(MatrixField $field, ElementInterface $owner) $newQuery->ownerId = $owner->id; if (!$newQuery->exists()) { // Duplicate the blocks for the new owner - $elementsService = Craft::$app->getElements(); foreach ($blocks as $block) { $elementsService->duplicateElement($block, [ 'ownerId' => $owner->id, @@ -667,7 +668,7 @@ public function saveField(MatrixField $field, ElementInterface $owner) $block->ownerSiteId = ($field->localizeBlocks ? $owner->siteId : null); $block->propagating = $owner->propagating; - Craft::$app->getElements()->saveElement($block, false, $propagate); + $elementsService->saveElement($block, false, $propagate); $blockIds[] = $block->id;