Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eager loading - filter out elements, if field is not part of its layout #12646

Merged
merged 3 commits into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

- Fixed a PHP error that occurred after performing a Composer action within Craft.
- Fixed a bug where element attributes weren’t getting eager-loaded. ([#12646](https://github.com/craftcms/cms/pull/12646), [#12645](https://github.com/craftcms/cms/issues/12645))

## 3.7.65 - 2023-02-07

Expand Down
9 changes: 9 additions & 0 deletions src/base/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,15 @@ public static function eagerLoadingMap(array $sourceElements, string $handle)
// (Leave it up to the extended class to set the field context, if it shouldn't be 'global')
$field = Craft::$app->getFields()->getFieldByHandle($handle);
if ($field && $field instanceof EagerLoadingFieldInterface) {
// filter out elements, if field is not part of its layout
// https://github.com/craftcms/cms/issues/12539
$sourceElements = array_values(
array_filter($sourceElements, function($sourceElement) use ($handle) {
$fieldLayout = $sourceElement->getFieldLayout();
return !$fieldLayout || $fieldLayout->getFieldByHandle($handle) !== null;
})
);

return $field->getEagerLoadingMap($sourceElements);
}

Expand Down
26 changes: 2 additions & 24 deletions src/services/Elements.php
Original file line number Diff line number Diff line change
Expand Up @@ -2258,9 +2258,8 @@ public function eagerLoadElements(string $elementType, array $elements, $with)
* @param string $elementType
* @param array $elementsBySite
* @param EagerLoadPlan[] $with
* @param bool $checkIfInLayout
*/
private function _eagerLoadElementsInternal(string $elementType, array $elementsBySite, array $with, bool $checkIfInLayout = true)
private function _eagerLoadElementsInternal(string $elementType, array $elementsBySite, array $with)
{
foreach ($elementsBySite as $siteId => $elements) {
// In case the elements were
Expand All @@ -2283,26 +2282,6 @@ private function _eagerLoadElementsInternal(string $elementType, array $elements
$filteredElements = $elements;
}

// filter out fields that are not part of this layout
// https://github.com/craftcms/cms/issues/12539
if ($checkIfInLayout) {
$filteredElements = array_values(
array_filter($filteredElements, function($filteredElement) use ($plan) {
$fieldLayout = $filteredElement->getFieldLayout();
$fieldHandle = preg_replace('/(\w+)([\.|:]\S+)/', '$1', $plan->handle, 1);
return (
!$fieldLayout ||
$fieldLayout->getFieldByHandle($fieldHandle) !== null
);
})
);

// if we have nothing left in the $filteredElements, move on to the next iteration
if (empty($filteredElements)) {
continue;
}
}

// Get the eager-loading map from the source element type
/** @var ElementInterface|string $elementType */
$map = $elementType::eagerLoadingMap($filteredElements, $plan->handle);
Expand Down Expand Up @@ -2460,8 +2439,7 @@ private function _eagerLoadElementsInternal(string $elementType, array $elements
$this->_eagerLoadElementsInternal(
$map['elementType'],
array_map('array_values', $targetElements),
$plan->nested,
false
$plan->nested
);
}
}
Expand Down