Skip to content

Commit

Permalink
Avoid eager-loading alias conflicts
Browse files Browse the repository at this point in the history
Fixes #14057
  • Loading branch information
brandonkelly committed Dec 27, 2023
1 parent b798276 commit 6f1653f
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 13 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@
- Added `craft\events\BulkOpEvent`. ([#14032](https://github.com/craftcms/cms/pull/14032))
- Added `craft\events\DefineEntryTypesForFieldEvent`.
- Added `craft\events\DefineFieldHtmlEvent::$inline`.
- Added `craft\events\SetEagerLoadedElementsEvent::$plan`.
- Added `craft\fieldlayoutelements\BaseField::$includeInCards`.
- Added `craft\fieldlayoutelements\BaseField::$providesThumbs`.
- Added `craft\fieldlayoutelements\BaseField::previewHtml()`.
Expand Down Expand Up @@ -299,6 +300,7 @@
- `craft\base\ElementInterface::getEagerLoadedElementCount()` can now return `null` for counts that haven’t been eager-loaded yet.
- `craft\base\ElementInterface::getEagerLoadedElements` now has an `ElementCollection|null` return type, rather than `Collection|null`.
- `craft\base\ElementInterface::indexHtml()``$showCheckboxes` argument is now `$selectable`, and it now has a `$sortable` argument.
- `craft\base\ElementInterface::setEagerLoadedElements()` now has a `$plan` argument, which will be set to the eager-loading plan.
- `craft\base\ElementInterface::setParent()` no longer needs to specify a default value for the `parent` argument.
- `craft\base\ElementInterface::setRevisionCreatorId()` no longer needs to specify a default value for the `creatorId` argument.
- `craft\base\ElementInterface::setRevisionNotes()` no longer needs to specify a default value for the `notes` argument.
Expand Down Expand Up @@ -479,3 +481,4 @@
- Improved the performance of autosaves for elements with newly-created Matrix entries.
- Slugs are no longer required for elements that don’t have a URI format that contains `slug`.
- Fixed a bug where multi-site element queries weren’t scoring elements on a per-site basis. ([#13801](https://github.com/craftcms/cms/discussions/13801))
- Fixed an error that could occur if eager-loading aliases conflicted with native eager-loading handles, such as `author`. ([#14057](https://github.com/craftcms/cms/issues/14057))
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
## Unreleased

- Fixed a bug where section preview targets weren’t being respected in Craft Pro. ([#14050](https://github.com/craftcms/cms/issues/14050))
- Added `craft\events\SetEagerLoadedElementsEvent::$plan`.
- `craft\base\ElementInterface::setEagerLoadedElements()` now has a `$plan` argument, which will be set to the eager-loading plan.
- Fixed an error that could occur if eager-loading aliases conflicted with native eager-loading handles, such as `author`. ([#14057](https://github.com/craftcms/cms/issues/14057))

## 5.0.0-alpha.3 - 2023-12-21

Expand Down
6 changes: 4 additions & 2 deletions src/base/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use craft\elements\actions\View as ViewAction;
use craft\elements\conditions\ElementCondition;
use craft\elements\conditions\ElementConditionInterface;
use craft\elements\db\EagerLoadPlan;
use craft\elements\db\ElementQuery;
use craft\elements\db\ElementQueryInterface;
use craft\elements\ElementCollection;
Expand Down Expand Up @@ -4888,9 +4889,9 @@ public function getEagerLoadedElements(string $handle): ?ElementCollection
/**
* @inheritdoc
*/
public function setEagerLoadedElements(string $handle, array $elements): void
public function setEagerLoadedElements(string $handle, array $elements, EagerLoadPlan $plan): void
{
switch ($handle) {
switch ($plan->handle) {
case 'parent':
$this->_parent = $elements[0] ?? false;
break;
Expand All @@ -4916,6 +4917,7 @@ public function setEagerLoadedElements(string $handle, array $elements): void
$event = new SetEagerLoadedElementsEvent([
'handle' => $handle,
'elements' => $elements,
'plan' => $plan,
]);
$this->trigger(self::EVENT_SET_EAGER_LOADED_ELEMENTS, $event);
if (!$event->handled) {
Expand Down
4 changes: 3 additions & 1 deletion src/base/ElementInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use craft\behaviors\CustomFieldBehavior;
use craft\elements\conditions\ElementConditionInterface;
use craft\elements\db\EagerLoadPlan;
use craft\elements\db\ElementQuery;
use craft\elements\db\ElementQueryInterface;
use craft\elements\ElementCollection;
Expand Down Expand Up @@ -1544,8 +1545,9 @@ public function getEagerLoadedElements(string $handle): ?ElementCollection;
*
* @param string $handle The handle that was used to eager-load the elements
* @param self[] $elements The eager-loaded elements
* @param EagerLoadPlan $plan The eager-loading plan
*/
public function setEagerLoadedElements(string $handle, array $elements): void;
public function setEagerLoadedElements(string $handle, array $elements, EagerLoadPlan $plan): void;

/**
* Sets whether the given eager-loaded element handles were eager-loaded lazily.
Expand Down
7 changes: 4 additions & 3 deletions src/elements/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use craft\elements\conditions\assets\AssetCondition;
use craft\elements\conditions\ElementConditionInterface;
use craft\elements\db\AssetQuery;
use craft\elements\db\EagerLoadPlan;
use craft\elements\db\ElementQueryInterface;
use craft\enums\MenuItemType;
use craft\errors\AssetException;
Expand Down Expand Up @@ -321,14 +322,14 @@ public static function eagerLoadingMap(array $sourceElements, string $handle): a
* @inheritdoc
* @since 3.4.0
*/
public function setEagerLoadedElements(string $handle, array $elements): void
public function setEagerLoadedElements(string $handle, array $elements, EagerLoadPlan $plan): void
{
if ($handle === 'uploader') {
if ($plan->handle === 'uploader') {
/** @var User|null $uploader */
$uploader = $elements[0] ?? null;
$this->setUploader($uploader);
} else {
parent::setEagerLoadedElements($handle, $elements);
parent::setEagerLoadedElements($handle, $elements, $plan);
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/elements/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use craft\elements\conditions\entries\EntryCondition;
use craft\elements\conditions\entries\SectionConditionRule;
use craft\elements\conditions\entries\TypeConditionRule;
use craft\elements\db\EagerLoadPlan;
use craft\elements\db\ElementQuery;
use craft\elements\db\ElementQueryInterface;
use craft\elements\db\EntryQuery;
Expand Down Expand Up @@ -1706,12 +1707,12 @@ public function getGqlTypeName(): string
/**
* @inheritdoc
*/
public function setEagerLoadedElements(string $handle, array $elements): void
public function setEagerLoadedElements(string $handle, array $elements, EagerLoadPlan $plan): void
{
if ($handle === 'author') {
if ($plan->handle === 'author') {
$this->_author = $elements[0] ?? false;
} else {
parent::setEagerLoadedElements($handle, $elements);
parent::setEagerLoadedElements($handle, $elements, $plan);
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/elements/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use craft\elements\conditions\ElementConditionInterface;
use craft\elements\conditions\users\UserCondition;
use craft\elements\db\AddressQuery;
use craft\elements\db\EagerLoadPlan;
use craft\elements\db\ElementQueryInterface;
use craft\elements\db\UserQuery;
use craft\enums\MenuItemType;
Expand Down Expand Up @@ -2043,14 +2044,14 @@ private function _validateLocale(?string $locale, bool $checkAllLocales): ?strin
/**
* @inheritdoc
*/
public function setEagerLoadedElements(string $handle, array $elements): void
public function setEagerLoadedElements(string $handle, array $elements, EagerLoadPlan $plan): void
{
if ($handle === 'photo') {
if ($plan->handle === 'photo') {
/** @var Asset|null $photo */
$photo = $elements[0] ?? null;
$this->setPhoto($photo);
} else {
parent::setEagerLoadedElements($handle, $elements);
parent::setEagerLoadedElements($handle, $elements, $plan);
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/events/SetEagerLoadedElementsEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace craft\events;

use craft\base\ElementInterface;
use craft\elements\db\EagerLoadPlan;
use yii\base\Event;

/**
Expand All @@ -27,4 +28,10 @@ class SetEagerLoadedElementsEvent extends Event
* @param ElementInterface[] $elements The eager-loaded elements
*/
public array $elements;

/**
* @param EagerLoadPlan $plan The eager-loading plan
* @since 5.0.0
*/
public EagerLoadPlan $plan;
}
2 changes: 1 addition & 1 deletion src/services/Elements.php
Original file line number Diff line number Diff line change
Expand Up @@ -3226,7 +3226,7 @@ private function _eagerLoadElementsInternal(string $elementType, array $elements
}
}

$sourceElement->setEagerLoadedElements($planHandle, $targetElementsForSource);
$sourceElement->setEagerLoadedElements($planHandle, $targetElementsForSource, $plan);
$sourceElement->setLazyEagerLoadedElements($planHandle, $plan->lazy);

if ($plan->count) {
Expand Down

0 comments on commit 6f1653f

Please sign in to comment.