diff --git a/src/base/NestedElementTrait.php b/src/base/NestedElementTrait.php index 0878a1e0aee..6d656c5761e 100644 --- a/src/base/NestedElementTrait.php +++ b/src/base/NestedElementTrait.php @@ -164,10 +164,26 @@ public function getPrimaryOwner(): ?ElementInterface return null; } + $criteria = [ + 'site' => '*', + 'preferSites' => [$this->siteId], + 'unique' => true, + 'status' => null, + 'drafts' => null, + 'provisionalDrafts' => null, + 'revisions' => null, + 'trashed' => null, + ]; + if (isset($this->id, $this->elementQueryResult)) { // Eager-load the primary owner for each of the elements in the result, // as we're probably going to end up needing them too - Craft::$app->getElements()->eagerLoadElements($this::class, $this->elementQueryResult, ['primaryOwner']); + Craft::$app->getElements()->eagerLoadElements($this::class, $this->elementQueryResult, [ + [ + 'path' => 'primaryOwner', + 'criteria' => $criteria, + ], + ]); } if (!isset($this->_primaryOwner) || $this->_primaryOwner === false) { @@ -177,17 +193,9 @@ public function getPrimaryOwner(): ?ElementInterface return null; } - $this->_primaryOwner = $ownerType::find() - ->id($primaryOwnerId) - ->site('*') - ->preferSites([$this->siteId]) - ->unique() - ->status(null) - ->drafts(null) - ->provisionalDrafts(null) - ->revisions(null) - ->trashed(null) - ->one() ?? false; + $query = $ownerType::find()->id($primaryOwnerId); + Craft::configure($query, $criteria); + $this->_primaryOwner = $query->one() ?? false; if (!$this->_primaryOwner) { throw new InvalidConfigException("Invalid owner ID: $primaryOwnerId"); @@ -239,10 +247,26 @@ public function getOwner(): ?ElementInterface return $this->getPrimaryOwner(); } + $criteria = [ + 'site' => '*', + 'preferSites' => [$this->siteId], + 'unique' => true, + 'status' => null, + 'drafts' => null, + 'provisionalDrafts' => null, + 'revisions' => null, + 'trashed' => null, + ]; + if (isset($this->id, $this->elementQueryResult)) { // Eager-load the owner for each of the elements in the result, // as we're probably going to end up needing them too - Craft::$app->getElements()->eagerLoadElements($this::class, $this->elementQueryResult, ['owner']); + Craft::$app->getElements()->eagerLoadElements($this::class, $this->elementQueryResult, [ + [ + 'path' => 'owner', + 'criteria' => $criteria, + ], + ]); } if (!isset($this->_owner) || $this->_owner === false) { @@ -252,17 +276,9 @@ public function getOwner(): ?ElementInterface return null; } - $this->_owner = $ownerType::find() - ->id($ownerId) - ->site('*') - ->preferSites([$this->siteId]) - ->unique() - ->status(null) - ->drafts(null) - ->provisionalDrafts(null) - ->revisions(null) - ->trashed(null) - ->one() ?? false; + $query = $ownerType::find()->id($ownerId); + Craft::configure($query, $criteria); + $this->_owner = $query->one() ?? false; if (!$this->_owner) { throw new InvalidConfigException("Invalid owner ID: $ownerId");