[2.0.0-beta2] Links normalize drops link options

Created on 6 September 2024, 5 months ago
Updated 15 September 2024, 4 months ago

Problem/Motivation

The port of 🐛 Links normalize drops link options Needs review for UIP2

Proposed resolution

To do after 📌 [2.0.0-beta2] Attributes normalization Fixed 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.
    *
📌 Task
Status

Fixed

Version

2.0

Component

Code

Created by

🇫🇷France pdureau Paris

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024