diff --git a/CHANGELOG.md b/CHANGELOG.md index 198d330affd..b6d95dfa0f9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - `craft\behaviors\SessionBehavior::setSuccess()` and `getSuccess()` use the `success` flash key now, rather than `notice`. ([#14345](https://github.com/craftcms/cms/pull/14345)) - Exception response data no longer includes an `error` key with the exception message. `message` should be used instead. ([#14346](https://github.com/craftcms/cms/pull/14346)) - Fixed a bug where read/write splitting was always getting disabled for GraphQL POST requests. ([#14324](https://github.com/craftcms/cms/issues/14324)) +- Fixed an error that occurred when adding a new block to a Matrix field with an overridden handle. ([#14350](https://github.com/craftcms/cms/issues/14350)) ## 5.0.0-beta.1 - 2024-02-08 diff --git a/src/controllers/MatrixController.php b/src/controllers/MatrixController.php index c657222dce8..9dfdbf94853 100644 --- a/src/controllers/MatrixController.php +++ b/src/controllers/MatrixController.php @@ -79,7 +79,13 @@ public function actionCreateEntry(): Response $siteId = $this->request->getRequiredBodyParam('siteId'); $namespace = $this->request->getRequiredBodyParam('namespace'); - $field = Craft::$app->getFields()->getFieldById($fieldId); + $elementsService = Craft::$app->getElements(); + $owner = $elementsService->getElementById($ownerId, $ownerElementType, $siteId); + if (!$owner) { + throw new BadRequestHttpException("Invalid owner ID, element type, or site ID."); + } + + $field = $owner->getFieldLayout()?->getFieldById($fieldId); if (!$field instanceof Matrix) { throw new BadRequestHttpException("Invalid Matrix field ID: $fieldId"); } @@ -94,12 +100,6 @@ public function actionCreateEntry(): Response throw new BadRequestHttpException("Invalid site ID: $siteId"); } - $elementsService = Craft::$app->getElements(); - $owner = $elementsService->getElementById($ownerId, $ownerElementType, $siteId); - if (!$owner) { - throw new BadRequestHttpException("Invalid owner ID, element type, or site ID."); - } - /** @var Entry $entry */ $entry = Craft::createObject([ 'class' => Entry::class,