Skip to content

Commit ddb0149

Browse files
buchasreichel
andauthored
Fix regression bug for duplicate block rendering with getSortedChildren() (#4480)
* Fix regression bug for duplicate block rendering with getSortedChildren() * Fix code formatting via php-cs --------- Co-authored-by: Sven Reichel <github-sr@hotmail.com>
1 parent 1dabe2d commit ddb0149

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

app/code/core/Mage/Core/Block/Abstract.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,6 @@ public function setChild($alias, $block)
465465

466466
$block->setParentBlock($this);
467467
$block->setBlockAlias($alias);
468-
$this->unsetChild($alias);
469468
$this->_children[$alias] = $block;
470469
return $this;
471470
}
@@ -705,6 +704,11 @@ public function insert($block, $siblingName = '', $after = false, $alias = '')
705704
$this->setChild($name, $block);
706705
}
707706

707+
$existingKey = array_search($name, $this->_sortedChildren);
708+
if ($existingKey !== false) {
709+
array_splice($this->_sortedChildren, $existingKey, 1);
710+
}
711+
708712
if ($siblingName === '') {
709713
if ($after) {
710714
$this->_sortedChildren[] = $name;

tests/unit/Mage/Core/Block/Text/ListTest.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,33 @@ public function testUniqueBlockNameOrdering(): void
9090
$parentBlock->insert($childBlockC, 'child_b', true);
9191

9292
$childBlockA = $layout->createBlock('core/text', 'child_a')->setText('A');
93-
$parentBlock->insert($childBlockC, 'child_b', false);
93+
$parentBlock->insert($childBlockA, 'child_b', false);
9494

9595
$childBlockB = $layout->createBlock('core/text', 'child_b')->setText('B');
96-
$parentBlock->insert($childBlockC, 'child_a', true);
96+
$parentBlock->insert($childBlockB, 'child_a', true);
9797

9898
$parentBlock->unsetChild('child_a');
9999
$parentBlock->unsetChild('child_b');
100100

101101
$this->assertSame('CD', $parentBlock->toHtml());
102102
}
103+
104+
public function testSortInstructionsAfterReplaceChild()
105+
{
106+
$layout = Mage::getModel('core/layout');
107+
108+
$parentBlock = $layout->createBlock('core/text_list', 'parent');
109+
110+
$childBlockA = $layout->createBlock('core/text', 'target_block')->setText('A');
111+
$parentBlock->insert($childBlockA, '', false, 'child');
112+
113+
$childBlockB = $layout->createBlock('core/text', 'target_block')->setText('B');
114+
115+
// Replacing the block but keeping its order within the parent
116+
$layout->unsetBlock('target_block');
117+
$layout->setBlock('target_block', $childBlockB);
118+
$parentBlock->setChild('child', $childBlockB);
119+
120+
$this->assertSame('B', $parentBlock->toHtml());
121+
}
103122
}

0 commit comments

Comments
 (0)