uw_sites_all.module 4.58 KB
Newer Older
1 2 3 4 5 6 7
<?php

/**
 * @file
 * Workbench module file.
 */

8
use Drupal\Core\Form\FormStateInterface;
9
use Drupal\menu_link_content\Entity\MenuLinkContent;
10
use Drupal\webform\WebformInterface;
11 12 13 14 15

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 */
function uw_sites_all_form_node_form_alter(array &$form, FormStateInterface $form_state, string $form_id): void {
16 17
  // Add submit handler.
  $form['actions']['submit']['#submit'][] = '_uw_sites_all_form_node_form_submit';
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

  // Allow a page to have a menu link with the 'enabled' property false.
  if (isset($form['menu']['link'])) {
    // Rename "Provide a menu link".
    $form['menu']['enabled']['#title'] = t('Place in site hierarchy');

    // Get current value for the 'enabled' property, defaulting to TRUE.
    $enabled = TRUE;
    if (!empty($form['menu']['link']['entity_id']['#value'])) {
      $entity_id = (int) $form['menu']['link']['entity_id']['#value'];
      $menu_link = MenuLinkContent::load($entity_id);
      if (isset($menu_link)) {
        $enabled = $menu_link->isEnabled();
      }
    }

    // Create checkbox for hiding menu links.
    $form['menu']['link']['place_in_menu'] = [
      '#type' => 'checkbox',
      '#title' => t('Add menu link'),
      '#weight' => -50,
      '#default_value' => $enabled,
    ];

    // Hide menu fields that do not apply when the menu link is not enabled.
    $hide_state = [
      'invisible' => [
        'input[name="menu[place_in_menu]"]' => ['checked' => FALSE],
      ],
    ];
    $form['menu']['link']['title']['#states'] = $hide_state;
    $form['menu']['link']['description']['#states'] = $hide_state;
    $form['menu']['link']['weight']['#states'] = $hide_state;
  }
52 53 54 55 56 57
}

/**
 * Form submit handler.
 */
function _uw_sites_all_form_node_form_submit(array $form, FormStateInterface &$form_state): void {
58 59
  // For new nodes of content types that use Layout Builder, redirect to layout
  // tab.
60
  $nid = $form_state->getValue('nid');
61 62 63
  $form_id = $form_state->getBuildInfo()['form_id'];
  $fieldDefinitions = $form['#process'][1][0]->get('fieldDefinitions');
  if ($nid && substr($form_id, -10) !== '_edit_form' && isset($fieldDefinitions['layout_builder__layout'])) {
64 65
    $form_state->setRedirect('layout_builder.overrides.node.view', ['node' => $nid]);
  }
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

  // If a page is in the site hierarchy, enable or disabled its menu item based
  // on place_in_menu selection.
  $in_hierarchy = $form_state->getValue(['menu', 'enabled']);
  if ($in_hierarchy) {
    $entity_id = (int) $form_state->getValue(['menu', 'entity_id']);
    $enabled = (bool) $form_state->getValue(['menu', 'place_in_menu']);
    $menu_link = MenuLinkContent::load($entity_id);
    // If JS is disabled, "Menu link title" will not be automatically filled. If
    // the title is empty, no link will be created and the following will error.
    // So, only run these if the menu link exists.
    if ($menu_link) {
      $menu_link->set('enabled', $enabled);
      $menu_link->save();
    }
  }
82 83
}

84 85 86 87
/**
 * Implements hook_link_alter().
 */
function uw_sites_all_link_alter(&$variables) {
88 89
  if ($variables['text'] == 'All recent content') {
    $variables['text'] = t('@text', ['@text' => 'Manage Content']);
90
  }
91 92
}

93 94 95
/**
 * Implements hook_page_attachments().
 */
96 97 98 99 100 101
function uw_sites_all_page_attachments(array &$page) {
  $is_admin = \Drupal::service('router.admin_context')->isAdminRoute();
  if ($is_admin) {
    $page['#attached']['library'][] = 'uw_sites_all/uw-sites-all-adminimal';
    return;
  }
102

103 104
}

105 106 107 108 109 110 111 112 113 114
/**
 * Implements template_preprocess_block().
 */
function uw_sites_all_preprocess_block(&$variables) {

  // Get the arguments of the URL and separate into parts.
  $args = explode('/', \Drupal::service('path.current')->getPath());

  // If the last argument is layout, we are on a layout page,
  // so set the variable to true.
115
  if (in_array('layout', $args) || in_array('layout_builder', $args)) {
116 117 118
    $variables['in_layout_builder'] = TRUE;
  }
}
119 120 121 122 123 124 125 126 127 128 129

/**
 * Implements hook_update_projects_alter().
 */
function uw_sites_all_update_projects_alter(&$projects) {
  foreach ($projects as &$project) {
    // Removes -uw_wcms (and any trailing numbers) from version numbers so
    // Drupal reports updates properly.
    $project['info']['version'] = preg_replace('/-uw_wcms\d*$/', '', $project['info']['version']);
  }
}
130 131 132 133

/**
 * Implements hook_ENTITY_TYPE_presave().
 */
134
function uw_sites_all_webform_presave(WebformInterface $webform): void {
135
  // Add a Webform CAPTCHA component to each newly-created Webform.
136
  if ($webform->isNew() && empty($webform->getElementsRaw())) {
137 138 139 140 141 142 143 144
    $elements = [
      'captcha' => [
        '#type' => 'captcha',
      ],
    ];
    $webform->setElements($elements);
  }
}