menu_test.module 4.77 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Module that implements various hooks for menu tests.
6 7
 */

8
/**
9
 * Implements hook_menu_links_discovered_alter().
10
 */
11
function menu_test_menu_links_discovered_alter(&$links) {
12 13 14
  // Many of the machine names here are slightly different from the route name.
  // Since the machine name is arbitrary, this helps ensure that core does not
  // add mistaken assumptions about the correlation.
15 16
  $links['menu_test.menu_name_test']['menu_name'] = menu_test_menu_name();
  $links['menu_test.context']['title'] = \Drupal::config('menu_test.menu_item')->get('title');
17 18 19 20 21 22 23 24

  // Adds a custom menu link.
  $links['menu_test.custom'] = array(
    'title' => 'Custom link',
    'route_name' => 'menu_test.custom',
    'description' => 'Custom link used to check the integrity of manually added menu links.',
    'parent' => 'menu_test',
  );
25 26
}

27 28
/**
 * Implements hook_menu_local_tasks().
29 30 31
 *
 * If the menu_test.settings configuration 'tasks.add' has been set, adds
 * several local tasks to menu-test/tasks.
32
 */
33
function menu_test_menu_local_tasks(&$data, $route_name) {
34
  if (!\Drupal::config('menu_test.settings')->get('tasks.add')) {
35 36
    return;
  }
37
  if (in_array($route_name, array('menu_test.tasks_default', 'menu_test.tasks_empty', 'menu_test.tasks_tasks'))) {
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    $data['tabs'][0]['foo'] = array(
      '#theme' => 'menu_local_task',
      '#link' => array(
        'title' => 'Task 1',
        'href' => 'task/foo',
      ),
      '#weight' => 10,
    );
    $data['tabs'][0]['bar'] = array(
      '#theme' => 'menu_local_task',
      '#link' => array(
        'title' => 'Task 2',
        'href' => 'task/bar',
      ),
      '#weight' => 20,
    );
  }
}

/**
 * Implements hook_menu_local_tasks_alter().
59 60 61
 *
 * If the menu_test.settings configuration 'tasks.alter' has been set, adds
 * several local tasks to menu-test/tasks.
62
 */
63
function menu_test_menu_local_tasks_alter(&$data, $route_name) {
64
  if (!\Drupal::config('menu_test.settings')->get('tasks.alter')) {
65 66
    return;
  }
67
  if (in_array($route_name, array('menu_test.tasks_default', 'menu_test.tasks_empty', 'menu_test.tasks_tasks'))) {
68 69 70 71 72
    // Rename the default local task from 'View' to 'Show'.
    // $data['tabs'] is expected to be keyed by link hrefs.
    // The default local task always links to its parent path, which means that
    // if the tab root path appears as key in $data['tabs'], then that key is
    // the default local task.
73
    $key = $route_name . '_tab';
74 75 76 77 78 79 80 81 82
    if (isset($data['tabs'][0][$key])) {
      $data['tabs'][0][$key]['#link']['title'] = 'Show it';
    }
    // Rename the 'foo' task to "Advanced settings" and put it last.
    $data['tabs'][0]['foo']['#link']['title'] = 'Advanced settings';
    $data['tabs'][0]['foo']['#weight'] = 110;
  }
}

83
/**
84
 * Page callback: Provides a dummy function which can be used as a placeholder.
85
 *
86 87 88 89
 * @return string
 *   A string that can be used for comparison.
 *
 * @see menu_test_menu().
90 91
 *
 * @deprecated Use \Drupal\menu_test\Controller\MenuTestController::menuTestCallback()
92 93
 */
function menu_test_callback() {
94
  return ['#markup' => 'This is menu_test_callback().'];
95
}
96

97
/**
98
 * Page callback: Tests the theme negotiation functionality.
99 100 101 102
 *
 * @param bool $inherited
 *   (optional) TRUE when the requested page is intended to inherit
 *   the theme of its parent.
103
 *
104
 * @return string
105 106
 *   A string describing the requested custom theme and actual theme being used
 *   for the current page request.
107 108
 *
 * @see menu_test_menu().
109 110
 *
 * @deprecated Use \Drupal\menu_test\Controller\MenuTestController::themePage()
111 112
 */
function menu_test_theme_page_callback($inherited = FALSE) {
113
  $theme_key = \Drupal::theme()->getActiveTheme()->getName();
114
  // Now we check what the theme negotiator service returns.
115
  $active_theme = \Drupal::service('theme.negotiator')->determineActiveTheme(\Drupal::routeMatch());
116
  $output = "Active theme: $active_theme. Actual theme: $theme_key.";
117
  if ($inherited) {
118
    $output .= ' Theme negotiation inheritance is being tested.';
119
  }
120
  return ['#markup' => $output];
121 122
}

123
/**
124
 * Sets a static variable for the testMenuName() test.
125
 *
126 127 128 129 130 131 132
 * Used to change the menu_name parameter of a menu.
 *
 * @param string $new_name
 *   (optional) If set, will change the $menu_name value.
 *
 * @return string
 *   The $menu_name value to use.
133 134
 */
function menu_test_menu_name($new_name = '') {
135
  static $menu_name = 'original';
136
  if ($new_name) {
137
    $menu_name = $new_name;
138
  }
139
  return $menu_name;
140
}
141

142
/**
143
 * Title callback: Concatenates the title and case number.
144
 *
145
 * @param string $title
146
 *   Title string.
147 148 149 150 151 152 153
 * @param int $case_number
 *   (optional) The current case number which it tests (defaults to 3).
 *
 * @return string
 *   A string containing the title and case number.
 *
 * @see menu_test_menu().
154
 */
155 156
function menu_test_title_callback($title, $case_number = 3) {
  return t($title) . ' - Case ' . $case_number;
157
}