Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Section.php

function Section::insertComponent

Inserts a component at a specified delta.

Parameters

int $delta: The zero-based delta in which to insert the component.

\Drupal\layout_builder\SectionComponent $new_component: The component being inserted.

Return value

$this

Throws

\OutOfBoundsException Thrown when the specified delta is invalid.

1 call to Section::insertComponent()
Section::insertAfterComponent in core/modules/layout_builder/src/Section.php
Inserts a component after a specified existing component.

File

core/modules/layout_builder/src/Section.php, line 314

Class

Section
Provides a domain object for layout sections.

Namespace

Drupal\layout_builder

Code

public function insertComponent($delta, SectionComponent $new_component) {
    $components = $this->getComponentsByRegion($new_component->getRegion());
    $count = count($components);
    if ($delta > $count) {
        throw new \OutOfBoundsException(sprintf('Invalid delta "%s" for the "%s" component', $delta, $new_component->getUuid()));
    }
    // If the delta is the end of the list, append the component instead.
    if ($delta === $count) {
        return $this->appendComponent($new_component);
    }
    // Find the weight of the component that exists at the specified delta.
    $weight = array_values($components)[$delta]->getWeight();
    $this->setComponent($new_component->setWeight($weight++));
    // Increase the weight of every subsequent component.
    foreach (array_slice($components, $delta) as $component) {
        $component->setWeight($weight++);
    }
    return $this;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal