Skip to content
Snippets Groups Projects
uw_cfg_common.module 12.1 KiB
Newer Older
Liam Morland's avatar
Liam Morland committed
<?php

/**
 * @file
 * Module file.
 */

Igor Biki's avatar
Igor Biki committed
use Drupal\Core\Entity\EntityInterface;
Liam Morland's avatar
Liam Morland committed
use Drupal\Core\Form\FormStateInterface;
use Drupal\uw_cfg_common\Service\UWService;
use Drupal\webform\WebformInterface;
use Drupal\webform\WebformSubmissionStorageInterface;
Liam Morland's avatar
Liam Morland committed

Igor Biki's avatar
Igor Biki committed
function uw_cfg_common_entity_presave(EntityInterface $entity) {

  // Check if we are on a menu link.
  if ($entity->getEntityTypeId() == 'menu_link_content') {

    // Check that we are on a Information For (audience) link.
    if ($entity->menu_name->value == 'uw-menu-audience-menu') {

      // Invalid all the menu caching.
      \Drupal::cache('menu')->invalidateAll();

      // Rebuild all the menus.
      \Drupal::service('plugin.manager.menu.link')->rebuild();
    }
  }
}

Liam Morland's avatar
Liam Morland committed
/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Remove the None option from layout builder styles.
 */
function uw_cfg_common_form_layout_builder_configure_section_alter(array &$form, FormStateInterface $form_state, string $form_id): void {
Liam Morland's avatar
Liam Morland committed
  // Remove the None option from layout builder styles.
  unset($form['layout_builder_style']['#empty_option']);

  // Ensuring that the contained width is selected by default.
  $form['layout_builder_style']['#default_value'] = $form['layout_builder_style']['#default_value'] ?: 'uw_lbs_contained_width';
Liam Morland's avatar
Liam Morland committed
}
/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Configure admin/structure/webform/config/submissions.
 */
function uw_cfg_common_form_webform_admin_config_submissions_form_alter(array &$form, FormStateInterface $form_state, string $form_id): void {
  // Remove undesired features.
  unset($form['views_settings']);
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Configure admin/structure/webform/manage/WEBFORM_ID/access.
 */
function uw_cfg_common_form_webform_settings_access_form_alter(array &$form, FormStateInterface $form_state, string $form_id): void {
  // Remove sections for access control that should not be available.
  $sections_to_remove = [
    'update_any',
    'update_own',
    'delete_own',
    'administer',
    'configuration',
  ];
  foreach ($sections_to_remove as $section) {
    unset($form['access'][$section]);
  }

  // Remove all but user-based access for submissions and test.
  $permissions_to_edit = [
    'create',
    'view_any',
    'delete_any',
    'purge_any',
    'view_own',
    'test',
  ];
  $access_types_to_remove = [
    'roles',
    'permissions',
  ];
  foreach ($permissions_to_edit as $permission) {
    foreach ($access_types_to_remove as $type) {
      unset($form['access'][$permission][$type]);
    }
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Configure admin/structure/webform/manage/WEBFORM_ID/settings/confirmation.
 */
function uw_cfg_common_form_webform_settings_confirmation_form_alter(array &$form, FormStateInterface $form_state, string $form_id): void {
  // Remove undesirable Webform submission confirmation types. These appear on
  // admin/structure/webform/manage/*/settings/confirmation.
  // The 'modal' type is just a different way to display the message. Disable
  // for consistency.
  unset($form['confirmation_type']['confirmation_type']['#options']['modal']);
  // The 'none' type is only useful along with a custom handler which provides
  // the confirmation message.
  unset($form['confirmation_type']['confirmation_type']['#options']['none']);

  // Remove undesired features.
  unset($form['confirmation_attributes_container']);
  unset($form['back']['back_container']['confirmation_back_attributes_container']);
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Configure admin/structure/webform/manage/WEBFORM_ID/settings.
 */
function uw_cfg_common_form_webform_settings_form_alter(array &$form, FormStateInterface $form_state, string $form_id): void {
  // Remove undesired features.
  unset($form['ajax_settings']);

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Configure admin/structure/webform/manage/WEBFORM_ID/settings/form.
 */
function uw_cfg_common_form_webform_settings_form_form_alter(array &$form, FormStateInterface $form_state, string $form_id): void {
  // Unset the source entity settings in webforms.
  unset($form['form_behaviors']['form_prepopulate_source_entity']);
  unset($form['form_behaviors']['form_prepopulate_source_entity_required']);
  unset($form['form_behaviors']['form_prepopulate_source_entity_type']);

  // Remove undesired features.
  unset($form['access_denied']);
  unset($form['custom_settings']);
  unset($form['form_behaviors']['form_autofocus']);
  unset($form['form_behaviors']['form_disable_back']);
  unset($form['form_behaviors']['form_novalidate']);
  unset($form['form_behaviors']['form_required']);
  unset($form['form_behaviors']['form_reset']);
  unset($form['form_behaviors']['form_submit_back']);
  unset($form['form_settings']['form_attributes']);
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Configure admin/structure/webform/manage/WEBFORM_ID/settings/submissions.
 */
function uw_cfg_common_form_webform_settings_submissions_form_alter(array &$form, FormStateInterface $form_state, string $form_id): void {
  // Remove undesired features.
  unset($form['access_denied']);
  unset($form['submission_behaviors']['form_convert_anonymous']);
  unset($form['submission_behaviors']['submission_log']);
  unset($form['submission_behaviors']['token_update']);
  unset($form['views_settings']);
/**
 * Implements hook_ENTITY_TYPE_create().
 */
function uw_cfg_common_webform_create(WebformInterface $webform) {
  // Submission purge settings. Set the default to purge drafts after 28 days.
  $webform->setSetting('purge', WebformSubmissionStorageInterface::PURGE_DRAFT);
  $webform->setSetting('purge_days', 28);
}

/**
 * Implements hook_toolbar_alter().
 *
 * Remove the Manage link from the toolbar for authenticated users.
 */
function uw_cfg_common_toolbar_alter(&$items) {
  // Get the current user.
  $current_user = \Drupal::currentUser();

  // No changes for user 1.
  if ((int) $current_user->id() === 1) {
    return;
  }
  // Get the roles of the user.
  $roles = $current_user->getRoles();
  // If there is only 1 role and that first role is authenticated, remove the
  // manage link. If there are multiple roles then we know that they will have
  // the Manage link, we are only removing the manage link for strictly
  // authenticated users only.
  if (count($roles) == 1 && $roles[0] == 'authenticated') {
    // Remove the manage link.
    unset($items['administration']);

/**
 * Implements hook_preprocess_node().
 */
function uw_cfg_common_preprocess_node(&$variables) {

  // Get the current path.
  $path = explode('/', \Drupal::service('path.current')->getPath());

  // The paths to place the content moderation block on.  Made this
  // an array to future proof, if there are more pages later.
  $paths_for_content_moderation = ['latest'];

  // ISTWCMS-4493: adding class if section has full width.
  // If there is a sidebar on the node, check all sections for full width.
  if (isset($variables['sidebar'])) {

    // Get the layouts from the node.
    $layouts = $variables['node']->layout_builder__layout->getValue();

    // Step through each of the layouts and check for full width.
    foreach ($layouts as $layout) {

      // Get the layout settings from the section.
      $settings = $layout['section']->getLayoutSettings();

      // If the layout builder style is set to full width, then set
      // the classes variable for the node and exit the loop.
      if ($settings['layout_builder_styles_style'] == "uw_lbs_full_width") {

        // Add a class to the node for full width on a section.
        $variables['attributes']['class'][] = 'uw-section-has-full-width';

        // Break out of the loop to save computational time.
        break;
      }
    }
  }

  // Check if we are to add the content moderation place.
  if (in_array(end($path), $paths_for_content_moderation)) {

    // Add the content moderation block.
    $variables['uw_content_moderation_form'] = \Drupal::formBuilder()->getForm('Drupal\content_moderation\Form\EntityModerationForm', $variables['node']);
  }
  else {

    $block_manager = \Drupal::service('plugin.manager.block');

    $plugin_block = $block_manager->createInstance('uw_cbl_content_moderation', []);

    $access_result = $plugin_block->access(\Drupal::currentUser());

    // Return empty render array if user doesn't have access.
    // $access_result can be boolean or an AccessResult class.
    if (is_object($access_result) && $access_result->isForbidden() || is_bool($access_result) && !$access_result) {
      return [];
    }

    $render = $plugin_block->build();

    $variables['uw_content_moderation_form'] = $render;
  }
}
/**
 * Implements hook_page_attachments().
 */
function uw_cfg_common_page_attachments(array &$page) {
  $page['#attached']['library'][] = 'uw_cfg_common/uw_mathjax';

}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Set the default of preview mode disabled.
 */
function uw_cfg_common_form_node_type_add_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $form['submission']['preview_mode']['#default_value'] = 0;

}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Node edit form: node/NID/edit.
 *
 * Prevent certain changes to the home page.
 */
function uw_cfg_common_form_node_uw_ct_web_page_edit_form_alter(array &$form, FormStateInterface $form_state, string $form_id): void {
  // No changes for those with access.
  if (\Drupal::currentUser()->hasPermission('bypass home page protection')) {
    return;
  }

  // Do not allow the home page to be parent of any item.
  unset($form['menu']['link']['menu_parent']['#options']['main:uw_base_profile.front_page']);

  // Early return if not editing home page.
  $nid = (int) \Drupal::routeMatch()->getRawParameter('node');
  if (!UWService::nodeIsHomePage($nid)) {
    return;
  }

  // Remove access to certain controls.
  $form['path']['#access'] = FALSE;
  $form['promote']['#access'] = FALSE;
  $form['sticky']['#access'] = FALSE;
  // For 'menu', setting #access did not work for non-admins. So, also hide the
  // sub-components and make it a container so that nothing appears on the page.
  $form['menu']['#access'] = FALSE;
  $form['menu']['#type'] = 'container';
  $form['menu']['enabled']['#access'] = FALSE;
  $form['menu']['link']['#access'] = FALSE;

  // Hide delete link if no access. This should happen by itself, but does not.
  if (!$form['actions']['delete']['#url']->access()) {
    $form['actions']['delete']['#access'] = FALSE;
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Menu edit form: admin/structure/menu/manage/main.
 *
 * Prevent certain changes to the home page.
 */
function uw_cfg_common_form_menu_edit_form_alter(array &$form, FormStateInterface $form_state, string $form_id): void {
  // No changes for those with access.
  if (\Drupal::currentUser()->hasPermission('bypass home page protection')) {
    return;
  }

  // Return early if not editing "Main navigation" menu.
  if (!isset($form['links']['links']['menu_plugin_id:uw_base_profile.front_page'])) {
    return;
  }

  // Remove access to home page controls.
  $form['links']['links']['menu_plugin_id:uw_base_profile.front_page']['enabled']['#access'] = FALSE;
  $form['links']['links']['menu_plugin_id:uw_base_profile.front_page']['operations']['#access'] = FALSE;
  $form['links']['links']['menu_plugin_id:uw_base_profile.front_page']['weight']['#access'] = FALSE;
  // Make home page not draggable.
  $key = array_search('draggable', $form['links']['links']['menu_plugin_id:uw_base_profile.front_page']['#attributes']['class'], TRUE);
  unset($form['links']['links']['menu_plugin_id:uw_base_profile.front_page']['#attributes']['class'][$key]);
}

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Menu link edit form: admin/structure/menu/link/LINK/edit.
 *
 * Do not allow the home page to be parent of any item.
 */
function uw_cfg_common_form_menu_link_edit_alter(array &$form, FormStateInterface $form_state, string $form_id): void {
  // No changes for those with access.
  if (\Drupal::currentUser()->hasPermission('bypass home page protection')) {
    return;
  }

  // Do not allow the home page to be parent of any item.
  unset($form['menu_parent']['#options']['main:uw_base_profile.front_page']);
}