Skip to content
Snippets Groups Projects
uw_cfg_common.module 7.89 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\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->getValue()[0]['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();

  // Ensure that the current user is not user1.

    // Get the roles of the user.
    $roles = $current_user->getRoles();

Liam Morland's avatar
Liam Morland committed
    // 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'];

  // 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_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;

}