Skip to content
Snippets Groups Projects
Commit 1906eb4c authored by Florent Torregrosa's avatar Florent Torregrosa Committed by Pierre Dureau
Browse files

Issue #3488582 by grimreaper, pdureau: Side effect in links prop type normalization casting

parent 486c760c
No related branches found
No related tags found
1 merge request!270Issue #3488582 by grimreaper, pdureau: Side effect in links prop type normalization casting
Pipeline #353312 passed
......@@ -53,7 +53,7 @@ class AttributesPropType extends PropTypePluginBase {
plugins are expected to return a mapping to not break SDC prop validation
against the prop type schema.
*/
if (is_array($value)) {
if (is_array($value) && !empty($value)) {
// Attribute::createAttributeValue() is already normalizing some stuff:
// - 'class' attribute must be a list
// - MarkupInterface values must be resolved.
......
......@@ -115,20 +115,14 @@ class LinksPropType extends PropTypePluginBase implements ContainerFactoryPlugin
*/
protected static function normalizeLink(array $item): array {
$item = self::normalizeAttributes($item);
// Do not normalize title as it can be a string or a renderable array.
if (array_key_exists("text", $item)) {
// Examples: links.html.twig, breadcrumb.html.twig, pager.html.twig,
// views_mini_pager.html.twig.
if (is_scalar($item["text"]) || $item["text"] instanceof \Stringable) {
$item["title"] = (string) $item["text"];
}
else {
$item["title"] = $item["text"];
}
$item["title"] = $item["text"];
unset($item["text"]);
}
if (!is_string($item["title"])) {
$item["title"] = (string) $item["title"];
}
if (array_key_exists("href", $item)) {
// Examples: pager.html.twig, views_mini_pager.html.twig.
$item["url"] = $item["href"];
......@@ -150,16 +144,13 @@ class LinksPropType extends PropTypePluginBase implements ContainerFactoryPlugin
if (!array_key_exists($property, $item)) {
return $item;
}
if (is_a($item[$property], '\Drupal\Core\Template\Attribute')) {
$item[$property] = $item[$property]->toArray();
}
$item[$property] = AttributesPropType::normalize($item[$property]);
// Empty PHP arrays are converted in JSON arrays instead of JSON objects
// by json_encode(), so it is better to remove them.
if (is_array($item[$property]) && empty($item[$property])) {
if (empty($item[$property])) {
unset($item[$property]);
return $item;
}
$item[$property] = AttributesPropType::normalize($item[$property]);
return $item;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment