Skip to content
Snippets Groups Projects

Issue #3505217 by pdureau: Support Single directory components

2 files
+ 293
44
Compare changes
  • Side-by-side
  • Inline
Files
2
@@ -11,7 +11,7 @@ namespace Drupal\ui_examples;
* Let's put them back.
*
* Before: ["type" => "component", "component" => "example:card"]
* After: ["#type" => "component", "#component" => "example:card"]
* After: ["#type" => "component", "#component" => "example:card"]
*/
class ExampleSyntaxConverter implements ExampleSyntaxConverterInterface {
@@ -29,6 +29,49 @@ class ExampleSyntaxConverter implements ExampleSyntaxConverterInterface {
'#type',
];
public const KNOWN_PROPERTIES = [
'type' => [
'html_tag' => [
'attached',
'attributes',
'tag',
'type',
'value',
],
],
'theme' => [
'layout' => [
'attached',
'attributes',
'theme',
'settings',
],
],
];
/**
* List of render properties which should have been children instead.
*/
public const PROPERTIES_INSTEAD_OF_CHILDREN = [
'type' => [
'component' => [
'slots',
],
],
'theme' => [
'status_messages' => [
'message_list',
],
'table' => [
'header',
'rows',
'footer',
'empty',
'caption',
],
],
];
/**
* {@inheritdoc}
*/
@@ -55,43 +98,111 @@ class ExampleSyntaxConverter implements ExampleSyntaxConverterInterface {
* The processed render array.
*/
protected function convertRenderArray(array $renderable): array {
foreach ($renderable as $property => $value) {
if (\is_array($value)) {
$renderable[$property] = $this->convertArray($value);
// Weird detection.
if (isset($renderable['type'], self::PROPERTIES_INSTEAD_OF_CHILDREN['type'][$renderable['type']])) {
return $this->convertWeirdRenderArray($renderable, self::PROPERTIES_INSTEAD_OF_CHILDREN['type'][$renderable['type']]);
}
if (isset($renderable['theme'])) {
$baseThemeHook = \explode('__', $renderable['theme'])[0];
if (isset(self::PROPERTIES_INSTEAD_OF_CHILDREN['theme'][$baseThemeHook])) {
return $this->convertWeirdRenderArray($renderable, self::PROPERTIES_INSTEAD_OF_CHILDREN['theme'][$baseThemeHook]);
}
}
$in_html_tag = (isset($renderable['type']) && $renderable['type'] === 'html_tag');
$html_tag_allowed_render_keys = [
'attached',
'attributes',
'tag',
'type',
'value',
];
$in_layout = (isset($renderable['theme']) && \explode('__', $renderable['theme'])[0] === 'layout');
$layout_allowed_render_keys = [
'attached',
'attributes',
'theme',
'settings',
];
// Normal with special case detection.
if (isset($renderable['type'], self::KNOWN_PROPERTIES['type'][$renderable['type']])) {
return $this->convertNormalRenderArray($renderable, self::KNOWN_PROPERTIES['type'][$renderable['type']]);
}
if (isset($renderable['theme'])) {
$baseThemeHook = \explode('__', $renderable['theme'])[0];
if (isset(self::KNOWN_PROPERTIES['theme'][$baseThemeHook])) {
return $this->convertNormalRenderArray($renderable, self::KNOWN_PROPERTIES['theme'][$baseThemeHook]);
}
}
return $this->convertNormalRenderArray($renderable, []);
}
/**
* Add property prefix.
*
* @param array $renderable
* The renderable array.
* @param mixed $property
* The property.
*
* @return array
* The array with prefixed property.
*/
protected function convertProperty(array $renderable, mixed $property): array {
if (!\is_string($property)) {
return $renderable;
}
if (\str_starts_with($property, '#')) {
return $renderable;
}
$renderable['#' . $property] = $renderable[$property];
unset($renderable[$property]);
return $renderable;
}
/**
* To convert "normal" render array.
*
* A "normal" render arrays is an array:
* - where properties (key starts with a '#') are not renderables
* - children (key does not start with a '#') are only renderables.
*
* Examples:
* - html_tag which is forbidding renderables in #value
* - layout where every region is a child.
*
* @param array $renderable
* The renderable array.
* @param array $knownProperties
* The list of know properties.
*
* @return array
* The converted array.
*/
protected function convertNormalRenderArray(array $renderable, array $knownProperties): array {
foreach ($renderable as $property => $value) {
if (!\is_string($property)) {
continue;
if (empty($knownProperties) && \is_string($property)) {
// Default to add prefix to every entries.
$renderable = $this->convertProperty($renderable, $property);
}
// html_tag is special.
if ($in_html_tag && !\in_array($property, $html_tag_allowed_render_keys, TRUE)) {
continue;
elseif (\in_array($property, $knownProperties, TRUE)) {
// We add # prefix only to known properties.
$renderable = $this->convertProperty($renderable, $property);
}
// Layouts are special.
if ($in_layout && !\in_array($property, $layout_allowed_render_keys, TRUE)) {
continue;
elseif (\is_array($value)) {
// Other keys may have children so let's drill.
$renderable[$property] = $this->convertArray($value);
}
if (\str_starts_with($property, '#')) {
continue;
}
return $renderable;
}
/**
* The "weird" render arrays, where renderables are found only in properties.
*
* Examples: component with #slots, table with #rows...
*
* @param array $renderable
* The renderable array.
* @param array $propertiesWithRenderables
* The list of properties to look for.
*
* @return array
* The updated renderable array.
*/
protected function convertWeirdRenderArray(array $renderable, array $propertiesWithRenderables): array {
foreach ($renderable as $property => $value) {
if (\in_array($property, $propertiesWithRenderables, TRUE) && \is_array($value)) {
$renderable[$property] = $this->convertArray($value);
}
$renderable['#' . $property] = $value;
unset($renderable[$property]);
// There are no children, so we add a # everywhere.
$renderable = $this->convertProperty($renderable, $property);
}
return $renderable;
}
Loading