Problem/Motivation

The port of #3469755: Links normalize drops link options for UIP2

Proposed resolution

To do after #3470231: [2.0.0-beta2] Attributes normalization is merged

Proposal:

diff --git a/src/Plugin/UiPatterns/PropType/LinksPropType.php b/src/Plugin/UiPatterns/PropType/LinksPropType.php
index 7ba20aa..a4c6142 100644
--- a/src/Plugin/UiPatterns/PropType/LinksPropType.php
+++ b/src/Plugin/UiPatterns/PropType/LinksPropType.php
@@ -94,7 +94,7 @@ class LinksPropType extends PropTypePluginBase implements ContainerFactoryPlugin
   protected static function normalizeLinks(array $value): array {
     // Don't inject URL object into patterns templates, use "title" as item
     // label and "url" as item target.
-    foreach ($value as $index => &$item) {
+    foreach ($value as $index => $item) {
       if (!is_array($item)) {
         unset($value[$index]);
         continue;
@@ -114,17 +114,13 @@ class LinksPropType extends PropTypePluginBase implements ContainerFactoryPlugin
         $item["url"] = $item["href"];
         unset($item["href"]);
       }
-      if (!isset($item["url"]) && isset($item["link"])) {
-        // Example: links.html.twig.
-        $item["url"] = $item["link"]["#url"];
-        $item["url"]->setOptions($item["link"]["#options"]);
-        unset($item["link"]);
-      }
+      $item = self::extractLinkData($item);
       $item = self::normalizeUrl($item);
       $item = static::normalizeAttributes($item, "link_attributes");
       if (array_key_exists("below", $item)) {
         $item["below"] = self::normalize($item["below"]);
       }
+      $value[$index] = $item;
     }
     return array_values($value);
   }
@@ -154,6 +150,26 @@ class LinksPropType extends PropTypePluginBase implements ContainerFactoryPlugin
     return $item;
   }
 
+  /**
+   * Extract data from link.
+   *
+   * Useful for: links.html.twig.
+   */
+  protected static function extractLinkData(array $item): array {
+    if (isset($item["url"])) {
+      return $item;
+    }
+    if (!isset($item["link"])) {
+      return $item;
+    }
+    $item["url"] = $item["link"]["#url"];
+    if (isset($item["link"]["#options"])) {
+      $item["url"]->mergeOptions($item["link"]["#options"]);
+    }
+    unset($item["link"]);
+    return $item;
+  }
+
   /**
    * Normalize URL in an item.
    *
Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

pdureau created an issue. See original summary.

pdureau’s picture

pdureau’s picture

pdureau’s picture

Assigned: pdureau » grimreaper
Status: Active » Needs review

Can you have a look once the pipelines has run?

pdureau’s picture

pipeline has run and everything is OK :)

  • pdureau committed d37f46a6 on 2.0.x
    Issue #3472571 by pdureau, grimreaper: Links normalize drops link...
pdureau’s picture

Assigned: grimreaper » Unassigned
Status: Needs review » Fixed
pdureau’s picture

Status: Fixed » Closed (fixed)