Skip to content
Snippets Groups Projects
Commit cbf48ef8 authored by Lily Yan's avatar Lily Yan
Browse files

ISTWCMS-6850 Add message to service to create categories if none exist

parent f8eac1f9
No related branches found
No related tags found
2 merge requests!60ISTWCMS-6095 Update maxlength settings for title, text fields and link fields...,!51ISTWCMS-6850 Add message to service to create categories if none exist
......@@ -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.');
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment