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

ISTWCMS-5846: adding fake required for blank summaries and drupal validation for the summary

parent bdc9f190
No related branches found
No related tags found
2 merge requests!295ISTWCMS-5846: adding code and config for blank summaries,!274Draft: ISTWCMS-5551: fixing office hours display
...@@ -38,6 +38,48 @@ function uw_cfg_common_preprocess_form_element(&$variables) { ...@@ -38,6 +38,48 @@ function uw_cfg_common_preprocess_form_element(&$variables) {
} }
} }
/**
* Implements template_preprocess_form_element_label().
*/
function uw_cfg_common_preprocess_form_element_label(&$variables) {
// Check if we need to add the form required to the label.
// Conditions are not the blank summary checkbox,
// the id of the label contains edit-field-uw and has
// either summary or position in the id.
if (
$variables['element']['#id'] !== 'edit-field-uw-blank-summary-value' &&
str_contains($variables['element']['#id'], 'edit-field-uw-') &&
(
str_contains($variables['element']['#id'], 'summary') ||
str_contains($variables['element']['#id'], 'position')
)
) {
// Try and get the node type, by replacing the id of the label.
$node_type = $variables['element']['#id'];
$node_type = str_replace('edit-field-uw-', '', $node_type);
$node_type = str_replace('-summary-0-value', '', $node_type);
$node_type = str_replace('-position-0-value', '', $node_type);
// The node types to place the form required on the label.
$blank_summary_node_types = [
'blog',
'event',
'news',
'opportunity',
'profile',
'project',
];
// If we are on a node that needs a form required
// on the label add the class.
if (in_array($node_type, $blank_summary_node_types)) {
$variables['attributes']['class'][] = 'form-required';
}
}
}
/** /**
* Implements hook_sendgrid_integration_categories_alter(). * Implements hook_sendgrid_integration_categories_alter().
* *
...@@ -955,13 +997,13 @@ function uw_cfg_common_form_alter(array &$form, FormStateInterface $form_state, ...@@ -955,13 +997,13 @@ function uw_cfg_common_form_alter(array &$form, FormStateInterface $form_state,
// Set the states of the summary, required and visible. // Set the states of the summary, required and visible.
$form[$field_name]['widget'][0]['#states'] = [ $form[$field_name]['widget'][0]['#states'] = [
'required' => [
':input[name="field_uw_blank_summary[value]"]' => ['checked' => FALSE],
],
'visible' => [ 'visible' => [
':input[name="field_uw_blank_summary[value]"]' => ['checked' => FALSE], ':input[name="field_uw_blank_summary[value]"]' => ['checked' => FALSE],
], ],
]; ];
// Add custom validation for blank summaries.
$form['#validate'][] = '_uw_cfg_common_blank_summaries_validation';
} }
} }
...@@ -1163,6 +1205,38 @@ function _uw_cfg_common_alias_validate(array &$form, FormStateInterface $form_st ...@@ -1163,6 +1205,38 @@ function _uw_cfg_common_alias_validate(array &$form, FormStateInterface $form_st
} }
} }
/**
* Validates submission values for banners on nodes.
*/
function _uw_cfg_common_blank_summaries_validation(array &$form, FormStateInterface $form_state) {
// Get the values from the form state.
$values = $form_state->getValues();
// Get the node type.
$node_type = str_replace('node_uw_ct_', '', $form['#form_id']);
$node_type = str_replace('_edit', '', $node_type);
$node_type = str_replace('_form', '', $node_type);
$node_type = str_replace('_item', '', $node_type);
// If the node type is an opportunity, we have to give
// the specific field name, if not just use the field name.
if ($node_type == 'opportunity') {
$field_name = 'field_uw_opportunity_position';
}
else {
$field_name = 'field_uw_' . $node_type . '_summary';
}
// If the blank summary is checked and the summary is null,
// then set an error.
if (!$values['field_uw_blank_summary']['value']) {
if ($values[$field_name][0]['value'] == '') {
$form_state->setError($form[$field_name]['widget'][0], t('Summary/position field is required.'));
}
}
}
/** /**
* Validates submission values for banners on nodes. * Validates submission values for banners on nodes.
*/ */
......
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