Skip to content
Snippets Groups Projects

Resolve #3517343 "Infinite loop with"

Files
4
@@ -187,12 +187,19 @@ abstract class ComponentFormBase extends FormElementBase implements TrustedCallb
/**
* Get source plugin form.
*/
protected static function getSourcePluginForm(FormStateInterface $form_state, SourceInterface $source, string $wrapper_id): array {
protected static function getSourcePluginForm(FormStateInterface $form_state, ?SourceInterface $source, string $wrapper_id): array {
if (!$source) {
return [
"#type" => 'container',
"#attributes" => [
'id' => $wrapper_id,
],
];
}
$form = $source->settingsForm([], $form_state);
$form["#type"] = 'container';
$form['#attributes'] = [
'id' => $wrapper_id,
];
// @phpstan-ignore-next-line
$form['#prefix'] = "<div id='" . $wrapper_id . "'>" . ($form['#prefix'] ?? '');
$form['#suffix'] = ($form['#suffix'] ?? '') . "</div>";
// Weird, but :switchSourceForm() AJAX handler doesn't work without that.
foreach (Element::children($form) as $child) {
if (isset($form[$child]['#description']) && !isset($form[$child]['#description_display'])) {
@@ -221,6 +228,28 @@ abstract class ComponentFormBase extends FormElementBase implements TrustedCallb
return $source;
}
}
return static::selectDefaultSource($sources);
}
/**
* Select default source plugin from a list.
*
* @param array $sources
* The sources.
*
* @return \Drupal\ui_patterns\SourceInterface|null
* The selected source or NULL.
*/
protected static function selectDefaultSource(array $sources): ?SourceInterface {
// Try to return the first widget source.
foreach ($sources as $source) {
/** @var \Drupal\ui_patterns\SourceInterface $source */
$plugin_definition = $source->getPluginDefinition() ?? [];
$tags = is_array($plugin_definition) ? ($plugin_definition["tags"] ?? []) : [];
if (in_array("widget", $tags)) {
return $source;
}
}
return NULL;
}
@@ -244,6 +273,28 @@ abstract class ComponentFormBase extends FormElementBase implements TrustedCallb
return $element;
}
/**
* Add title and description to the source element.
*
* @param array $element
* The element.
*
* @return array
* The element with title and description.
*/
protected static function addTitleAndDescription(array $element): array {
if (isset($element["source"]["value"])) {
$element["source"]["value"]["#title_display"] = 'before';
if (empty($element["source"]["value"]["#title"])) {
$element["source"]["value"]["#title"] = $element["#title"];
}
if (empty($element["source"]["value"]["#description"])) {
$element["source"]["value"]["#description"] = $element['#description'] ?? NULL;
}
}
return $element;
}
/**
* Alter the element after the form is built.
*
@@ -303,7 +354,7 @@ abstract class ComponentFormBase extends FormElementBase implements TrustedCallb
if (empty($sources)) {
return [];
}
if (count($sources) == 1) {
if ($selected_source && (count($sources) == 1)) {
return [
'#type' => 'hidden',
'#value' => array_keys($sources)[0],
@@ -318,6 +369,7 @@ abstract class ComponentFormBase extends FormElementBase implements TrustedCallb
'#attributes' => [
'class' => ["uip-source-selector"],
],
'#empty_option' => t('- Select -'),
// '#prop_id' => $selected_source?->getPropId(),
// '#prop_definition' => $selected_source?->getPropDefinition(),
'#ajax' => [
Loading