You are here

public function QuickNodeCloneEntityFormBuilder::cloneParagraphs in Quick Node Clone 8

Clone the paragraphs of a node.

If we do not clone the paragraphs attached to the node, the linked paragraphs would be linked to two nodes which is not ideal.

Parameters

\Drupal\node\Entity\Node $node: The node to clone.

Return value

\Drupal\node\Entity\Node The node with cloned paragraph fields.

1 call to QuickNodeCloneEntityFormBuilder::cloneParagraphs()
QuickNodeCloneEntityFormBuilder::getForm in src/Entity/QuickNodeCloneEntityFormBuilder.php
Gets the built and processed entity form for the given entity.

File

src/Entity/QuickNodeCloneEntityFormBuilder.php, line 218

Class

QuickNodeCloneEntityFormBuilder
Builds entity forms.

Namespace

Drupal\quick_node_clone\Entity

Code

public function cloneParagraphs(Node $node) {
  foreach ($node
    ->getFieldDefinitions() as $field_definition) {
    $field_storage_definition = $field_definition
      ->getFieldStorageDefinition();
    $field_settings = $field_storage_definition
      ->getSettings();
    $field_name = $field_storage_definition
      ->getName();
    if (isset($field_settings['target_type']) && $field_settings['target_type'] == "paragraph") {
      if (!$node
        ->get($field_name)
        ->isEmpty()) {
        foreach ($node
          ->get($field_name) as $value) {
          if ($value->entity) {
            $value->entity = $value->entity
              ->createDuplicate();
            foreach ($value->entity
              ->getFieldDefinitions() as $field_definition) {
              $field_storage_definition = $field_definition
                ->getFieldStorageDefinition();
              $pfield_settings = $field_storage_definition
                ->getSettings();
              $pfield_name = $field_storage_definition
                ->getName();

              // Check whether this field is excluded and if so unset.
              if ($this
                ->excludeParagraphField($pfield_name, $value->entity
                ->bundle())) {
                unset($value->entity->{$pfield_name});
              }
              $this->moduleHandler
                ->alter('cloned_node_paragraph_field', $value->entity, $pfield_name, $pfield_settings);
            }
          }
        }
      }
    }
  }
  return $node;
}