Skip to content

Commit

Permalink
Reset Matrix & relation field values after saving a new element
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkelly committed May 15, 2019
1 parent 10d027d commit 0a88191
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Fixed
- Fixed a bug where slugs could get double-hyphenated. ([#4266](https://github.com/craftcms/cms/issues/4266))
- Fixed an error that would occur when installing Craft if the `allowAdminChanges` config setting was disabled. ([#4267](https://github.com/craftcms/cms/issues/4267))
- Fixed a bug where Matrix fields would return the wrong set of Matrix blocks on new or duplicated elements, immediately after they were saved.

## 3.1.27 - 2019-05-14

Expand Down
5 changes: 5 additions & 0 deletions src/fields/BaseRelationField.php
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,11 @@ public function afterElementSave(ElementInterface $element, bool $isNew)

/** @var int|int[]|false|null $targetIds */
Craft::$app->getRelations()->saveRelations($this, $element, $targetIds);

// Reset the field value if this is a new element
if ($isNew) {
$element->setFieldValue($this->handle, null);
}
}

parent::afterElementSave($element, $isNew);
Expand Down
5 changes: 5 additions & 0 deletions src/services/Matrix.php
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,11 @@ public function saveField(MatrixField $field, ElementInterface $owner)
throw $e;
}

// Reset the field value if this is a new element
if ($owner->duplicateOf || !$query->ownerId) {
$owner->setFieldValue($field->handle, null);
}

// Tell the browser to collapse any new block IDs
if (!Craft::$app->getRequest()->getIsConsoleRequest() && !Craft::$app->getResponse()->isSent && !empty($collapsedBlockIds)) {
Craft::$app->getSession()->addAssetBundleFlash(MatrixAsset::class);
Expand Down

0 comments on commit 0a88191

Please sign in to comment.