Skip to content

Commit

Permalink
Fixed owner/primaryOwner eager loading for drafts
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkelly committed Jan 27, 2025
1 parent fc70f80 commit 9656150
Showing 1 changed file with 40 additions and 24 deletions.
64 changes: 40 additions & 24 deletions src/base/NestedElementTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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");
Expand Down Expand Up @@ -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) {
Expand All @@ -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");
Expand Down

0 comments on commit 9656150

Please sign in to comment.