Skip to content

Commit 46c2bdf

Browse files
committed
Fixed #16327
1 parent 823a552 commit 46c2bdf

2 files changed

Lines changed: 44 additions & 30 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- Fixed a bug where elements’ `getPrev()` and `getNext()` methods could cause duplicate queries. ([#16329](https://github.com/craftcms/cms/discussions/16329))
66
- Fixed a bug where assets that were shorter than the preview thumb container weren’t getting vertically centered within it.
77
- Fixed a bug where it was possible to set a focal point on SVGs, even though focal points on SVGs aren’t supported. ([#16258](https://github.com/craftcms/cms/issues/16258))
8+
- Fixed a bug where `ancestors`, `children`, `descendants`, and `parent` eager-loading wasn’t working for previewed elements. ([#16327](https://github.com/craftcms/cms/issues/16327))
89

910
## 4.13.6 - 2024-12-10
1011

src/base/Element.php

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,21 +1427,7 @@ public static function eagerLoadingMap(array $sourceElements, string $handle): a
14271427
*/
14281428
private static function _mapDescendants(array $sourceElements, bool $children): ?array
14291429
{
1430-
// Get the source element IDs
1431-
$sourceElementIds = array_map(fn(ElementInterface $element) => $element->id, $sourceElements);
1432-
1433-
// Get the structure data for these elements
1434-
$selectColumns = ['structureId', 'elementId', 'lft', 'rgt'];
1435-
1436-
if ($children) {
1437-
$selectColumns[] = 'level';
1438-
}
1439-
1440-
$elementStructureData = (new Query())
1441-
->select($selectColumns)
1442-
->from([Table::STRUCTUREELEMENTS])
1443-
->where(['elementId' => $sourceElementIds])
1444-
->all();
1430+
$elementStructureData = self::_structureDataForElements($sourceElements, $children);
14451431

14461432
if (empty($elementStructureData)) {
14471433
return null;
@@ -1513,21 +1499,7 @@ private static function _mapDescendants(array $sourceElements, bool $children):
15131499
*/
15141500
private static function _mapAncestors(array $sourceElements, bool $parents): ?array
15151501
{
1516-
// Get the source element IDs
1517-
$sourceElementIds = array_map(fn(ElementInterface $element) => $element->id, $sourceElements);
1518-
1519-
// Get the structure data for these elements
1520-
$selectColumns = ['structureId', 'elementId', 'lft', 'rgt'];
1521-
1522-
if ($parents) {
1523-
$selectColumns[] = 'level';
1524-
}
1525-
1526-
$elementStructureData = (new Query())
1527-
->select($selectColumns)
1528-
->from([Table::STRUCTUREELEMENTS])
1529-
->where(['elementId' => $sourceElementIds])
1530-
->all();
1502+
$elementStructureData = self::_structureDataForElements($sourceElements, $parents);
15311503

15321504
if (empty($elementStructureData)) {
15331505
return null;
@@ -1595,6 +1567,47 @@ private static function _mapAncestors(array $sourceElements, bool $parents): ?ar
15951567
];
15961568
}
15971569

1570+
/**
1571+
* @param ElementInterface[] $elements
1572+
* @return array
1573+
*/
1574+
private static function _structureDataForElements(array $elements, bool $withLevel): array
1575+
{
1576+
$data = [];
1577+
$fetchDataForIds = [];
1578+
1579+
foreach ($elements as $element) {
1580+
if (isset($element->structureId, $element->lft, $element->rgt, $element->level)) {
1581+
$data[] = [
1582+
'structureId' => $element->structureId,
1583+
'elementId' => $element->id,
1584+
'lft' => $element->lft,
1585+
'rgt' => $element->rgt,
1586+
'level' => $element->level,
1587+
];
1588+
} else {
1589+
$fetchDataForIds[] = $element->id;
1590+
}
1591+
}
1592+
1593+
if (!empty($fetchDataForIds)) {
1594+
// Get the structure data for these elements
1595+
$selectColumns = ['structureId', 'elementId', 'lft', 'rgt'];
1596+
1597+
if ($withLevel) {
1598+
$selectColumns[] = 'level';
1599+
}
1600+
1601+
array_push($data, ...(new Query())
1602+
->select($selectColumns)
1603+
->from([Table::STRUCTUREELEMENTS])
1604+
->where(['elementId' => $fetchDataForIds])
1605+
->all());
1606+
}
1607+
1608+
return $data;
1609+
}
1610+
15981611
/**
15991612
* Returns an eager-loading map for the source elements in other locales.
16001613
*

0 commit comments

Comments
 (0)