From a81118304224ff4a0fb7729e20f8d3d5a43e026b Mon Sep 17 00:00:00 2001
From: Eric Volkernick <eric@devcollaborative.com>
Date: Thu, 6 Feb 2025 17:29:17 -0500
Subject: [PATCH] 3446040: to prevent errors in some situations, build a simple
 version of the facet element initially rather than returning a completely
 empty element.

---
 .../src/Plugin/views/filter/FacetsFilter.php  | 32 +++++++++++--------
 1 file changed, 18 insertions(+), 14 deletions(-)

diff --git a/modules/facets_exposed_filters/src/Plugin/views/filter/FacetsFilter.php b/modules/facets_exposed_filters/src/Plugin/views/filter/FacetsFilter.php
index 93ff1504..872d5ffb 100644
--- a/modules/facets_exposed_filters/src/Plugin/views/filter/FacetsFilter.php
+++ b/modules/facets_exposed_filters/src/Plugin/views/filter/FacetsFilter.php
@@ -84,7 +84,22 @@ class FacetsFilter extends FilterPluginBase {
    * {@inheritdoc}
    */
   public function valueForm(&$form, FormStateInterface $form_state) {
-    $form['value'] = [];
+    // Build a version of the element without options, they'll be added later.
+    $form['value'] = [
+      '#type' => 'select',
+      '#options' => [],
+      '#multiple' => $this->options["expose"]["multiple"],
+    ];
+
+    $exposed_form_type = $this->displayHandler->getPlugin('exposed_form')->getPluginId();
+    if ($exposed_form_type == 'bef') {
+      $form['value']['#process'] = ['facets_exposed_filters_remove_validation'];
+    }
+    else {
+      // We need to merge the existing #process callbacks with our own.
+      $select_element = \Drupal::service('element_info')->getInfo('select');
+      $form['value']['#process'] = array_merge($select_element["#process"], ['facets_exposed_filters_remove_validation']);
+    }
 
     // Extra checks when in views UI.
     if (isset($_POST["form_id"]) && $_POST["form_id"] === 'view_preview_form') {
@@ -165,23 +180,12 @@ class FacetsFilter extends FilterPluginBase {
       facets_exposed_filters_get_processed_facet($this->view->id(), $this->view->current_display, $this->options["id"], $facet);
     }
 
-    // We need to merge the existing #process callbacks with our own.
-    $select_element = \Drupal::service('element_info')->getInfo('select');
-
     $this->value = $facet->getActiveItems();
     // Store processed results so other modules can use these.
     $this->facet_results = $facet->getResults();
-    $form['value'] = [
-      '#type' => 'select',
-      '#options' => $this->buildOptions($facet->getResults(), $facet),
-      '#multiple' => $this->options["expose"]["multiple"],
-      '#process' => array_merge($select_element["#process"], ['facets_exposed_filters_remove_validation']),
-    ];
 
-    $exposed_form_type = $this->displayHandler->getPlugin('exposed_form')->getPluginId();
-    if ($exposed_form_type == 'bef') {
-      $form['value']['#process'] = ['facets_exposed_filters_remove_validation'];
-    }
+    // Populate the element with facet's options.
+    $form['value']['#options'] = $this->buildOptions($facet->getResults(), $facet);
   }
 
   /**
-- 
GitLab