Skip to content
Snippets Groups Projects

Add support for nested paragraphs

@@ -3,6 +3,7 @@
namespace Drupal\quick_node_clone\Entity;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityFormBuilder;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
@@ -205,25 +206,25 @@ class QuickNodeCloneEntityFormBuilder extends EntityFormBuilder {
}
/**
* Clone the paragraphs of a node.
* Clone the paragraphs of an entity.
*
* If we do not clone the paragraphs attached to the node, the linked
* paragraphs would be linked to two nodes which is not ideal.
* If we do not clone the paragraphs attached to the entity, the linked
* paragraphs would be linked to two entities which is not ideal.
*
* @param \Drupal\node\Entity\Node $node
* The node to clone.
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The entity to clone paragraphs for.
*
* @return \Drupal\node\Entity\Node
* The node with cloned paragraph fields.
* @return \Drupal\Core\Entity\ContentEntityInterface $entity
* The entity with cloned paragraph fields.
*/
public function cloneParagraphs(Node $node) {
foreach ($node->getFieldDefinitions() as $field_definition) {
public function cloneParagraphs(ContentEntityInterface $entity) {
foreach ($entity->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 (!$entity->get($field_name)->isEmpty()) {
foreach ($entity->get($field_name) as $value) {
if ($value->entity) {
$value->entity = $value->entity->createDuplicate();
foreach ($value->entity->getFieldDefinitions() as $field_definition) {
@@ -237,6 +238,11 @@ class QuickNodeCloneEntityFormBuilder extends EntityFormBuilder {
}
$this->moduleHandler->alter('cloned_node_paragraph_field', $value->entity, $pfield_name, $pfield_settings);
if (isset($pfield_settings['target_type']) && $pfield_settings['target_type'] == "paragraph" && !empty($value->entity->{$pfield_name})) {
// Do the same for any nested paragraphs.
self::cloneParagraphs($value->entity);
}
}
}
}
@@ -244,7 +250,7 @@ class QuickNodeCloneEntityFormBuilder extends EntityFormBuilder {
}
}
return $node;
return $entity;
}
/**
Loading