From 077032dda4cc2b6e766a3e43a94d45450196cff0 Mon Sep 17 00:00:00 2001
From: Eric Bremner <ebremner@uwaterloo.ca>
Date: Wed, 19 Oct 2022 10:07:41 -0400
Subject: [PATCH] ISTWCMS-5880: fixing the required for image in banners

---
 uw_cfg_common.module | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/uw_cfg_common.module b/uw_cfg_common.module
index 8586bd9d..e4aec498 100644
--- a/uw_cfg_common.module
+++ b/uw_cfg_common.module
@@ -1163,9 +1163,25 @@ function _uw_cfg_common_banner_validate(array &$form, FormStateInterface $form_s
  * Implements hook_field_widget_WIDGET_TYPE_form_alter().
  */
 function uw_cfg_common_field_widget_entity_reference_paragraphs_form_alter(&$element, &$form_state, $context) {
+
+  // If this is a banner image, add the required manually.
   if ($element['#paragraph_type'] == 'uw_para_image_banner') {
+
+    // If this is not a block, add the class for required
+    // on the image, we need to do this so that when using
+    // banners with media, the required does not work properly
+    // when banners are not selected.
+    // If it is a block, just add the required to the element.
     if (!isset($context['form']['#block'])) {
-      $element['subform']['field_uw_ban_image']['widget']['#attributes']['class'] = 'form-required js-form-required';
+
+      // If there already is classes set, then add to the array.
+      // If no classes yet, add as an array.
+      if (isset( $element['subform']['field_uw_ban_image']['widget']['#attributes']['class'])) {
+        $element['subform']['field_uw_ban_image']['widget']['#attributes']['class'][] = 'form-required';
+      }
+      else {
+        $element['subform']['field_uw_ban_image']['widget']['#attributes']['class'] = ['form-required'];
+      }
     }
     else {
       $element['subform']['field_uw_ban_image']['widget']['#required'] = TRUE;
-- 
GitLab