From a126d042174ec008b5d6211055b6d2d41faa823f Mon Sep 17 00:00:00 2001
From: catch <catch@35733.no-reply.drupal.org>
Date: Mon, 26 Feb 2024 16:27:22 +0000
Subject: [PATCH] Issue #3091841 by quietone, Grevil, Anybody, mikelutz,
 smustgrave, danflanagan8, jklmnop, ShaunDychko, fengtan, MariaIoann,
 ranjith_kumar_k_u: Remove hardcoded plugin IDs from migration process plugins

---
 core/modules/block/migrations/d6_block.yml    |  20 ++++
 core/modules/block/migrations/d7_block.yml    |  20 ++++
 .../Plugin/migrate/process/BlockPluginId.php  |  13 ++-
 .../migrate/process/BlockVisibility.php       |  15 ++-
 .../Plugin/migrate/process/RolesLookup.php    | 105 ++++++++++++++++++
 .../field/migrations/d7_field_instance.yml    |  17 +++
 .../Plugin/migrate/process/d7/FieldBundle.php |  13 ++-
 .../process/d6/FilterFormatPermission.php     |  21 ++++
 core/modules/user/migrations/d6_user_role.yml |   1 +
 9 files changed, 215 insertions(+), 10 deletions(-)
 create mode 100644 core/modules/block/src/Plugin/migrate/process/RolesLookup.php

diff --git a/core/modules/block/migrations/d6_block.yml b/core/modules/block/migrations/d6_block.yml
index f74eb8e05991..bbfe15bfbdf8 100644
--- a/core/modules/block/migrations/d6_block.yml
+++ b/core/modules/block/migrations/d6_block.yml
@@ -22,6 +22,23 @@ process:
     postfix: _
     length: 29
     source: module
+  _block_module_plugin_id:
+    -
+      plugin: static_map
+      source:
+        - module
+      map:
+        block: block
+      default_value: ''
+    -
+      plugin: skip_on_empty
+      method: process
+    -
+      plugin: migration_lookup
+      migration:
+        - d6_custom_block
+      source:
+        - delta
   plugin:
     -
       plugin: static_map
@@ -87,6 +104,9 @@ process:
       - delta
       - settings
       - title
+  _role_ids:
+    plugin: roles_lookup
+    migration: d6_user_role
   visibility:
     plugin: block_visibility
     source:
diff --git a/core/modules/block/migrations/d7_block.yml b/core/modules/block/migrations/d7_block.yml
index 184eee2f81ba..23080fc7c99b 100644
--- a/core/modules/block/migrations/d7_block.yml
+++ b/core/modules/block/migrations/d7_block.yml
@@ -25,6 +25,23 @@ process:
     -
       plugin: machine_name
       field: id
+  _block_module_plugin_id:
+    -
+      plugin: static_map
+      source:
+        - module
+      map:
+        block: block
+      default_value: ''
+    -
+      plugin: skip_on_empty
+      method: process
+    -
+      plugin: migration_lookup
+      migration:
+        - d7_custom_block
+      source:
+        - delta
   plugin:
     -
       plugin: static_map
@@ -108,6 +125,9 @@ process:
       - delta
       - settings
       - title
+  _role_ids:
+    plugin: roles_lookup
+    migration: d7_user_role
   visibility:
     plugin: block_visibility
     source:
diff --git a/core/modules/block/src/Plugin/migrate/process/BlockPluginId.php b/core/modules/block/src/Plugin/migrate/process/BlockPluginId.php
index 8f7f9f6e36a0..41524821b68e 100644
--- a/core/modules/block/src/Plugin/migrate/process/BlockPluginId.php
+++ b/core/modules/block/src/Plugin/migrate/process/BlockPluginId.php
@@ -88,9 +88,16 @@ public function transform($value, MigrateExecutableInterface $migrate_executable
 
         case 'block':
           if ($this->blockContentStorage) {
-            $lookup_result = $this->migrateLookup->lookup(['d6_custom_block', 'd7_custom_block'], [$delta]);
-            if ($lookup_result) {
-              $block_id = $lookup_result[0]['id'];
+            $block_id = $row->getDestinationProperty('_block_module_plugin_id');
+            // Legacy generated migrations will not have the destination
+            // property '_block_module_plugin_id'.
+            if (!$block_id) {
+              $lookup_result = $this->migrateLookup->lookup(['d6_custom_block', 'd7_custom_block'], [$delta]);
+              if ($lookup_result) {
+                $block_id = $lookup_result[0]['id'];
+              }
+            }
+            if ($block_id) {
               return 'block_content:' . $this->blockContentStorage->load($block_id)->uuid();
             }
           }
diff --git a/core/modules/block/src/Plugin/migrate/process/BlockVisibility.php b/core/modules/block/src/Plugin/migrate/process/BlockVisibility.php
index cb365b9550c6..a5984e3b7dc6 100644
--- a/core/modules/block/src/Plugin/migrate/process/BlockVisibility.php
+++ b/core/modules/block/src/Plugin/migrate/process/BlockVisibility.php
@@ -97,13 +97,22 @@ public function transform($value, MigrateExecutableInterface $migrate_executable
         ],
         'negate' => FALSE,
       ];
-
+      // Legacy generated migrations will not have the destination property
+      // '_role_ids'.
+      $role_ids = $row->getDestinationProperty('_role_ids');
       foreach ($roles as $key => $role_id) {
-        $lookup_result = $this->migrateLookup->lookup(['d6_user_role', 'd7_user_role'], [$role_id]);
+        if (!$role_ids) {
+          $lookup = $this->migrateLookup->lookup(['d6_user_role', 'd7_user_role'], [$role_id]);
+          $lookup_result = $lookup[0]['id'];
+        }
+        else {
+          $lookup_result = $role_ids[$role_id] ?? NULL;
+        }
         if ($lookup_result) {
-          $roles[$key] = $lookup_result[0]['id'];
+          $roles[$key] = $lookup_result;
         }
       }
+
       $visibility['user_role']['roles'] = array_combine($roles, $roles);
     }
 
diff --git a/core/modules/block/src/Plugin/migrate/process/RolesLookup.php b/core/modules/block/src/Plugin/migrate/process/RolesLookup.php
new file mode 100644
index 000000000000..210a9c21d9ab
--- /dev/null
+++ b/core/modules/block/src/Plugin/migrate/process/RolesLookup.php
@@ -0,0 +1,105 @@
+<?php
+
+namespace Drupal\block\Plugin\migrate\process;
+
+use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
+use Drupal\migrate\MigrateLookupInterface;
+use Drupal\migrate\Plugin\MigrationInterface;
+use Drupal\migrate\MigrateExecutableInterface;
+use Drupal\migrate\ProcessPluginBase;
+use Drupal\migrate\Row;
+use Symfony\Component\DependencyInjection\ContainerInterface;
+
+/**
+ * Gets the destination roles ID for an array of source roles IDs.
+ *
+ * The roles_lookup plugin is used to get the destination roles for roles that
+ * are assigned to a block. It always uses the 'roles' value on the row as the
+ * source value.
+ *
+ *  Examples
+ *
+ * @code
+ *  process:
+ *    roles:
+ *      plugin: roles_lookup
+ *      migration: d7_user_role
+ * @endcode
+ *
+ * This will get the destination role ID for each role in the 'roles' value on
+ * the source row.
+ *
+ * @see \Drupal\migrate\Plugin\MigrateProcessInterface
+ *
+ * @MigrateProcessPlugin(
+ *   id = "roles_lookup"
+ * )
+ */
+class RolesLookup extends ProcessPluginBase implements ContainerFactoryPluginInterface {
+
+  /**
+   * The migrate lookup service.
+   *
+   * @var \Drupal\migrate\MigrateLookupInterface
+   */
+  protected $migrateLookup;
+
+  /**
+   * The migration for user role lookup.
+   *
+   * @var string
+   */
+  protected $migration;
+
+  /**
+   * Constructs a BlockVisibility object.
+   *
+   * @param array $configuration
+   *   The plugin configuration.
+   * @param string $plugin_id
+   *   The plugin ID.
+   * @param mixed $plugin_definition
+   *   The plugin definition.
+   * @param \Drupal\migrate\MigrateLookupInterface $migrate_lookup
+   *   The migrate lookup service.
+   */
+  public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrateLookupInterface $migrate_lookup) {
+    parent::__construct($configuration, $plugin_id, $plugin_definition);
+    $this->migrateLookup = $migrate_lookup;
+
+    if (isset($configuration['migration'])) {
+      $this->migration = $configuration['migration'];
+    }
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
+    return new static(
+      $configuration,
+      $plugin_id,
+      $plugin_definition,
+      $container->get('migrate.lookup')
+    );
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
+    $roles = $row->get('roles');
+    $roles_result = [];
+    // If the block is assigned to specific roles, add the user_role condition.
+    if ($roles) {
+      foreach ($roles as $role_id) {
+        $lookup_result = $this->migrateLookup->lookup([$this->migration], [$role_id]);
+        if ($lookup_result) {
+          $roles_result[$role_id] = $lookup_result[0]['id'];
+        }
+      }
+    }
+    return $roles_result;
+  }
+
+}
diff --git a/core/modules/field/migrations/d7_field_instance.yml b/core/modules/field/migrations/d7_field_instance.yml
index 0fefd047070e..0dd6e3be8217 100644
--- a/core/modules/field/migrations/d7_field_instance.yml
+++ b/core/modules/field/migrations/d7_field_instance.yml
@@ -9,6 +9,7 @@ source:
   plugin: d7_field_instance
   constants:
     status: true
+    comment_node: comment_node_
 process:
   type:
     plugin: process_field
@@ -25,6 +26,22 @@ process:
     bypass: true
     map:
       comment_node_forum: comment_forum
+  _comment_type:
+    -
+      plugin: explode
+      source: bundle
+      delimiter: comment_node_
+    -
+      plugin: extract
+      index: [1]
+      default: false
+    -
+      plugin: skip_on_empty
+      method: process
+    -
+      plugin: migration_lookup
+      migration:
+        - d7_comment_type
   bundle:
     plugin: field_bundle
     source:
diff --git a/core/modules/field/src/Plugin/migrate/process/d7/FieldBundle.php b/core/modules/field/src/Plugin/migrate/process/d7/FieldBundle.php
index c91017928558..5b0b95019515 100644
--- a/core/modules/field/src/Plugin/migrate/process/d7/FieldBundle.php
+++ b/core/modules/field/src/Plugin/migrate/process/d7/FieldBundle.php
@@ -102,10 +102,15 @@ public function transform($value, MigrateExecutableInterface $migrate_executable
     // For comment entity types get the destination bundle from the
     // d7_comment_type migration, if it exists.
     if ($entity_type === 'comment' && $bundle != 'comment_forum') {
-      $value = str_replace('comment_node_', '', $bundle);
-      $migration = 'd7_comment_type';
-      $lookup_result = $this->migrateLookup->lookup($migration, [$value]);
-      $lookup_result = empty($lookup_result) ? NULL : reset($lookup_result[0]);
+      $lookup_result = $row->get('@_comment_type');
+      // Legacy generated migrations will not have the destination property
+      // '_comment_type'.
+      if (!$row->hasDestinationProperty('_comment_type')) {
+        $value = str_replace('comment_node_', '', $bundle);
+        $migration = 'd7_comment_type';
+        $lookup_result = $this->migrateLookup->lookup($migration, [$value]);
+        $lookup_result = empty($lookup_result) ? NULL : reset($lookup_result[0]);
+      }
     }
     return $lookup_result ? $lookup_result : $bundle;
   }
diff --git a/core/modules/filter/src/Plugin/migrate/process/d6/FilterFormatPermission.php b/core/modules/filter/src/Plugin/migrate/process/d6/FilterFormatPermission.php
index 357d3562034f..d05ce6792b55 100644
--- a/core/modules/filter/src/Plugin/migrate/process/d6/FilterFormatPermission.php
+++ b/core/modules/filter/src/Plugin/migrate/process/d6/FilterFormatPermission.php
@@ -13,6 +13,27 @@
 /**
  * Migrate filter format serial to string id in permission name.
  *
+ * The filter_format_permission plugin is used to get the filter formats for a
+ * role and convert it to a permission name.
+ *
+ *  Available configuration keys:
+ *  - migration: (optional) The filter migration. Defaults to
+ *  'd6_filter_format'.
+ *
+ *  Examples:
+ *
+ * @code
+ *  process:
+ *    result:
+ *      plugin: filter_format_permission
+ *      migration: d6_filter_format
+ * @endcode
+ *
+ *  This will use the 'd6_filter_format' migration to lookup the destination
+ *  filter formats for a role.
+ *
+ * @see \Drupal\migrate\Plugin\MigrateProcessInterface
+ *
  * @MigrateProcessPlugin(
  *   id = "filter_format_permission",
  *   handle_multiples = TRUE
diff --git a/core/modules/user/migrations/d6_user_role.yml b/core/modules/user/migrations/d6_user_role.yml
index a0504851d98a..b27e2da1dc92 100644
--- a/core/modules/user/migrations/d6_user_role.yml
+++ b/core/modules/user/migrations/d6_user_role.yml
@@ -35,6 +35,7 @@ process:
     - plugin: node_update_7008
     - plugin: flatten
     - plugin: filter_format_permission
+      migration: d6_filter_format
 destination:
   plugin: entity:user_role
 migration_dependencies:
-- 
GitLab