dkan
dkan copied to clipboard
Standardize UI widget extentions
We have a WidgetRouter in JSON Form Widget that has gotten a bit unwieldy. It is not exactly a router it is also a collection of methods that all transform default field types into custom ones based on UI schema. This would be better implemented if each field type/widget/whatever the right word would be was its own class, following am interface. The most obvious pattern would be to implement these as plugins. So for instance, WidgetRouter::handleTextAreaElement(), currently like this:
/**
* Helper function for getting a textarea element.
*
* @param mixed $spec
* Object with spec for UI options.
* @param array $element
* Element to convert into textarea.
*
* @return array
* The element configured as textarea.
*/
public function handleTextareaElement(mixed $spec, array $element) {
$element['#type'] = 'textarea';
unset($element['#maxlength']);
if (isset($spec->textFormat)) {
$element['#type'] = 'text_format';
$element['#format'] = $spec->textFormat;
$element['#allowed_formats'] = [
$spec->textFormat,
];
}
if (isset($spec->rows)) {
$element['#rows'] = $spec->rows;
}
if (isset($spec->cols)) {
$element['#cols'] = $spec->cols;
}
return $element;
}
might become a class called TextArea, something like this:
class TextArea implements JsonWidgetInterface {
/**
* {@inheritdoc}
*/
public function applyWidget(mixed $spec, array $element): array {
...
}
}
Not fully thought-through but something like this would make this all cleaner.