diff --git a/src/Element/ComponentFormBase.php b/src/Element/ComponentFormBase.php index fb2fd0fa5d921f622d300c32b096b8e81b91f5a3..17d2c9a6ce911bcc9d9f4f30f536ca1d00b30b22 100644 --- a/src/Element/ComponentFormBase.php +++ b/src/Element/ComponentFormBase.php @@ -22,6 +22,32 @@ abstract class ComponentFormBase extends FormElementBase implements TrustedCallb return ['preRenderPropOrSlot', 'processPropOrSlot']; } + /** + * Check if the form element needs a details. + * + * @param array $element + * The form element. + * + * @return string|null + * Prop or slot id if the form element needs a details. + */ + protected static function checkDetailsElement(array &$element) : ?string { + if (!isset($element["#wrap"]) || !$element["#wrap"]) { + return NULL; + } + $prop_or_slot_id = $element["#prop_id"] ?? $element["#slot_id"]; + $title_in_component = $element["#title_in_component"] ?? $prop_or_slot_id; + $title = !empty($element['#title']) ? $element['#title'] : $title_in_component; + if (!array_key_exists($prop_or_slot_id, $element)) { + $element[$prop_or_slot_id] = [ + "#type" => "details", + "#title" => $title, + "#open" => FALSE, + ]; + } + return $prop_or_slot_id; + } + /** * Customize slot or prop form elements (pre-render). * @@ -32,17 +58,8 @@ abstract class ComponentFormBase extends FormElementBase implements TrustedCallb * Processed element */ public static function preRenderPropOrSlot(array $element) : array { - if (isset($element["#wrap"]) && $element["#wrap"]) { - $prop_or_slot_id = $element["#prop_id"] ?? $element["#slot_id"]; - $title = !empty($element['#title']) ? $element['#title'] : $prop_or_slot_id; + if ($prop_or_slot_id = static::checkDetailsElement($element)) { $children_keys = Element::children($element); - if (!array_key_exists($prop_or_slot_id, $element)) { - $element[$prop_or_slot_id] = [ - "#type" => "details", - "#title" => $title, - "#open" => FALSE, - ]; - } foreach ($children_keys as $child_key) { if ($child_key === $prop_or_slot_id) { continue; @@ -67,17 +84,7 @@ abstract class ComponentFormBase extends FormElementBase implements TrustedCallb */ public static function processPropOrSlot(array &$element, FormStateInterface $form_state) { $triggering_element = $form_state->getTriggeringElement(); - if (isset($element["#wrap"]) && $element["#wrap"]) { - $prop_or_slot_id = $element["#prop_id"] ?? $element["#slot_id"]; - $title = !empty($element['#title']) ? $element['#title'] : $prop_or_slot_id; - if (!array_key_exists($prop_or_slot_id, $element)) { - $element[$prop_or_slot_id] = [ - "#type" => "details", - "#title" => $title, - "#open" => FALSE, - ]; - } - + if ($prop_or_slot_id = static::checkDetailsElement($element)) { if (is_array($triggering_element) && isset($triggering_element["#array_parents"]) && is_array($triggering_element["#array_parents"])) { $element_array_parents = $element["#array_parents"]; $trigger_array_parents = $triggering_element["#array_parents"]; diff --git a/src/Element/ComponentPropsForm.php b/src/Element/ComponentPropsForm.php index 3a43701a766cbc943fc6a4025244569e4a4dfbde..e980d752c78f27b66342c5cd1fffa462d9db1c05 100644 --- a/src/Element/ComponentPropsForm.php +++ b/src/Element/ComponentPropsForm.php @@ -74,18 +74,14 @@ class ComponentPropsForm extends ComponentFormBase { return $element; } $configuration = $element['#default_value']['props'] ?? []; - $is_first_prop = TRUE; $prop_heading = new FormattableMarkup("<p><strong>@title</strong></p>", ["@title" => t("Props")]); + $element[] = [ + '#markup' => $prop_heading, + ]; foreach ($props as $prop_id => $prop) { if ($prop_id === 'variant') { continue; } - if ($is_first_prop) { - $element[] = [ - '#markup' => $prop_heading, - ]; - $is_first_prop = FALSE; - } $prop_type = $prop['ui_patterns']['type_definition']; $element[$prop_id] = [ '#type' => 'component_prop_form', @@ -95,8 +91,6 @@ class ComponentPropsForm extends ComponentFormBase { '#tag_filter' => $element['#tag_filter'], '#component_id' => $component->getPluginId(), '#prop_id' => $prop_id, - '#prefix' => "<div class='component-form-prop'>", - '#suffix' => "</div>", '#wrap' => TRUE, ]; } diff --git a/src/Element/ComponentSlotForm.php b/src/Element/ComponentSlotForm.php index 6f4a7d5b5bc1209d47549615f8bcc08cf15d5b0e..589ebe00defb69c7b317be4f21e246d831e6c083 100644 --- a/src/Element/ComponentSlotForm.php +++ b/src/Element/ComponentSlotForm.php @@ -78,6 +78,7 @@ class ComponentSlotForm extends ComponentFormBase { [$class, 'preRenderPropOrSlot'], ], "#wrap" => TRUE, + "#title_in_component" => NULL, ]; } @@ -109,6 +110,7 @@ class ComponentSlotForm extends ComponentFormBase { $wrapper_id = static::getElementId($element, 'ui-patterns-slot-' . $slot_id); $element['#tree'] = TRUE; $element['#table_title'] = $element['#title']; + $element['#title_in_component'] = $element['#title']; $element['#title'] = ''; $element['sources'] = static::buildSourcesForm($element, $form_state, $definition, $wrapper_id); if ($element['#cardinality_multiple'] === TRUE || diff --git a/src/Element/ComponentSlotsForm.php b/src/Element/ComponentSlotsForm.php index 605083b29dba6f386a3445d9733d052f85a5f403..b405123e1defdd1d7dfc997ff24ac3496069b4c6 100644 --- a/src/Element/ComponentSlotsForm.php +++ b/src/Element/ComponentSlotsForm.php @@ -72,15 +72,11 @@ class ComponentSlotsForm extends ComponentFormBase { } $contexts = $element['#source_contexts'] ?? []; $configuration = $element['#default_value']['slots'] ?? []; - $is_first_slot = TRUE; $slot_heading = new FormattableMarkup("<p><strong>@title</strong></p>", ["@title" => t("Slots")]); + $element[] = [ + '#markup' => $slot_heading, + ]; foreach ($component->metadata->slots as $slot_id => $slot) { - if ($is_first_slot) { - $element[] = [ - '#markup' => $slot_heading, - ]; - $is_first_slot = FALSE; - } $element[$slot_id] = [ '#title' => $slot['title'] ?? '', '#type' => 'component_slot_form', @@ -92,7 +88,6 @@ class ComponentSlotsForm extends ComponentFormBase { '#prefix' => "<div class='component-form-slot'>", '#suffix' => "</div>", ]; - } return $element; }