Problem/Motivation

When we select `Clone entity browser blocks` option while cloning entity, all blocks get cloned. In some scenarios, we need to clone only specific blocks and reuse few of them.

Proposed resolution

We should show a checklist to select blocks. In form alter, we are providing one checkbox(Clone entity browser blocks). If the user selects this checkbox, we can show a list of blocks that can be cloned. By default, all will be selected. If the user wants, user can deselect any block that can be resued instead of cloning.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

maithili11 created an issue. See original summary.

maithili11’s picture

Assigned: maithili11 » Unassigned
Issue summary: View changes
Status: Active » Needs review
FileSize
4.99 KB
38.53 KB

Providing a patch based on "Proposed resolution"

mohit_aghera’s picture

Status: Needs review » Needs work
+++ b/entity_clone_entity_browser_block.module
@@ -28,14 +30,93 @@ function entity_clone_entity_browser_block_help($route_name, RouteMatchInterface
+function getOptionList($entities) {
+  $options = [];
+  foreach ($entities as $id => $entity) {
+    $options[$id] = $entity->label();
+  }
+  return $options;

Function name should be prefixed with module machine name

+++ b/entity_clone_entity_browser_block.module
@@ -28,14 +30,93 @@ function entity_clone_entity_browser_block_help($route_name, RouteMatchInterface
+function getBlockContentEntities($entity) {
+  $block_content_entities = [];
...
+    foreach ($entity->getFieldDefinitions() as $field_id => $field_definition) {
+      if (fieldIsLayoutBuilder($field_definition)) {
+        $field = $entity->get($field_id);
+        $sections = $field->getSections();
+        foreach ($sections as $delta => $section) {
+          $components = $section->getComponents();
+          foreach ($components as $uuid => $component) {
...
+            if ($plugin instanceof EntityBrowserBlock) {
+              $block_ids = $plugin->getConfiguration()['entity_ids'];
+              foreach ($block_ids as $block_id) {
+                [$entity_type_id, $entity_id] = explode(':', $block_id);
+                $block_entity = Drupal::entityTypeManager()->getStorage($entity_type_id)->load($entity_id);
+                if ($block_entity) {
+                  $block_content_entities[$block_id] = $block_entity;
+                }
+              }
+            }
+          }
+        }
+      }
+    }
+  }
+  return $block_content_entities;

- We can update method name.
- Reduce nested conditions and optimize code.
We can leverage early return by checking
"if(!$entity instanceOf FieldableEntityInterface)" etc.

+++ b/entity_clone_entity_browser_block.module
@@ -28,14 +30,93 @@ function entity_clone_entity_browser_block_help($route_name, RouteMatchInterface
+function fieldIsLayoutBuilder($field_definition) {
+  return ($field_definition->getType() == 'layout_section');
+}
+

I think we can give remove additional method and directly use the condition where this method is called.