public function Section::insertComponent in Drupal 8
Same name and namespace in other branches
- 9 core/modules/layout_builder/src/Section.php \Drupal\layout_builder\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 295
Class
- Section
- Provides a domain object for layout sections.
Namespace
Drupal\layout_builderCode
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;
}