Skip to content
Snippets Groups Projects
Commit bbf7b7b7 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, grimreaper, pdureau: Check attributes props default value

See merge request !323
parents 7b030373 a4cdbc25
No related branches found
No related tags found
No related merge requests found
Pipeline #397897 passed
......@@ -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,25 @@ 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);
// Trim because Attribute::__toString() add a space on the left as it is
// intended to be printed in HTML.
return trim((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"
......@@ -31,7 +31,7 @@ props:
$ref: "ui-patterns://boolean"
links:
title: "Links"
$ref: 'ui-patterns://links'
$ref: "ui-patterns://links"
enum_integer:
title: "Enum Integer"
type: "integer"
......@@ -47,8 +47,8 @@ props:
- "2"
- "3"
enum_list:
title: 'Enum List'
$ref: 'ui-patterns://enum_list'
title: "Enum List"
$ref: "ui-patterns://enum_list"
items:
enum:
- A
......@@ -57,20 +57,20 @@ props:
- D
- E
- F
'meta:enum':
A: 'Label A'
B: 'Label B'
C: 'Label C'
D: 'Label D'
E: 'Label E'
F: 'Label F'
"meta:enum":
A: "Label A"
B: "Label B"
C: "Label C"
D: "Label D"
E: "Label E"
F: "Label F"
enum_list_multiple:
title: "Enum List"
type: array
maxItems: 3
items:
type: integer
enum: [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ]
enum: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
enum_set:
title: "Enum Set"
type: array
......@@ -78,7 +78,7 @@ props:
maxItems: 3
items:
type: integer
enum: [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ]
enum: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
list_string:
title: "List String"
type: "array"
......@@ -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