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.
*Issue fork ui_patterns-3472571
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
Comment #3
pdureau commentedComment #4
pdureau commentedComment #6
pdureau commentedCan you have a look once the pipelines has run?
Comment #7
pdureau commentedpipeline has run and everything is OK :)
Comment #9
pdureau commentedComment #10
pdureau commented