You are here

public function SectionListTrait::insertSection in Drupal 10

Same name and namespace in other branches
  1. 9 core/modules/layout_builder/src/SectionListTrait.php \Drupal\layout_builder\SectionListTrait::insertSection()

File

core/modules/layout_builder/src/SectionListTrait.php, line 78

Class

SectionListTrait
Provides a trait for maintaining a list of sections.

Namespace

Drupal\layout_builder

Code

public function insertSection($delta, Section $section) {

  // Clear the section list if there is currently a blank section.
  if ($this
    ->hasBlankSection()) {
    $this
      ->removeAllSections();
  }
  if ($this
    ->hasSection($delta)) {

    // @todo Use https://www.drupal.org/node/66183 once resolved.
    $start = array_slice($this
      ->getSections(), 0, $delta);
    $end = array_slice($this
      ->getSections(), $delta);
    $this
      ->setSections(array_merge($start, [
      $section,
    ], $end));
  }
  else {
    $this
      ->appendSection($section);
  }
  return $this;
}