Skip to content

Commit 6736983

Browse files
committed
Allow sorting of children via block argument "sort_order"
1 parent ca7686a commit 6736983

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

Util/Block/ChildRenderer.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,31 @@
88

99
class ChildRenderer extends AbstractRenderer
1010
{
11+
public function all(
12+
AbstractBlock $parentBlock
13+
): string {
14+
$html = '';
15+
$childNames = $parentBlock->getChildNames();
16+
$children = [];
17+
18+
foreach ($childNames as $childName) {
19+
$childBlock = $parentBlock->getLayout()->getBlock($childName);
20+
if (false === $childBlock instanceof AbstractBlock) {
21+
continue;
22+
}
23+
24+
$children[] = $childBlock;
25+
}
26+
27+
$sortedChildren = $this->sortBlocks($children);
28+
29+
foreach ($sortedChildren as $sortedChild) {
30+
$html .= $sortedChild->toHtml();
31+
}
32+
33+
return $html;
34+
}
35+
1136
public function get(
1237
AbstractBlock $ancestorBlock,
1338
string $blockAlias,
@@ -49,4 +74,13 @@ public function html(
4974
return '';
5075
}
5176
}
77+
78+
private function sortBlocks(array $blocks): array
79+
{
80+
usort($blocks, function (AbstractBlock $blockA, AbstractBlock $blockB) {
81+
return (int)$blockA->getSortOrder() <=> (int)$blockB->getSortOrder();
82+
});
83+
84+
return $blocks;
85+
}
5286
}

0 commit comments

Comments
 (0)