From 510b5904fcd2498800b733d4e3015003eed23c81 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Mon, 18 Oct 2021 11:04:01 +0100
Subject: [PATCH] Issue #3064016 by Matroskeen, alisonjo315, danflanagan8,
 quietone: Allow menu_link source plugin to filter menu links by menu name(s)

---
 .../src/Plugin/migrate/source/MenuLink.php    | 32 +++++++++++++++++++
 .../Plugin/migrate/source/MenuLinkTest.php    | 22 ++++++++++++-
 2 files changed, 53 insertions(+), 1 deletion(-)

diff --git a/core/modules/menu_link_content/src/Plugin/migrate/source/MenuLink.php b/core/modules/menu_link_content/src/Plugin/migrate/source/MenuLink.php
index fe45b946ef65..271517442b7c 100644
--- a/core/modules/menu_link_content/src/Plugin/migrate/source/MenuLink.php
+++ b/core/modules/menu_link_content/src/Plugin/migrate/source/MenuLink.php
@@ -9,6 +9,35 @@
 /**
  * Drupal menu link source from database.
  *
+ * Available configuration keys:
+ * - menu_name: (optional) The menu name(s) to filter menu links from the source
+ *   can be a string or an array. If not declared then menu links of all menus
+ *   are retrieved.
+ *
+ * Examples:
+ *
+ * @code
+ * source:
+ *   plugin: menu_link
+ *   menu_name: main-menu
+ * @endcode
+ *
+ * In this example menu links of main-menu are retrieved from the source
+ * database.
+ *
+ * @code
+ * source:
+ *   plugin: menu_link
+ *   menu_name: [main-menu, navigation]
+ * @endcode
+ *
+ * In this example menu links of main-menu and navigation menus are retrieved
+ * from the source database.
+ *
+ * For additional configuration keys, refer to the parent classes:
+ * @see \Drupal\migrate\Plugin\migrate\source\SqlBase
+ * @see \Drupal\migrate\Plugin\migrate\source\SourcePluginBase
+ *
  * @MigrateSource(
  *   id = "menu_link",
  *   source_module = "menu"
@@ -29,6 +58,9 @@ public function query() {
       ->condition('ml.customized', 1)
       ->condition($and);
     $query->condition($condition);
+    if (isset($this->configuration['menu_name'])) {
+      $query->condition('ml.menu_name', (array) $this->configuration['menu_name'], 'IN');
+    }
     $query->leftJoin('menu_links', 'pl', '[ml].[plid] = [pl].[mlid]');
     $query->addField('pl', 'link_path', 'parent_link_path');
     $query->orderBy('ml.depth');
diff --git a/core/modules/menu_link_content/tests/src/Kernel/Plugin/migrate/source/MenuLinkTest.php b/core/modules/menu_link_content/tests/src/Kernel/Plugin/migrate/source/MenuLinkTest.php
index e19e0640bb29..b8d80c153180 100644
--- a/core/modules/menu_link_content/tests/src/Kernel/Plugin/migrate/source/MenuLinkTest.php
+++ b/core/modules/menu_link_content/tests/src/Kernel/Plugin/migrate/source/MenuLinkTest.php
@@ -212,7 +212,7 @@ public function providerSource() {
       ],
       [
         // D7 non-customized menu link, provided by menu module.
-        'menu_name' => 'menu-test-menu',
+        'menu_name' => 'menu-test2-menu',
         'mlid' => 300,
         'plid' => 0,
         'link_path' => 'node/142',
@@ -270,6 +270,26 @@ public function providerSource() {
     $tests[0]['expected_data'][] = $expected[3];
     $tests[0]['expected_data'][] = $expected[4];
 
+    // Tests retrieval of links from multiple menus.
+    $tests[1] = $tests[0];
+    $tests[1]['expected_count'] = NULL;
+    $tests[1]['configuration'] = [
+      'menu_name' => ['menu-test-menu', 'menu-test2-menu'],
+    ];
+
+    // Tests retrieval of links from a single menu.
+    $tests[2] = $tests[1];
+    $tests[2]['configuration'] = [
+      'menu_name' => 'menu-test2-menu',
+    ];
+    $tests[2]['expected_data'] = [$expected[6]];
+
+    // Tests retrieval of links from a not existing menu.
+    $tests[3] = $tests[1];
+    $tests[3]['configuration'] = [
+      'menu_name' => 'menu-not-exists',
+    ];
+    $tests[3]['expected_data'] = [];
     return $tests;
   }
 
-- 
GitLab