diff --git a/CHANGELOG.md b/CHANGELOG.md index abdb92fc39e..ba38756f473 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/base/Element.php b/src/base/Element.php index 9d685002576..af999b063f7 100644 --- a/src/base/Element.php +++ b/src/base/Element.php @@ -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); } diff --git a/src/services/Elements.php b/src/services/Elements.php index 33ae18e4ed1..9e94790ea1d 100644 --- a/src/services/Elements.php +++ b/src/services/Elements.php @@ -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 @@ -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); @@ -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 ); } }