Skip to content

Commit

Permalink
Fixed #4015
Browse files Browse the repository at this point in the history
  • Loading branch information
andris-sevcenko committed Oct 14, 2019
1 parent 736682e commit 921f128
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 34 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- Fixed a bug where user email changes were going through email verification even if someone with permission to administrate users was making the change. ([#5088](https://github.com/craftcms/cms/issues/5088))
- Fixed a bug where it wasn’t possible to eager-load Matrix blocks that belong to a draft or revision. ([#5031](https://github.com/craftcms/cms/issues/5031))
- Fixed a bug where the `setup` command would think that Craft was installed when it wasn’t. ([#5093](https://github.com/craftcms/cms/issues/5093))
- Fixed an error that could occur when syncing the project config, that could occur if a Matrix block had been changed to something else. ([#4015](https://github.com/craftcms/cms/issues/4015))

## 3.3.9 - 2019-10-10

Expand Down
73 changes: 39 additions & 34 deletions src/services/Matrix.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,48 +360,53 @@ public function handleChangedBlockType(ConfigEvent $event)
$contentService->fieldColumnPrefix = 'field_' . $blockTypeRecord->handle . '_';
/** @var MatrixField $matrixField */
$matrixField = $fieldsService->getFieldById($blockTypeRecord->fieldId);
$contentService->contentTable = $matrixField->contentTable;
$fieldsService->oldFieldColumnPrefix = 'field_' . ($blockTypeRecord->getOldAttribute('handle') ?? $data['handle']) . '_';

$oldFields = $previousData['fields'] ?? [];
$newFields = $data['fields'] ?? [];
// Ignore it, if the parent field is not a Matrix field.
if ($matrixField instanceof MatrixField) {
$contentService->contentTable = $matrixField->contentTable;
$fieldsService->oldFieldColumnPrefix = 'field_' . ($blockTypeRecord->getOldAttribute('handle') ?? $data['handle']) . '_';

$oldFields = $previousData['fields'] ?? [];
$newFields = $data['fields'] ?? [];

// Remove fields that this block type no longer has
foreach ($oldFields as $fieldUid => $fieldData) {
if (!array_key_exists($fieldUid, $newFields)) {
$fieldsService->applyFieldDelete($fieldUid);
// Remove fields that this block type no longer has
foreach ($oldFields as $fieldUid => $fieldData) {
if (!array_key_exists($fieldUid, $newFields)) {
$fieldsService->applyFieldDelete($fieldUid);
}
}
}

// (Re)save all the fields that now exist for this block.
foreach ($newFields as $fieldUid => $fieldData) {
$fieldsService->applyFieldSave($fieldUid, $fieldData, 'matrixBlockType:' . $blockTypeUid);
}
// (Re)save all the fields that now exist for this block.
foreach ($newFields as $fieldUid => $fieldData) {
$fieldsService->applyFieldSave($fieldUid, $fieldData, 'matrixBlockType:' . $blockTypeUid);
}

// Refresh the schema cache
Craft::$app->getDb()->getSchema()->refresh();
// Refresh the schema cache
Craft::$app->getDb()->getSchema()->refresh();

$contentService->fieldContext = $originalFieldContext;
$contentService->fieldColumnPrefix = $originalFieldColumnPrefix;
$contentService->contentTable = $originalContentTable;
$fieldsService->oldFieldColumnPrefix = $originalOldFieldColumnPrefix;

if (!empty($data['fieldLayouts'])) {
// Save the field layout
$layout = FieldLayout::createFromConfig(reset($data['fieldLayouts']));
$layout->id = $blockTypeRecord->fieldLayoutId;
$layout->type = MatrixBlock::class;
$layout->uid = key($data['fieldLayouts']);
$fieldsService->saveLayout($layout);
$blockTypeRecord->fieldLayoutId = $layout->id;
} else if ($blockTypeRecord->fieldLayoutId) {
// Delete the field layout
$fieldsService->deleteLayoutById($blockTypeRecord->fieldLayoutId);
$blockTypeRecord->fieldLayoutId = null;
}

$contentService->fieldContext = $originalFieldContext;
$contentService->fieldColumnPrefix = $originalFieldColumnPrefix;
$contentService->contentTable = $originalContentTable;
$fieldsService->oldFieldColumnPrefix = $originalOldFieldColumnPrefix;

if (!empty($data['fieldLayouts'])) {
// Save the field layout
$layout = FieldLayout::createFromConfig(reset($data['fieldLayouts']));
$layout->id = $blockTypeRecord->fieldLayoutId;
$layout->type = MatrixBlock::class;
$layout->uid = key($data['fieldLayouts']);
$fieldsService->saveLayout($layout);
$blockTypeRecord->fieldLayoutId = $layout->id;
} else if ($blockTypeRecord->fieldLayoutId) {
// Delete the field layout
$fieldsService->deleteLayoutById($blockTypeRecord->fieldLayoutId);
$blockTypeRecord->fieldLayoutId = null;
// Save it
$blockTypeRecord->save(false);
}

// Save it
$blockTypeRecord->save(false);
$transaction->commit();
} catch (\Throwable $e) {
$transaction->rollBack();
Expand Down

0 comments on commit 921f128

Please sign in to comment.