Skip to content
Snippets Groups Projects
Commit 91482c5c authored by Eric Bremner's avatar Eric Bremner Committed by Kevin Paxman
Browse files

ISTWCMS-5880: adding the custom validation for banners

parent 8b0d3e58
No related branches found
No related tags found
3 merge requests!284Feature/istwcms 5880 ebremner banners above,!274Draft: ISTWCMS-5551: fixing office hours display,!260Feature/istwcms 5668 a5kulkar rename references to publications
...@@ -928,7 +928,7 @@ function uw_cfg_common_form_alter(array &$form, FormStateInterface $form_state, ...@@ -928,7 +928,7 @@ function uw_cfg_common_form_alter(array &$form, FormStateInterface $form_state,
], ],
]; ];
// Set the states for the banner. // Set the states for the banner settings.
$form['group_banner_settings']['#states'] = [ $form['group_banner_settings']['#states'] = [
'visible' => [ 'visible' => [
[ [
...@@ -938,6 +938,9 @@ function uw_cfg_common_form_alter(array &$form, FormStateInterface $form_state, ...@@ -938,6 +938,9 @@ function uw_cfg_common_form_alter(array &$form, FormStateInterface $form_state,
], ],
], ],
]; ];
// Add our custom validation for banners.
$form['#validate'][] = '_uw_cfg_common_banner_validate';
} }
// If we are on the media upload form, we want to restrict // If we are on the media upload form, we want to restrict
...@@ -1040,6 +1043,69 @@ function _uw_cfg_common_alias_validate(array &$form, FormStateInterface $form_st ...@@ -1040,6 +1043,69 @@ function _uw_cfg_common_alias_validate(array &$form, FormStateInterface $form_st
} }
} }
/**
* Validates submission values for banners on nodes.
*/
function _uw_cfg_common_banner_validate(array &$form, FormStateInterface $form_state) {
// Get the values of the form state.
$values = $form_state->getValues();
// If there is a type of media set and it is banners,
// then perform the validation.
if (
isset($values['field_uw_type_of_media']) &&
$values['field_uw_type_of_media'][0]['value'] == 'banner'
) {
// Since the add more button is in the values, and there is
// a mix of integers and strings as keys, we need to count
// the array keys and if it is less than or equal to one
// means that the user has not entered a banner at all.
if (count(array_keys($values['field_uw_banner'])) <= 1) {
$form_state->setError($form['field_uw_banner'], t('You must add at least one banner.'));
}
// At least one banner, now check that there is an
// image in each banner.
else {
// Step through all the values of banners and check for an image.
foreach ($values['field_uw_banner'] as $key => $value) {
// Ensure that the key is an integer, since there is an add_more
// in the values.
if (is_int($key)) {
// If there is no selection on the media, then there is no image,
// so set the error.
// @TO-DO: fix so that inline errors show.
if (!isset($value['subform']['field_uw_ban_image']['selection'])) {
// Set the error that an image is missing.
$form_state->setError($form['field_uw_banner']['widget'][$key]['subform']['field_uw_ban_image'], t('Banner image field is required.'));
}
}
}
}
}
}
/**
* Implements hook_field_widget_WIDGET_TYPE_form_alter().
*/
function uw_cfg_common_field_widget_entity_reference_paragraphs_form_alter(&$element, &$form_state, $context) {
if ($element['#paragraph_type'] == 'uw_para_image_banner') {
if (!isset($context['form']['#block'])) {
$element['subform']['field_uw_ban_image']['widget']['#attributes']['class'] = 'form-required js-form-required';
}
else {
$element['subform']['field_uw_ban_image']['widget']['#required'] = TRUE;
}
}
}
/** /**
* Implements hook_field_widget_WIDGET_TYPE_form_alter(). * Implements hook_field_widget_WIDGET_TYPE_form_alter().
*/ */
......
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