You are here

public function LayoutParagraphsLayout::insertIntoRegion in Layout Paragraphs 2.0.x

Insert a paragraph component before an existing component.

Parameters

string $parent_uuid: The parent component's uuid.

string $region: The region.

\Drupal\paragraphs\Entity\Paragraph $paragraph: The paragraph component to add.

Return value

$this

File

src/LayoutParagraphsLayout.php, line 345

Class

LayoutParagraphsLayout
Provides a domain object for a complete Layout Paragraphs Layout.

Namespace

Drupal\layout_paragraphs

Code

public function insertIntoRegion(string $parent_uuid, string $region, Paragraph $paragraph) {

  // Create a layout component for the new paragraph.
  $component = $this
    ->getComponent($paragraph);

  // Make sure the parent component exists.
  if ($this
    ->getComponentByUuid($parent_uuid)) {

    // Set the parent and region.
    $component
      ->setSettings([
      'parent_uuid' => $parent_uuid,
      'region' => $region,
    ]);

    // Get the paragraph entity from the component.
    $new_paragraph = $component
      ->getEntity();
    $new_paragraph
      ->setParentEntity($this
      ->getEntity(), $this
      ->getFieldName());

    // Splice the new paragraph into the field item list.
    $list = $this->paragraphsReferenceField
      ->getValue();
    $list[] = [
      'entity' => $new_paragraph,
    ];
    $this->paragraphsReferenceField
      ->setValue($list);
  }
  else {

    // @todo: throw exception.
  }
  return $this;
}