From d6f56940311d93beaf6580d042b55d653e15f1e0 Mon Sep 17 00:00:00 2001
From: Nathaniel Catchpole <catch@35733.no-reply.drupal.org>
Date: Tue, 15 Mar 2016 10:35:01 +0900
Subject: [PATCH] Issue #2633568 by quietone, ultimike: Improve method for
 migrating link attributes from D6

---
 .../src/Plugin/migrate/process/d6/CckLink.php |  4 ++++
 .../src/Tests/Migrate/d6/MigrateNodeTest.php  | 24 +++++++++++++++++++
 2 files changed, 28 insertions(+)

diff --git a/core/modules/link/src/Plugin/migrate/process/d6/CckLink.php b/core/modules/link/src/Plugin/migrate/process/d6/CckLink.php
index 96ef9e398212..e0d107a3ae67 100644
--- a/core/modules/link/src/Plugin/migrate/process/d6/CckLink.php
+++ b/core/modules/link/src/Plugin/migrate/process/d6/CckLink.php
@@ -51,6 +51,10 @@ public function transform($value, MigrateExecutableInterface $migrate_executable
       $attributes = unserialize($attributes);
     }
 
+    if (!$attributes) {
+      $attributes = [];
+    }
+
     // Massage the values into the correct form for the link.
     $route['uri'] = $value['url'];
     $route['options']['attributes'] = $attributes;
diff --git a/core/modules/node/src/Tests/Migrate/d6/MigrateNodeTest.php b/core/modules/node/src/Tests/Migrate/d6/MigrateNodeTest.php
index 17294bbabeb1..9279cdb3d010 100644
--- a/core/modules/node/src/Tests/Migrate/d6/MigrateNodeTest.php
+++ b/core/modules/node/src/Tests/Migrate/d6/MigrateNodeTest.php
@@ -79,6 +79,30 @@ public function testNode() {
     $this->assertIdentical('test rev 3', $node->body->value);
     $this->assertIdentical('filtered_html', $node->body->format);
 
+    // Test that link fields are migrated.
+    $this->assertIdentical('http://groups.drupal.org/', $node->field_test_link->uri);
+    $this->assertIdentical('Drupal Groups', $node->field_test_link->title);
+    $this->assertIdentical([], $node->field_test_link->options['attributes']);
+
+    // Rerun migration with invalid link attributes and a different URL and
+    // title. If only the attributes are changed the error does not occur.
+    Database::getConnection('default', 'migrate')
+      ->update('content_type_story')
+      ->fields([
+        'field_test_link_url' => 'https://www.drupal.org/node/2127611',
+        'field_test_link_title' => 'Migrate API in Drupal 8',
+        'field_test_link_attributes' => '',
+      ])
+      ->condition('nid', '2')
+      ->condition('vid', '3')
+      ->execute();
+
+    $this->rerunMigration();
+    $node = Node::load(2);
+    $this->assertIdentical('https://www.drupal.org/node/2127611', $node->field_test_link->uri);
+    $this->assertIdentical('Migrate API in Drupal 8', $node->field_test_link->title);
+    $this->assertIdentical([], $node->field_test_link->options['attributes']);
+
     // Test that we can re-import using the EntityContentBase destination.
     $title = $this->rerunMigration();
     $node = Node::load(2);
-- 
GitLab