Skip to content

Commit

Permalink
ElementInterface::getRootOwner()
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkelly committed Aug 13, 2024
1 parent cbb4002 commit b1e4385
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### Extensibility
- Added `craft\base\ApplicationTrait::getEnvId()`. ([#15313](https://github.com/craftcms/cms/issues/15313))
- Added `craft\base\ElementInterface::getRootOwner()`.
- Deprecated `craft\helpers\ElementHelper::rootElement()`. `craft\base\ElementInterface::getRootOwner()` should be used instead.

### System
- MySQL mutex locks and PHP session names are now namespaced using the application ID combined with the environment name. ([#15313](https://github.com/craftcms/cms/issues/15313))
14 changes: 14 additions & 0 deletions src/base/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -3479,6 +3479,20 @@ public function getStatus(): ?string
return self::STATUS_ENABLED;
}

/**
* @inheritdoc
*/
public function getRootOwner(): ElementInterface
{
if ($this instanceof BlockElementInterface) {
$owner = $this->getOwner();
if ($owner) {
return $owner->getRootOwner();
}
}
return $this;
}

/**
* @inheritdoc
* @since 3.5.0
Expand Down
8 changes: 8 additions & 0 deletions src/base/ElementInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,14 @@ public function setEnabledForSite(array|bool $enabledForSite): void;
*/
public function getStatus(): ?string;

/**
* Returns the root owner element.
*
* @return self
* @since 4.12.0
*/
public function getRootOwner(): self;

/**
* Returns the same element in other locales.
*
Expand Down
2 changes: 1 addition & 1 deletion src/fields/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ protected function tableAttributeHtml(Collection $elements): string
public function afterElementSave(ElementInterface $element, bool $isNew): void
{
// No special treatment for revisions
$rootElement = ElementHelper::rootElement($element);
$rootElement = $element->getRootOwner();
if (!$rootElement->getIsRevision()) {
// Figure out what we're working with and set up some initial variables.
$isCanonical = $rootElement->getIsCanonical();
Expand Down
17 changes: 5 additions & 12 deletions src/helpers/ElementHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,21 +432,16 @@ public static function editableSiteIdsForElement(ElementInterface $element): arr
}

/**
* Returns the root element of a given element.
* Returns the root owner of a given element.
*
* @param ElementInterface $element
* @return ElementInterface
* @since 3.2.0
* @deprecated in 4.12.0. Use [[ElementInterface::getRootOwner()]] instead.
*/
public static function rootElement(ElementInterface $element): ElementInterface
{
if ($element instanceof BlockElementInterface) {
$owner = $element->getOwner();
if ($owner) {
return static::rootElement($owner);
}
}
return $element;
return $element->getRootOwner();
}

/**
Expand Down Expand Up @@ -530,8 +525,7 @@ public static function isDraftOrRevision(ElementInterface $element): bool
*/
public static function isCanonical(ElementInterface $element): bool
{
$root = static::rootElement($element);
return $root->getIsCanonical();
return $element->getRootOwner()->getIsCanonical();
}

/**
Expand All @@ -543,8 +537,7 @@ public static function isCanonical(ElementInterface $element): bool
*/
public static function isDerivative(ElementInterface $element): bool
{
$root = static::rootElement($element);
return $root->getIsDerivative();
return $element->getRootOwner()->getIsDerivative();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/services/Elements.php
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ public function invalidateCachesForElement(ElementInterface $element): void
];

try {
$rootElement = ElementHelper::rootElement($element);
$rootElement = $element->getRootOwner();
} catch (Throwable) {
$rootElement = $element;
}
Expand Down

0 comments on commit b1e4385

Please sign in to comment.