Replicated layout builder blocks don't always have the correct order

Created on 10 October 2023, 9 months ago

Problem/Motivation

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;
  }

Steps to reproduce

  1. Create a node on a layout-builder enabled (with overrides) content type and edit its layout
  2. Add block to region of a section
  3. Add another block to that same region of the section, so it appears below the first
  4. Drag the 2nd block above the 1st
  5. Save the layout
  6. Replicate the node
  7. Observe that on the replicated node, the order of the blocks is not the same as the original

Proposed resolution

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.

Remaining tasks

User interface changes

API changes

Data model changes

πŸ› Bug report
Status

Needs review

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States bkosborne New Jersey, USA

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.69.0 2024