Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability for nested element's to specify their owner's element type #16133

Open
wants to merge 2 commits into
base: 5.6
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
12 changes: 9 additions & 3 deletions src/base/NestedElementTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down