You are here

public function Paragraph::createDuplicate in Paragraphs 8

Creates a duplicate of the entity.

Return value

static A clone of $this with all identifiers unset, so saving it inserts a new entity into the storage system.

Overrides ContentEntityBase::createDuplicate

File

src/Entity/Paragraph.php, line 419

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;
}