Skip to content
Snippets Groups Projects
Commit 6caf5d41 authored by Kevin Paxman's avatar Kevin Paxman
Browse files

Merge branch 'feature/ISTWCMS-5846-ebremner-blank-summaries-new' into '1.1.x'

ISTWCMS-5846: adding code and config for blank summaries

See merge request !295
parents 68580a0c 2475c345
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
langcode: en
status: true
dependencies:
module:
- node
id: node.field_uw_blank_summary
field_name: field_uw_blank_summary
entity_type: node
type: boolean
settings: { }
module: core
locked: false
cardinality: 1
translatable: true
indexes: { }
persist_with_no_fields: false
custom_storage: false
......@@ -38,6 +38,49 @@ 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 (
isset($variables['element']['#id']) &&
$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().
*
......@@ -128,6 +171,36 @@ function uw_cfg_common_entity_presave(EntityInterface $entity) {
// images will not work.
if ($entity->getEntityTypeId() == 'node') {
// ISTWCMS-5846: if the leave summary blank is checked
// on the node add/edit page, set the summary to NULL.
// Check that the node has the field leave summary blank.
if ($entity->hasField('field_uw_blank_summary')) {
// If the leave summary blank is checked, set summary to NULL.
if ($entity->field_uw_blank_summary->value) {
// Get the node type from the entity.
$node_type = $entity->getType();
// Since all the summary fields are field_uw_ct_<node_type>_summary,
// we need to get the node_type from the getType. We need this
// because the node_type has uw_ct_ in the name, so simply replacing
// the uw_ct_ with nothing will give us the node_type.
// the uw_ct_ with nothing will give us the node_type.
$node_type = str_replace('uw_ct_', '', $node_type);
// Since news has the content type with news_item, we need
// to remove the _item to get the correct field name.
$node_type = str_replace('_item', '', $node_type);
// Set the field name using the notation from above.
$field_name = 'field_uw_' . $node_type . '_summary';
// Now set the summary to NULL using the field_name.
$entity->$field_name = NULL;
}
}
// If there is a hero image (media), continue to process.
if (
$entity->hasField('field_uw_hero_image') &&
......@@ -888,6 +961,53 @@ function uw_cfg_common_preprocess_responsive_image(&$variables) {
*/
function uw_cfg_common_form_alter(array &$form, FormStateInterface $form_state, string $form_id): void {
// ISTWCMS-5846: if we are on a UW content type which is
// the notation of node_uw_ct_<node_type>_form of the
// form_id, then we need to add the status to the
// summary field.
if (preg_match("/node_uw_ct_.*form/", $form_id)) {
// Only add states if we have the blank summary field.
if (isset($form['field_uw_blank_summary'])) {
// Since we are unable to simply get the node type in format that
// we need to use it (i.e. event, news, blog, etc.), we need to
// take the form_id and manipulate it to get us the node type.
// First is the remove the node_uw_ct_, then the _edit and
// finally the _form.
$node_type = str_replace('node_uw_ct_', '', $form_id);
$node_type = str_replace('_edit', '', $node_type);
$node_type = str_replace('_form', '', $node_type);
// We need to add this because news has the name news_item in its
// content type, so we need to remove that to just get news
// to be used in the field name.
$node_type = str_replace('_item', '', $node_type);
// If the node type is an opportunity, we have to give
// the specific field name.
if ($node_type == 'opportunity') {
$field_name = 'field_uw_opportunity_position';
}
// Now since all the rest of the field names
// are the same, field_uw_<node_type>_summary,
// we can get the proper field name.
else {
$field_name = 'field_uw_' . $node_type . '_summary';
}
// Set the states of the summary, required and visible.
$form[$field_name]['widget'][0]['#states'] = [
'visible' => [
':input[name="field_uw_blank_summary[value]"]' => ['checked' => FALSE],
],
];
// Add custom validation for blank summaries.
$form['#validate'][] = '_uw_cfg_common_blank_summaries_validation';
}
}
// ISTWCMS-4648: removing revisions from layout builder page.
if (\Drupal::routeMatch()->getRouteName() == 'layout_builder.overrides.node.view') {
$form['revision']['#access'] = FALSE;
......@@ -1086,6 +1206,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 not 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.
*/
......
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