Commit d63941bc authored by catch's avatar catch
Browse files

Issue #2975461 by quietone, Matroskeen, Lendude: Convert query string to array...

Issue #2975461 by quietone, Matroskeen, Lendude: Convert query string to array for d6 menu_link migration

(cherry picked from commit 55a804ce)
parent c0dca8d0
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -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:
+48 −0
Original line number Diff line number Diff line
<?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;
  }

}
+1 −1
Original line number Diff line number Diff line
@@ -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.
+2 −2
Original line number Diff line number Diff line
@@ -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());

+8 −1
Original line number Diff line number Diff line
@@ -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();
Loading