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
7 files
+ 141
24
Compare changes
  • Side-by-side
  • Inline
Files
7
@@ -98,19 +98,39 @@ class ComponentElementBuilder implements TrustedCallbackInterface {
@@ -98,19 +98,39 @@ class ComponentElementBuilder implements TrustedCallbackInterface {
}
}
}
}
else {
else {
if (!empty($data) || $prop_type->getPluginId() === 'attributes') {
if (!static::isPropValueEmpty($data, $prop_type)) {
// 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.
$build['#props'][$prop_or_slot_id] = $data;
$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.
* Update the build array for a configured source on a prop/slot.
*
*
Loading