Skip to content
Snippets Groups Projects

Resolve #3517343 "Infinite loop with"

Files
4
@@ -187,12 +187,18 @@ abstract class ComponentFormBase extends FormElementBase implements TrustedCallb
@@ -187,12 +187,18 @@ abstract class ComponentFormBase extends FormElementBase implements TrustedCallb
/**
/**
* Get source plugin form.
* 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 = $source->settingsForm([], $form_state);
$form["#type"] = 'container';
$form['#prefix'] = "<div id='" . $wrapper_id . "'>" . $form['#prefix'] ?? '';
$form['#attributes'] = [
$form['#suffix'] = ($form['#suffix'] ?? '') . "</div>";
'id' => $wrapper_id,
];
// Weird, but :switchSourceForm() AJAX handler doesn't work without that.
// Weird, but :switchSourceForm() AJAX handler doesn't work without that.
foreach (Element::children($form) as $child) {
foreach (Element::children($form) as $child) {
if (isset($form[$child]['#description']) && !isset($form[$child]['#description_display'])) {
if (isset($form[$child]['#description']) && !isset($form[$child]['#description_display'])) {
@@ -214,13 +220,17 @@ abstract class ComponentFormBase extends FormElementBase implements TrustedCallb
@@ -214,13 +220,17 @@ abstract class ComponentFormBase extends FormElementBase implements TrustedCallb
/**
/**
* Get selected source plugin.
* Get selected source plugin.
*/
*/
protected static function getSelectedSource(array $configuration, array $sources): ?SourceInterface {
protected static function getSelectedSource(array $configuration, array $sources, bool $auto_select = FALSE): ?SourceInterface {
$source_id = $configuration['source_id'] ?? NULL;
$source_id = $configuration['source_id'] ?? NULL;
foreach ($sources as $source) {
foreach ($sources as $source) {
if ($source->getPluginId() === $source_id) {
if ($source->getPluginId() === $source_id) {
return $source;
return $source;
}
}
}
}
 
if (($auto_select && count($sources) > 0) || (count($sources) == 1)) {
 
// Default source is in first position.
 
return current($sources);
 
}
return NULL;
return NULL;
}
}
@@ -244,6 +254,28 @@ abstract class ComponentFormBase extends FormElementBase implements TrustedCallb
@@ -244,6 +254,28 @@ abstract class ComponentFormBase extends FormElementBase implements TrustedCallb
return $element;
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.
* Alter the element after the form is built.
*
*
@@ -303,7 +335,7 @@ abstract class ComponentFormBase extends FormElementBase implements TrustedCallb
@@ -303,7 +335,7 @@ abstract class ComponentFormBase extends FormElementBase implements TrustedCallb
if (empty($sources)) {
if (empty($sources)) {
return [];
return [];
}
}
if (count($sources) == 1) {
if ($selected_source && (count($sources) == 1)) {
return [
return [
'#type' => 'hidden',
'#type' => 'hidden',
'#value' => array_keys($sources)[0],
'#value' => array_keys($sources)[0],
@@ -318,6 +350,7 @@ abstract class ComponentFormBase extends FormElementBase implements TrustedCallb
@@ -318,6 +350,7 @@ abstract class ComponentFormBase extends FormElementBase implements TrustedCallb
'#attributes' => [
'#attributes' => [
'class' => ["uip-source-selector"],
'class' => ["uip-source-selector"],
],
],
 
'#empty_option' => t('- Select -'),
// '#prop_id' => $selected_source?->getPropId(),
// '#prop_id' => $selected_source?->getPropId(),
// '#prop_definition' => $selected_source?->getPropDefinition(),
// '#prop_definition' => $selected_source?->getPropDefinition(),
'#ajax' => [
'#ajax' => [
Loading