diff --git a/CHANGELOG-WIP.md b/CHANGELOG-WIP.md index 92861accb63..93d8fa4a9e7 100644 --- a/CHANGELOG-WIP.md +++ b/CHANGELOG-WIP.md @@ -4,4 +4,8 @@ - Improved the accessibility of Checkboxes and Radio Buttons fields that allow custom options. ([#16080](https://github.com/craftcms/cms/pull/16080)) ### Development +- Added the ability to specify a nested element’s owner element type. ([#16133](https://github.com/craftcms/cms/pull/16133)) - Added support for fallback element partial templates, e.g. `_partials/entry.twig` as opposed to `_partials/entry/typeHandle.twig`. ([#16125](https://github.com/craftcms/cms/pull/16125)) + +### Extensibility +- Added `craft\base\NestedElementTrait::$ownerElementType`. diff --git a/src/base/NestedElementTrait.php b/src/base/NestedElementTrait.php index f0a7856ca48..a54be23fd32 100644 --- a/src/base/NestedElementTrait.php +++ b/src/base/NestedElementTrait.php @@ -29,6 +29,12 @@ */ trait NestedElementTrait { + /** + * @var string|null The owner element type. Used if the nested element is only every owner by one type of element. + * @since 5.6.0 + */ + public ?string $ownerElementType = null; + /** * @inheritdoc */ @@ -154,7 +160,7 @@ public function getPrimaryOwner(): ?ElementInterface return null; } - $this->_primaryOwner = Craft::$app->getElements()->getElementById($primaryOwnerId, null, $this->siteId, [ + $this->_primaryOwner = Craft::$app->getElements()->getElementById($primaryOwnerId, $this->ownerElementType, $this->siteId, [ 'trashed' => null, ]) ?? false; if (!$this->_primaryOwner) { @@ -206,7 +212,7 @@ public function getOwner(): ?ElementInterface return $this->getPrimaryOwner(); } - $this->_owner = Craft::$app->getElements()->getElementById($ownerId, null, $this->siteId, [ + $this->_owner = Craft::$app->getElements()->getElementById($ownerId, $this->ownerElementType, $this->siteId, [ 'trashed' => null, ]) ?? false; if (!$this->_owner) { @@ -306,7 +312,7 @@ private function saveOwnership(bool $isNew, string $elementTable, string $fieldI if (!$this->saveOwnership || !isset($this->fieldId)) { return; } - + $ownerId = $this->getOwnerId(); if (!$ownerId) { return;