- Issue created by @bkosborne
- last update
over 1 year ago 7 pass - @bkosborne opened merge request.
- Status changed to Needs review
over 1 year ago 2:20pm 10 October 2023
When this module replicates the layout section "components" in a layout builder layout, it does so in such a way that the original order of the blocks may be lost.
A layout builder section stores its components in an array. The order they appear in this array is actually not really relevant to the output order. Instead, each component has a weight property that controls that (since during rendering, the array is converted to a render array, these weights are used). When this module replicates a section, it processes one component at a time, removing it from the section, replicating it, then re-appending to the section using Section::appendComponent. This method resets the weight of the component, losing the original value:
/**
* Appends a component to the end of a region.
*
* @param \Drupal\layout_builder\SectionComponent $component
* The component being appended.
*
* @return $this
*/
public function appendComponent(SectionComponent $component) {
$component->setWeight($this->getNextHighestWeight($component->getRegion()));
$this->setComponent($component);
return $this;
}
Ideally we could just use the setComponent method of Section, but it's protected.
Instead, I think that we can just order the components by their weight before replicating them.
Needs review
1.0
Code