From d63941bc1af2e1a31debad1f202e46351eaa149d Mon Sep 17 00:00:00 2001
From: catch <catch@35733.no-reply.drupal.org>
Date: Mon, 8 Nov 2021 14:49:50 +0000
Subject: [PATCH] Issue #2975461 by quietone, Matroskeen, Lendude: Convert
 query string to array for d6 menu_link migration

(cherry picked from commit 55a804ce67ddf1296a0004303baf69512e3892c5)
---
 .../migrations/d6_menu_links.yml              |  4 +-
 .../Plugin/migrate/process/LinkOptions.php    | 48 +++++++++++++++++++
 .../Kernel/Migrate/d6/MigrateMenuLinkTest.php |  2 +-
 .../d6/MigrateMenuLinkTranslationTest.php     |  4 +-
 .../Kernel/Migrate/d7/MigrateMenuLinkTest.php |  9 +++-
 .../migrate_drupal/tests/fixtures/drupal7.php |  2 +-
 6 files changed, 63 insertions(+), 6 deletions(-)
 create mode 100644 core/modules/menu_link_content/src/Plugin/migrate/process/LinkOptions.php

diff --git a/core/modules/menu_link_content/migrations/d6_menu_links.yml b/core/modules/menu_link_content/migrations/d6_menu_links.yml
index 86dfb8057361..48faf0e28b11 100644
--- a/core/modules/menu_link_content/migrations/d6_menu_links.yml
+++ b/core/modules/menu_link_content/migrations/d6_menu_links.yml
@@ -27,7 +27,9 @@ process:
   'link/uri':
     plugin: link_uri
     source: link_path
-  'link/options': options
+  'link/options':
+    plugin: link_options
+    source: options
   route:
     plugin: route
     source:
diff --git a/core/modules/menu_link_content/src/Plugin/migrate/process/LinkOptions.php b/core/modules/menu_link_content/src/Plugin/migrate/process/LinkOptions.php
new file mode 100644
index 000000000000..af3e0998a994
--- /dev/null
+++ b/core/modules/menu_link_content/src/Plugin/migrate/process/LinkOptions.php
@@ -0,0 +1,48 @@
+<?php
+
+namespace Drupal\menu_link_content\Plugin\migrate\process;
+
+use Drupal\migrate\MigrateExecutableInterface;
+use Drupal\migrate\ProcessPluginBase;
+use Drupal\migrate\Row;
+
+/**
+ * Converts links options.
+ *
+ * Examples:
+ *
+ * @code
+ * process:
+ *   link/options:
+ *     plugin: link_options
+ *     source: options
+ * @endcode
+ *
+ * This will convert the query options of the link.
+ *
+ * @MigrateProcessPlugin(
+ *   id = "link_options",
+ *   handle_multiples = TRUE
+ * )
+ */
+class LinkOptions extends ProcessPluginBase {
+
+  /**
+   * {@inheritdoc}
+   */
+  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
+    if (isset($value['query'])) {
+      // If the query parameters are stored as a string (as in D6), convert it
+      // into an array.
+      if (is_string($value['query'])) {
+        parse_str($value['query'], $old_query);
+      }
+      else {
+        $old_query = $value['query'];
+      }
+      $value['query'] = $old_query;
+    }
+    return $value;
+  }
+
+}
diff --git a/core/modules/menu_link_content/tests/src/Kernel/Migrate/d6/MigrateMenuLinkTest.php b/core/modules/menu_link_content/tests/src/Kernel/Migrate/d6/MigrateMenuLinkTest.php
index 5ffe4e6cc036..bc72be37598d 100644
--- a/core/modules/menu_link_content/tests/src/Kernel/Migrate/d6/MigrateMenuLinkTest.php
+++ b/core/modules/menu_link_content/tests/src/Kernel/Migrate/d6/MigrateMenuLinkTest.php
@@ -89,7 +89,7 @@ protected function assertEntity($id, $title, $menu, $description, $enabled, $exp
    */
   public function testMenuLinks() {
     $this->assertEntity('138', 'Test 1', 'secondary-links', 'Test menu link 1', TRUE, FALSE, ['attributes' => ['title' => 'Test menu link 1'], 'langcode' => 'en'], 'internal:/user/login', -50);
-    $this->assertEntity('139', 'Test 2', 'secondary-links', 'Test menu link 2', TRUE, TRUE, ['query' => 'foo=bar', 'attributes' => ['title' => 'Test menu link 2']], 'internal:/admin', -49);
+    $this->assertEntity('139', 'Test 2', 'secondary-links', 'Test menu link 2', TRUE, TRUE, ['query' => ['foo' => 'bar'], 'attributes' => ['title' => 'Test menu link 2']], 'internal:/admin', -49);
     $this->assertEntity('140', 'Drupal.org', 'secondary-links', NULL, TRUE, FALSE, ['attributes' => ['title' => '']], 'https://www.drupal.org', -50);
 
     // Assert that missing title attributes don't stop or break migration.
diff --git a/core/modules/menu_link_content/tests/src/Kernel/Migrate/d6/MigrateMenuLinkTranslationTest.php b/core/modules/menu_link_content/tests/src/Kernel/Migrate/d6/MigrateMenuLinkTranslationTest.php
index afdbd7d01215..cedcfbb8fdf5 100644
--- a/core/modules/menu_link_content/tests/src/Kernel/Migrate/d6/MigrateMenuLinkTranslationTest.php
+++ b/core/modules/menu_link_content/tests/src/Kernel/Migrate/d6/MigrateMenuLinkTranslationTest.php
@@ -53,7 +53,7 @@ public function testMenuLinks() {
     $this->assertSame('secondary-links', $menu_link->getMenuName());
     $this->assertTrue($menu_link->isEnabled());
     $this->assertTrue($menu_link->isExpanded());
-    $this->assertSame(['query' => 'foo=bar', 'attributes' => ['title' => 'Test menu link 2']], $menu_link->link->options);
+    $this->assertSame(['query' => ['foo' => 'bar'], 'attributes' => ['title' => 'Test menu link 2']], $menu_link->link->options);
     $this->assertSame('internal:/admin', $menu_link->link->uri);
     $this->assertSame(-49, $menu_link->getWeight());
 
@@ -64,7 +64,7 @@ public function testMenuLinks() {
     $this->assertSame('secondary-links', $menu_link->getMenuName());
     $this->assertTrue($menu_link->isEnabled());
     $this->assertTrue($menu_link->isExpanded());
-    $this->assertSame(['query' => 'foo=bar', 'attributes' => ['title' => 'Test menu link 2']], $menu_link->link->options);
+    $this->assertSame(['query' => ['foo' => 'bar'], 'attributes' => ['title' => 'Test menu link 2']], $menu_link->link->options);
     $this->assertSame('internal:/admin', $menu_link->link->uri);
     $this->assertSame(-49, $menu_link->getWeight());
 
diff --git a/core/modules/menu_link_content/tests/src/Kernel/Migrate/d7/MigrateMenuLinkTest.php b/core/modules/menu_link_content/tests/src/Kernel/Migrate/d7/MigrateMenuLinkTest.php
index dd3cac07dfcd..cd271f89d516 100644
--- a/core/modules/menu_link_content/tests/src/Kernel/Migrate/d7/MigrateMenuLinkTest.php
+++ b/core/modules/menu_link_content/tests/src/Kernel/Migrate/d7/MigrateMenuLinkTest.php
@@ -70,7 +70,14 @@ public function testMenuLinks() {
 
     $this->assertEntity(245, 'und', 'Home', 'main', NULL, TRUE, FALSE, [], 'internal:/', 0);
     $this->assertEntity(478, 'und', 'custom link test', 'admin', NULL, TRUE, FALSE, ['attributes' => ['title' => '']], 'internal:/admin/content', 0);
-    $this->assertEntity(479, 'und', 'node link test', 'tools', 'node 2', TRUE, FALSE, ['attributes' => ['title' => 'node 2']], 'entity:node/2', 3);
+    $this->assertEntity(479, 'und', 'node link test', 'tools', 'node 2', TRUE, FALSE, [
+      'attributes' => ['title' => 'node 2'],
+      'query' => [
+        'name' => 'ferret',
+        'color' => 'purple',
+      ],
+    ],
+      'entity:node/2', 3);
 
     $menu_link_tree_service = \Drupal::service('menu.link_tree');
     $parameters = new MenuTreeParameters();
diff --git a/core/modules/migrate_drupal/tests/fixtures/drupal7.php b/core/modules/migrate_drupal/tests/fixtures/drupal7.php
index b7f16908737d..26a5b50fdd52 100644
--- a/core/modules/migrate_drupal/tests/fixtures/drupal7.php
+++ b/core/modules/migrate_drupal/tests/fixtures/drupal7.php
@@ -34445,7 +34445,7 @@
   'link_path' => 'node/2',
   'router_path' => 'node/%',
   'link_title' => 'node link test',
-  'options' => 'a:1:{s:10:"attributes";a:1:{s:5:"title";s:6:"node 2";}}',
+  'options' => 'a:2:{s:10:"attributes";a:1:{s:5:"title";s:6:"node 2";}s:5:"query";a:2:{s:4:"name";s:6:"ferret";s:5:"color";s:6:"purple";}}',
   'module' => 'menu',
   'hidden' => '0',
   'external' => '0',
-- 
GitLab