Skip to content
Snippets Groups Projects
Commit cd11ed96 authored by Mikael Meulle's avatar Mikael Meulle Committed by christian.wiedemann
Browse files

Issue #3495984 by just_like_good_vibes, grimreaper: [2.0.0-rc2] Configuring a...

Issue #3495984 by just_like_good_vibes, grimreaper: [2.0.0-rc2] Configuring a slot on Layout Builder make the page to redirect on the block form
parent dca22045
Branches
Tags
1 merge request!322Issue #3495984 by just_like_good_vibes, grimreaper: [2.0.0-rc2] Configuring a slot on Layout Builder make the page to redirect on the block form
Pipeline #402998 passed
......@@ -4,9 +4,11 @@ namespace Drupal\ui_patterns\Element;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\HtmlCommand;
use Drupal\Core\Ajax\InvokeCommand;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Render\Element\RenderElementBase;
use Drupal\ui_patterns\SourcePluginBase;
/**
......@@ -152,7 +154,7 @@ class ComponentSlotForm extends ComponentFormBase {
break;
case 'add':
$source_id = $trigger_element['#source_id'];
$source_id = $trigger_element['#source_id'] ?? $trigger_element["#value"];
$value['sources'][] = [
'source_id' => $source_id,
'source' => [],
......@@ -198,14 +200,6 @@ class ComponentSlotForm extends ComponentFormBase {
return $element;
}
/**
* Returns the dropdown options array.
*/
private static function getSourceOptions(array $element, array $definition):array {
$sources = static::getSources($element['#slot_id'], $definition, $element);
return static::sourcesToOptions($sources, FALSE);
}
/**
* Removes the first occurrence of the <thead> element from an HTML string.
*
......@@ -353,66 +347,33 @@ class ComponentSlotForm extends ComponentFormBase {
* Build source selector.
*/
protected static function buildSourceSelector(array $element, array $definition, string $wrapper_id): array {
$options = self::getSourceOptions($element, $definition);
$slot_id = $element['#slot_id'];
$action_buttons = [];
foreach ($options as $option_key => $group_or_source_label) {
$group_of_sources = is_array($group_or_source_label) ? $group_or_source_label : [$option_key => $group_or_source_label];
foreach ($group_of_sources as $source_id => $source_label) {
$label = ($source_id === array_key_first($options)) ? t('Add %source', ['%source' => $source_label]) : $source_label;
$action_buttons[$source_id] = static::expandComponentButton($element, [
'#type' => 'submit',
'#name' => strtr($slot_id, '-', '_') . implode('-', $element['#array_parents']) . '_' . $source_id . '_add_more',
'#value' => $label,
'#submit' => [
static::class . '::rebuildForm',
],
'#access' => TRUE,
'#source_id' => $source_id,
'#ui_patterns_slot_operation' => 'add',
'#ui_patterns_slot' => TRUE,
'#ui_patterns_slot_parents' => $element['#parents'],
'#ui_patterns_slot_array_parents' => $element['#array_parents'],
'#ajax' => [
'callback' => [
static::class,
'refreshForm',
],
'wrapper' => $wrapper_id,
'effect' => 'fade',
],
]);
}
}
return static::buildComponentDropbutton($action_buttons);
}
/**
* Build drop button.
*
* @param array $elements
* Elements for drop button.
*
* @return array
* Drop button array.
*/
protected static function buildComponentDropbutton(array $elements = []): array {
$build = [
'#type' => 'dropbutton',
'#dropbutton_type' => 'small',
'#links' => [],
$sources = static::getSources($slot_id, $definition, $element);
$options = static::sourcesToOptions($sources);
return [
"#type" => "select",
"#empty_option" => t("- Select a source to add -"),
"#options" => $options,
'#submit' => [
static::class . '::rebuildForm',
],
'#access' => TRUE,
'#ui_patterns_slot_operation' => 'add',
'#ui_patterns_slot' => TRUE,
'#ui_patterns_slot_parents' => $element['#parents'],
'#ui_patterns_slot_array_parents' => $element['#array_parents'],
/*'#attributes' => [
'id' => $wrapper_id . "-add",
],*/
'#ajax' => [
'callback' => [
static::class,
'refreshForm',
],
'wrapper' => $wrapper_id,
'effect' => 'fade',
],
];
// Because we are cloning the elements into title sub element we need to
// sort children first.
foreach (Element::children($elements, TRUE) as $child) {
// Clone the element as an operation.
$build["#links"][$child] = ['title' => $elements[$child]];
$build["#links"][$child]['title'] = RenderElementBase::preRenderAjaxForm($build["#links"][$child]['title']);
// Flag the original element as printed so it doesn't render twice.
$elements[$child]['#printed'] = TRUE;
}
return $build + $elements;
}
/**
......@@ -466,10 +427,19 @@ class ComponentSlotForm extends ComponentFormBase {
/**
* Ajax handler: Refresh sources form.
*/
public static function refreshForm(array $form, FormStateInterface $form_state) : ?array {
$parents = $form_state->getTriggeringElement()['#ui_patterns_slot_array_parents'];
public static function refreshForm(array $form, FormStateInterface $form_state) : mixed {
$triggering_element = $form_state->getTriggeringElement();
$wrapper_id = $triggering_element['#ajax']['wrapper'];
$parents = $triggering_element['#ui_patterns_slot_array_parents'];
$form_state->setRebuild(TRUE);
return NestedArray::getValue($form, $parents);
$returned = NestedArray::getValue($form, $parents);
$response = new AjaxResponse();
$response->addCommand(new HtmlCommand('#' . $wrapper_id, $returned));
if (isset($triggering_element["#ui_patterns_slot_operation"]) && $triggering_element["#ui_patterns_slot_operation"] === "add") {
$selector = "#" . $triggering_element["#id"];
$response->addCommand(new InvokeCommand($selector, "val", [""]));
}
return $response;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment