From 602bc7cd922edc54a05efc111ed09971e046d2f4 Mon Sep 17 00:00:00 2001
From: Eric Bremner <ebremner@uwaterloo.ca>
Date: Thu, 1 Feb 2024 14:56:02 -0500
Subject: [PATCH] ISTWCMS-6711: trying to fix the details labels

---
 templates/elements/details.html.twig |  2 +-
 uw_fdsu_theme_resp.theme             | 41 +++++++++++++++++++++++-----
 2 files changed, 35 insertions(+), 8 deletions(-)

diff --git a/templates/elements/details.html.twig b/templates/elements/details.html.twig
index 07ab4915..5ccb9c2e 100644
--- a/templates/elements/details.html.twig
+++ b/templates/elements/details.html.twig
@@ -30,7 +30,7 @@ Prefix 'details' class to avoid collision with Modernizr.
     ]
     %}
     <summary{{ summary_attributes.addClass(summary_classes) }}>
-      <label for="{{ form_field_id }}">{{ title }}</label>
+      <label{% if form_field_id %} for="{{ form_field_id }}"{% endif %}>{{ title }}</label>
     </summary>
   {%- endif -%}
   <div class="seven-details__wrapper details-wrapper">
diff --git a/uw_fdsu_theme_resp.theme b/uw_fdsu_theme_resp.theme
index 05f12bf9..77304427 100644
--- a/uw_fdsu_theme_resp.theme
+++ b/uw_fdsu_theme_resp.theme
@@ -693,11 +693,38 @@ function uw_fdsu_theme_resp_preprocess_container(&$variables) {
  */
 function uw_fdsu_theme_resp_preprocess_details(&$variables) {
 
-  // Add the form field id to the variables.
-  // This comes from ISTWCMS-6711, we are
-  // removing the -collapsible if there is
-  // one there and using the html id.  If there
-  // is no -collapsible, it will just use the
-  // html id.
-  $variables['form_field_id'] = str_replace('-collapsible', '', $variables['attributes']['id']);
+  // ISTWCMS-6711: this comes from the label not having the
+  // correct for in it.  So we are going to check if this is
+  // a better exposed filter and if so then add the correct
+  // based on the type of exposed filter it is.  The date
+  // filter needs special treatment, since it is using the
+  // wrapper as the data drupal selector, and we need it to
+  // use the -value.  If it is not a bef, then check if there
+  // is a data drupal selector and use it as the form field id.
+  if (
+    isset($variables['element'][0]['#context']['#plugin_type']) &&
+    $variables['element'][0]['#context']['#plugin_type'] == 'bef'
+  ) {
+
+    // If this is a date picker, then change the form field id
+    // to use -value instead of -wrapper.  If not then just use
+    // the straight data drupal selector.
+    if ($variables['element'][0]['#context']['#plugin_id'] == 'bef_datepicker') {
+      $variables['form_field_id'] = str_replace(
+        '-wrapper',
+        '-value',
+        $variables['element'][0]['#attributes']['data-drupal-selector']
+      );
+    }
+    else {
+      $variables['form_field_id'] = $variables['element'][0]['#attributes']['data-drupal-selector'];
+    }
+  }
+  else {
+
+    // If there is a data drupal selector use it for form field id.
+    if (isset($variables['element'][0]['#attributes']['data-drupal-selector'])) {
+      $variables['form_field_id'] = $variables['element'][0]['#attributes']['data-drupal-selector'];
+    }
+  }
 }
-- 
GitLab