Skip to content
Snippets Groups Projects
Commit 023c7299 authored by Pierre Dureau's avatar Pierre Dureau
Browse files

Issue #3482208 by pdureau: Fix attributes merging

parent e2066636
Branches
Tags
1 merge request!249Issue #3482208 by pdureau: Fix attributes merging
Pipeline #317361 passed
......@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Drupal\ui_patterns\Element;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Plugin\Component;
use Drupal\Core\Security\TrustedCallbackInterface;
use Drupal\Core\Theme\ComponentPluginManager;
......@@ -120,7 +121,13 @@ class ComponentElementAlter implements TrustedCallbackInterface {
if (is_a($element["#attributes"], '\Drupal\Core\Template\Attribute')) {
$element["#attributes"] = $element["#attributes"]->toArray();
}
$element["#props"]["attributes"] = array_merge(
// Like \Drupal\Core\Template\Attribute::merge(), we use
// NestedArray::mergeDeep().
// This function is similar to PHP's array_merge_recursive() function, but
// it handles non-array values differently. When merging values that are
// not both arrays, the latter value replaces the former rather than
// merging with it.
$element["#props"]["attributes"] = NestedArray::mergeDeep(
$element["#attributes"],
$element["#props"]["attributes"]
);
......
......@@ -29,7 +29,7 @@ class AttributesWidget extends SourcePluginBase {
// possible anymore because SDC will not validate it against the prop
// type schema.
$value = $this->getSetting('value');
if (!is_string($value)) {
if (empty($value) || !is_string($value)) {
return [];
}
return $this->convertStringToAttributesMapping($value);
......
......@@ -14,7 +14,7 @@ use Drupal\ui_patterns\SourcePluginPropValue;
*/
#[Source(
id: 'class_attribute',
label: new TranslatableMarkup('Class attribute'),
label: new TranslatableMarkup('HTML classes'),
description: new TranslatableMarkup('A space-separated list of HTML classes.'),
prop_types: ['attributes']
)]
......@@ -28,6 +28,9 @@ class ClassAttributeWidget extends SourcePluginPropValue {
// possible anymore because SDC will not validate it against the prop
// type schema.
$value = parent::getPropValue();
if (empty($value)) {
return [];
}
return is_string($value) ? $this->convertStringToAttributesMapping($value) : [];
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment