Skip to content

Commit

Permalink
Don't save block types from Matrix::afterSave() if project.yaml has p…
Browse files Browse the repository at this point in the history
…ending block type changes

Fixes #3695
  • Loading branch information
brandonkelly committed Jan 23, 2019
1 parent a8bd865 commit 6733f86
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

### Fixed
- Fixed a bug where `site` translations were falling back to English if the translated message was identical to the source message. ([#3692](https://github.com/craftcms/cms/issues/3692))
- Fixed a bug where syncing Matrix field changes to the project config would result in new changes to the project config. ([#3695](https://github.com/craftcms/cms/issues/3695))

## 3.1.3 - 2019-01-21

Expand Down
50 changes: 26 additions & 24 deletions src/services/Matrix.php
Original file line number Diff line number Diff line change
Expand Up @@ -573,38 +573,40 @@ public function saveSettings(MatrixField $matrixField, bool $validate = true): b
}
}

// Delete the old block types first, in case there's a handle conflict with one of the new ones
$oldBlockTypes = $this->getBlockTypesByFieldId($matrixField->id);
$oldBlockTypesById = [];
if (!Craft::$app->getProjectConfig()->areChangesPending(self::CONFIG_BLOCKTYPE_KEY)) {
// Delete the old block types first, in case there's a handle conflict with one of the new ones
$oldBlockTypes = $this->getBlockTypesByFieldId($matrixField->id);
$oldBlockTypesById = [];

foreach ($oldBlockTypes as $blockType) {
$oldBlockTypesById[$blockType->id] = $blockType;
}
foreach ($oldBlockTypes as $blockType) {
$oldBlockTypesById[$blockType->id] = $blockType;
}

foreach ($matrixField->getBlockTypes() as $blockType) {
if (!$blockType->getIsNew()) {
unset($oldBlockTypesById[$blockType->id]);
foreach ($matrixField->getBlockTypes() as $blockType) {
if (!$blockType->getIsNew()) {
unset($oldBlockTypesById[$blockType->id]);
}
}
}

foreach ($oldBlockTypesById as $blockType) {
$this->deleteBlockType($blockType);
}
foreach ($oldBlockTypesById as $blockType) {
$this->deleteBlockType($blockType);
}

// Save the new ones
$sortOrder = 0;
// Save the new ones
$sortOrder = 0;

$originalContentTable = Craft::$app->getContent()->contentTable;
Craft::$app->getContent()->contentTable = $matrixField->contentTable;
$originalContentTable = Craft::$app->getContent()->contentTable;
Craft::$app->getContent()->contentTable = $matrixField->contentTable;

foreach ($matrixField->getBlockTypes() as $blockType) {
$sortOrder++;
$blockType->fieldId = $matrixField->id;
$blockType->sortOrder = $sortOrder;
$this->saveBlockType($blockType, false);
}
foreach ($matrixField->getBlockTypes() as $blockType) {
$sortOrder++;
$blockType->fieldId = $matrixField->id;
$blockType->sortOrder = $sortOrder;
$this->saveBlockType($blockType, false);
}

Craft::$app->getContent()->contentTable = $originalContentTable;
Craft::$app->getContent()->contentTable = $originalContentTable;
}

$transaction->commit();
} catch (\Throwable $e) {
Expand Down

0 comments on commit 6733f86

Please sign in to comment.