From b0c17adcd1b225be56323848fe46f5bb0d6ef333 Mon Sep 17 00:00:00 2001
From: Pierre <pierredureau@yahoo.fr>
Date: Sun, 14 Jan 2024 18:21:22 +0100
Subject: [PATCH] Issue #3395963 by pdureau: Fix select & textfield sources.

---
 src/ComponentPluginManager.php                | 16 ++++++++++++++
 src/Plugin/UiPatterns/Source/SelectWidget.php |  6 +++++-
 .../UiPatterns/Source/TextfieldWidget.php     |  3 +--
 ui_patterns.module                            | 21 -------------------
 4 files changed, 22 insertions(+), 24 deletions(-)

diff --git a/src/ComponentPluginManager.php b/src/ComponentPluginManager.php
index a715c0970..f0b9bedaa 100644
--- a/src/ComponentPluginManager.php
+++ b/src/ComponentPluginManager.php
@@ -67,12 +67,27 @@ class ComponentPluginManager extends SdcPluginManager implements CategorizingPlu
     parent::alterDefinitions($definitions);
     $fallback_prop_type_id = $this->propTypePluginManager->getFallbackPluginId("");
     foreach ($definitions as $component_id => $definition) {
+      $definition["name"] = $definition["name"] ?? $component_id;
+      $definition = $this->alterSlots($definition);
       $definition = $this->annotateProps($definition, $fallback_prop_type_id);
       $definition = $this->resolveTemplatePath($definition);
       $definitions[$component_id] = $definition;
     }
   }
 
+  /**
+   * Alter slots.
+   */
+  protected function alterSlots(array $definition): array {
+    if (!isset($definition['slots'])) {
+      return $definition;
+    }
+    foreach ($definition['slots'] as $slot_id => $slot) {
+      $definition['slots'][$slot_id]["title"] = $slot["title"] ?? $slot_id;
+    }
+    return $definition;
+  }
+
   /**
    * Annotate each prop in a component definition.
    *
@@ -87,6 +102,7 @@ class ComponentPluginManager extends SdcPluginManager implements CategorizingPlu
       return $definition;
     }
     foreach ($definition['props']['properties'] as $prop_id => $prop) {
+      $definition["title"] = $definition["title"] ?? $prop_id;
       $prop_type = $this->propTypePluginManager->guessFromSchema($prop);
       if ($prop_type->getPluginId() === $fallback_prop_type_id) {
         $prop_type_adapter = $this->prop_type_adapterPluginManager->guessFromSchema($prop);
diff --git a/src/Plugin/UiPatterns/Source/SelectWidget.php b/src/Plugin/UiPatterns/Source/SelectWidget.php
index abbfda284..6cfaedf6d 100644
--- a/src/Plugin/UiPatterns/Source/SelectWidget.php
+++ b/src/Plugin/UiPatterns/Source/SelectWidget.php
@@ -35,12 +35,16 @@ class SelectWidget extends SourcePluginBase {
     foreach ($this->propDefinition['enum'] as $item) {
       $options[$item] = $item;
     }
-    return [
+    $form = [
       '#type' => 'select',
       '#title' => $this->propDefinition['title'],
       '#default_value' => $this->getConfigurationFormValue(),
       "#options" => $options,
     ];
+    // With Firefox, autocomplete may override #default_value.
+    // https://drupal.stackexchange.com/questions/257732/default-value-not-working-in-select-field
+    $form['#attributes']['autocomplete'] = 'off';
+    return $form;
   }
 
 }
diff --git a/src/Plugin/UiPatterns/Source/TextfieldWidget.php b/src/Plugin/UiPatterns/Source/TextfieldWidget.php
index 684a1c5ef..cb88e65f4 100644
--- a/src/Plugin/UiPatterns/Source/TextfieldWidget.php
+++ b/src/Plugin/UiPatterns/Source/TextfieldWidget.php
@@ -15,8 +15,7 @@ use Drupal\ui_patterns\SourcePluginBase;
  *   description = @Translation("One-line text field."),
  *   prop_types = {
  *     "string",
- *     "machine_name",
- *     "slot"
+ *     "machine_name"
  *   }
  * )
  */
diff --git a/ui_patterns.module b/ui_patterns.module
index ff150c1c2..e4709b87c 100644
--- a/ui_patterns.module
+++ b/ui_patterns.module
@@ -43,24 +43,3 @@ function template_preprocess_ui_patterns_actions(&$variables) {
     $variables['dropdown_actions'] = $element['dropdown_actions'];
   }
 }
-
-/**
- * Implements hook_preprocess_HOOK() for field_multiple_value_form().
- */
-function ui_patterns_preprocess_field_multiple_value_form(&$variables) {
-  // @todo Move header buttons to own column.
-  /*
-  if (!empty($variables['table']['#header']) && isset($variables['table']['#rows'][0])) {
-  // Find paragraph_actions and move to header.
-  // @see template_preprocess_field_multiple_value_form()
-  if (is_array($variables['table']['#rows'][0]['data'][1]) && !empty($variables['table']['#rows'][0]['data'][1]['data']['_remove']['#ui_patterns_header'])) {
-  $variables['table']['#header'][0]['data'] = [
-  'title' => $variables['table']['#header'][0]['data'],
-  'button' => $variables['table']['#rows'][0]['data'][1]['_remove']['data'],
-  ];
-  unset($variables['table']['#rows'][0]);
-  }
-  }
-   */
-
-}
-- 
GitLab