Skip to content
Snippets Groups Projects
Commit d04de2d7 authored by Mikael Meulle's avatar Mikael Meulle
Browse files

Merge branch '3498137-2.0.0-rc2-check-attributes' into '2.0.x'

Issue #3498137 by just_like_good_vibes: Check attributes props default value

See merge request !323
parents 5eea0ecd 5636ec6a
No related branches found
No related tags found
No related merge requests found
Pipeline #396909 failed
......@@ -8,6 +8,7 @@ use Drupal\Component\Utility\Html;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Security\TrustedCallbackInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Template\Attribute;
use Drupal\ui_patterns\Attribute\Source;
use Drupal\ui_patterns\SourcePluginPropValue;
use Drupal\ui_patterns\UnicodePatternValidatorTrait;
......@@ -42,6 +43,23 @@ class AttributesWidget extends SourcePluginPropValue implements TrustedCallbackI
return static::convertValueToAttributesMapping(parent::getPropValue());
}
/**
* {@inheritdoc}
*/
protected function convertPropValueToStoredValue(mixed $propValue): mixed {
if ($propValue === NULL) {
return NULL;
}
if ($propValue instanceof Attribute) {
return (string) $propValue;
}
if (!is_array($propValue)) {
return NULL;
}
$attributes = new Attribute($propValue);
return (string) $attributes;
}
/**
* {@inheritdoc}
*/
......
......@@ -41,12 +41,26 @@ abstract class SourcePluginPropValue extends SourcePluginBase {
protected function getDefaultFromPropDefinition(): mixed {
if (is_array($this->propDefinition) &&
array_key_exists("default", $this->propDefinition)) {
return $this->propDefinition["default"];
// Inside the prop definition,
// the default value is in the prop type's format.
return $this->convertPropValueToStoredValue($this->propDefinition["default"]);
}
return NULL;
}
/**
* Convert from prop type' format to stored settings' format.
*
* @param mixed $propValue
* Prop value in the format described in the prop type.
*
* @return mixed
* Value as stored in the settings.
*/
protected function convertPropValueToStoredValue(mixed $propValue): mixed {
return $propValue;
}
/**
* Merges default settings values into $settings.
*/
......
......@@ -15,3 +15,19 @@ attributes_1:
attributes:
same:
key: value
attributes_2:
component:
component_id: ui_patterns_test:test-component
props:
attributes_with_default:
source_id: attributes
entity:
body:
value: 'value_text_1'
output:
props:
attributes_with_default:
same:
data-hello: "world"
class: "hello-world-class hello-world-class-2"
......@@ -114,6 +114,14 @@ props:
attributes_class:
title: "Attributes when type is a PHP namespace"
type: 'Drupal\Core\Template\Attribute'
attributes_with_default:
title: "Attributes (with default value)"
"$ref": "ui-patterns://attributes"
default:
data-hello: 'world'
class:
- 'hello-world-class'
- 'hello-world-class-2'
slots:
slot:
title: "Slot"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment