From 303bffbd857f6ee0e6187f105c2e988db493e354 Mon Sep 17 00:00:00 2001 From: brandonkelly Date: Wed, 3 Oct 2018 06:47:44 -0700 Subject: [PATCH] Duplicate Matrix blocks across all sites when creating an element Resolves #3082 --- CHANGELOG-v3.md | 1 + src/services/Matrix.php | 27 +++++++++++++++++++-------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/CHANGELOG-v3.md b/CHANGELOG-v3.md index 493857c4d8d..f00a5e1f5d4 100644 --- a/CHANGELOG-v3.md +++ b/CHANGELOG-v3.md @@ -8,6 +8,7 @@ ### Changed - It’s now possible to load a Create Entry page with a specific user preselected in the Author field, using a new `authorId` query string param. ([#3326](https://github.com/craftcms/cms/pull/3326)) - The `svg()` Twig function now has a `namespace` argument, which can be set to `false` to avoid namespacing IDs and class names. ([#3337](https://github.com/craftcms/cms/issues/3337)) +- Matrix fields that are set to manage blocks on a per-site basis will now duplicate Matrix blocks across all of the owner element’s supported sites when the element is first created. ([#3082](https://github.com/craftcms/cms/issues/3082)) - Disabled Matrix blocks are no longer visible when sharing an entry draft or version. ([#3338](https://github.com/craftcms/cms/issues/3338)) - Control Panel tabs that have errors now have alert icons. diff --git a/src/services/Matrix.php b/src/services/Matrix.php index e50def5c192..d158553beb1 100644 --- a/src/services/Matrix.php +++ b/src/services/Matrix.php @@ -621,9 +621,12 @@ public function saveField(MatrixField $field, ElementInterface $owner) /** @var MatrixBlock[] $blocks */ $query = $owner->getFieldValue($field->handle); - // Skip if the query's site ID is different than the element's - // (Indicates that the value as copied from another site for element propagation) - if ($query->siteId != $owner->siteId) { + // If the element is brand new and propagating, and the field manages blocks on a per-site basis, + // then we will need to duplicate the blocks for this site + $duplicateBlocks = !$query->ownerId && $owner->propagating && $field->localizeBlocks; + + // Skip if the element is propagating right now, and we don't need to duplicate the blocks + if ($owner->propagating && !$duplicateBlocks) { return; } @@ -664,11 +667,19 @@ public function saveField(MatrixField $field, ElementInterface $owner) $propagate = !$owner->propagating; foreach ($blocks as $block) { - $block->ownerId = $owner->id; - $block->ownerSiteId = ($field->localizeBlocks ? $owner->siteId : null); - $block->propagating = $owner->propagating; - - $elementsService->saveElement($block, false, $propagate); + if ($duplicateBlocks) { + $block = $elementsService->duplicateElement($block, [ + 'ownerId' => $owner->id, + 'ownerSiteId' => ($field->localizeBlocks ? $owner->siteId : null), + 'siteId' => $owner->siteId, + 'propagating' => false, + ]); + } else { + $block->ownerId = $owner->id; + $block->ownerSiteId = ($field->localizeBlocks ? $owner->siteId : null); + $block->propagating = $owner->propagating; + $elementsService->saveElement($block, false, $propagate); + } $blockIds[] = $block->id;