From b1e4385de04b01d52ebdf28df58af1920296099a Mon Sep 17 00:00:00 2001 From: brandonkelly Date: Tue, 13 Aug 2024 15:21:51 -0700 Subject: [PATCH] ElementInterface::getRootOwner() --- CHANGELOG-WIP.md | 2 ++ src/base/Element.php | 14 ++++++++++++++ src/base/ElementInterface.php | 8 ++++++++ src/fields/Assets.php | 2 +- src/helpers/ElementHelper.php | 17 +++++------------ src/services/Elements.php | 2 +- 6 files changed, 31 insertions(+), 14 deletions(-) diff --git a/CHANGELOG-WIP.md b/CHANGELOG-WIP.md index 0c4bc352bef..2444ea02a16 100644 --- a/CHANGELOG-WIP.md +++ b/CHANGELOG-WIP.md @@ -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)) diff --git a/src/base/Element.php b/src/base/Element.php index 542dfa9c50a..adb252bd07c 100644 --- a/src/base/Element.php +++ b/src/base/Element.php @@ -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 diff --git a/src/base/ElementInterface.php b/src/base/ElementInterface.php index 44cdb96384d..e1019fd84d7 100644 --- a/src/base/ElementInterface.php +++ b/src/base/ElementInterface.php @@ -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. * diff --git a/src/fields/Assets.php b/src/fields/Assets.php index 2ef87beee4c..efc9cd8556a 100644 --- a/src/fields/Assets.php +++ b/src/fields/Assets.php @@ -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(); diff --git a/src/helpers/ElementHelper.php b/src/helpers/ElementHelper.php index 7980b0dc9a4..05b7ccf335d 100644 --- a/src/helpers/ElementHelper.php +++ b/src/helpers/ElementHelper.php @@ -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(); } /** @@ -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(); } /** @@ -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(); } /** diff --git a/src/services/Elements.php b/src/services/Elements.php index 0b3fb4b9bd2..0166958ea54 100644 --- a/src/services/Elements.php +++ b/src/services/Elements.php @@ -775,7 +775,7 @@ public function invalidateCachesForElement(ElementInterface $element): void ]; try { - $rootElement = ElementHelper::rootElement($element); + $rootElement = $element->getRootOwner(); } catch (Throwable) { $rootElement = $element; }