public function Paragraph::createDuplicate

File

paragraphs/src/Entity/Paragraph.php, line 424

Class

Paragraph
Defines the Paragraph entity.

Namespace

Drupal\paragraphs\Entity

Code

public function createDuplicate() {
  $duplicate = parent::createDuplicate();

  // Loop over entity fields and duplicate nested paragraphs.
  foreach ($duplicate
    ->getFields() as $fieldItemList) {
    if ($fieldItemList instanceof EntityReferenceFieldItemListInterface && $fieldItemList
      ->getSetting('target_type') === $this
      ->getEntityTypeId()) {
      foreach ($fieldItemList as $delta => $item) {
        if ($item->entity) {
          $fieldItemList[$delta] = $item->entity
            ->createDuplicate();
        }
      }
    }
  }
  return $duplicate;
}