Skip to content
Snippets Groups Projects

Draft: Quickfix to make Quick Node Clone compatible with layout_paragraphs.

1 file
+ 32
0
Compare changes
  • Side-by-side
  • Inline
@@ -221,8 +221,23 @@ class QuickNodeCloneEntityFormBuilder extends EntityFormBuilder {
$field_name = $field_storage_definition->getName();
if (isset($field_settings['target_type']) && $field_settings['target_type'] == "paragraph") {
if (!$node->get($field_name)->isEmpty()) {
// TODO: Layout Paragraphs integration:
// TODO: See https://www.drupal.org/project/layout_paragraphs/issues/3218226#comment-14129846
// TODO: See https://www.drupal.org/project/quick_node_clone/issues/3218222
// TODO: All the Steps 1-6 don't belong here in a perfect world but belong to layout_paragraphs
// 1. Create helper array to store old / new uuids from all paragraphs
// to update parent_uuids from all children later here:
$uuidOldNewMap = [];
foreach ($node->get($field_name) as $value) {
if ($value->entity) {
// 2. Store the old uuid of this paragraph:
$oldUuid = $value->entity->uuid();
$value->entity = $value->entity->createDuplicate();
// 3. Store the new uuid of this paragraph:
$newUuid = $value->entity->uuid();
// 4. Save old => new uuid relations in array to replace the old one later:
$uuidOldNewMap[$oldUuid] = $newUuid;
$value->entity = $value->entity->createDuplicate();
foreach ($value->entity->getFieldDefinitions() as $field_definition) {
$field_storage_definition = $field_definition->getFieldStorageDefinition();
@@ -235,9 +250,26 @@ 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);
}
}
}
}
// 5. Update children paragraphs parent_uuid's to the new uuids from
// the cloned paragraphs:
$behavior_settings = $value->entity->getAllBehaviorSettings();
if(isset($behavior_settings['layout_paragraphs']) && !empty($behavior_settings['layout_paragraphs']['parent_uuid'])){
$oldParentUuid = $behavior_settings['layout_paragraphs']['parent_uuid'];
if(!empty($uuidOldNewMap[$oldParentUuid])){
$newParentUuid = $uuidOldNewMap[$oldParentUuid];
$behavior_settings['layout_paragraphs']['parent_uuid'] = $newParentUuid;
// kint([$oldParentUuid, $newParentUuid]);
$value->entity->setBehaviorSettings('layout_paragraphs', $behavior_settings['layout_paragraphs']);
}
}
}
}
}
Loading