Skip to content

Commit

Permalink
Duplicate Matrix blocks across all sites when creating an element
Browse files Browse the repository at this point in the history
Resolves #3082
  • Loading branch information
brandonkelly committed Oct 3, 2018
1 parent cb69ddf commit 303bffb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
27 changes: 19 additions & 8 deletions src/services/Matrix.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 303bffb

Please sign in to comment.