Skip to content
Snippets Groups Projects

Issue #3510596 by goz, just_like_good_vibes: [2.0.4] Improve prop value rendering for booleans

Merged Issue #3510596 by goz, just_like_good_vibes: [2.0.4] Improve prop value rendering for booleans
Merged Mikael Meulle requested to merge issue/ui_patterns-3510596:3510596-improved into 2.0.x
Files
7
@@ -98,19 +98,39 @@ class ComponentElementBuilder implements TrustedCallbackInterface {
}
}
else {
if (!empty($data) || $prop_type->getPluginId() === 'attributes') {
// For JSON Schema validator, empty value is not the same as missing
// value, and we want to prevent some of the prop types rules to be
// applied on empty values: string pattern, string format,
// enum, number min/max...
// However, we don't remove empty attributes to avoid an error with
// Drupal\Core\Template\TwigExtension::createAttribute() when themers
// forget to use the default({}) filter.
if (!static::isPropValueEmpty($data, $prop_type)) {
$build['#props'][$prop_or_slot_id] = $data;
}
}
}
/**
* Determine if a prop value is empty.
*
* @param mixed $data
* The prop value.
* @param \Drupal\ui_patterns\PropTypeInterface $prop_type
* Target prop type.
*
* @return bool
* TRUE if the prop value is empty, FALSE otherwise.
*/
protected static function isPropValueEmpty(mixed $data, PropTypeInterface $prop_type): bool {
// For JSON Schema validator, empty value is not the same as missing
// value, and we want to prevent some of the prop types rules to be
// applied on empty values: string pattern, string format,
// enum, number min/max...
// However, we don't remove empty attributes to avoid an error with
// Drupal\Core\Template\TwigExtension::createAttribute() when themers
// forget to use the default({}) filter.
// For boolean values, we only remove NULL values.
return match ($prop_type->getPluginId()) {
'attributes' => FALSE,
'boolean' => ($data === NULL),
default => empty($data),
};
}
/**
* Update the build array for a configured source on a prop/slot.
*
Loading