From cbf48ef8e8d0c20c9529aa09265811e98e9d45f8 Mon Sep 17 00:00:00 2001
From: Lily Yan <l26yan@uwaterloo.ca>
Date: Wed, 10 Apr 2024 13:07:36 -0400
Subject: [PATCH] ISTWCMS-6850 Add message to service to create categories if
 none exist

---
 uw_ct_service.module | 57 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 57 insertions(+)

diff --git a/uw_ct_service.module b/uw_ct_service.module
index 07f8f40..1f2a0be 100644
--- a/uw_ct_service.module
+++ b/uw_ct_service.module
@@ -5,6 +5,7 @@
  * Provides configuration and settings for services.
  */
 
+use Drupal\Core\Link;
 use Drupal\Core\Form\FormStateInterface;
 use Drupal\Core\Url;
 use Drupal\views\Plugin\views\query\QueryPluginBase;
@@ -121,6 +122,33 @@ function uw_ct_service_form_node_uw_ct_service_form_alter(array &$form, FormStat
 
   // ISTWCMS-5551: adding help text to office hours exception days.
   $form['field_uw_service_hours']['widget']['office_hours_exceptions']['#description'] = t('<p>Exceptions further in the future will display once the exception is under one week away.</p>');
+
+  // Get terms from uw_vocab_service_categories.
+  $terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree('uw_vocab_service_categories');
+
+  // If there is not service categories taxonomy term set yet.
+  if (empty($terms)) {
+    // Set message.
+    \Drupal::messenger()->addMessage(t('You must create at least one service category before adding a service.'), 'error');
+
+    // Get the current user.
+    $current_user = \Drupal::currentUser();
+
+    // If the current user has no permission, set field description.
+    if (!$current_user->hasPermission('create terms in uw_vocab_service_categories')) {
+      $form['field_uw_service_category']['widget']['#description'] = t('Reach out to someone who has access to add a service category.');
+    }
+
+    // If the current user has permission, add a link to create a term.
+    else {
+      $form['field_uw_service_category']['widget']['#description'] =
+        Link::fromTextAndUrl(t('Add a service category.'), Url::fromUri('internal:/admin/structure/taxonomy/manage/uw_vocab_service_categories/add'))->toString();
+    }
+  }
+
+  // Add in our own validation to check if there is no service categories.
+  $form['#validate'][] = '_uw_ct_service_node_validate';
+
 }
 
 /**
@@ -234,3 +262,32 @@ function uw_ct_service_views_query_alter(
     }
   }
 }
+
+/**
+ * Validate function for node add/edit for service content type.
+ *
+ * This will ensure that only the tabs for the specific term are saved
+ * when the node is saved.  If for some reason a term was switched and
+ * the term has different tabs to be displayed, the tabs from the old
+ * term would still appear.  This will remove any values in the tabs
+ * that are not listed in the term.
+ *
+ * @param array $form
+ *   The form array.
+ * @param \Drupal\Core\Form\FormStateInterface $form_state
+ *   The form state array.
+ */
+function _uw_ct_service_node_validate(array &$form, FormStateInterface &$form_state) {
+
+  // Get the term id from the form state.
+  if (isset($form_state->getValue('field_uw_service_category')[0]['target_id'])) {
+    $tid = $form_state->getValue('field_uw_service_category')[0]['target_id'];
+
+    // If the tid is NULL, that means that there is
+    // no catalog yet created so throw a form state
+    // error and add message to create one.
+    if (!$tid) {
+      $form_state->setErrorByName('service categories', 'You must create at least one service category before adding a service.');
+    }
+  }
+}
-- 
GitLab