summaryrefslogtreecommitdiffstats
path: root/core/modules/menu_link/menu_link.module
blob: ac3c408f84294ebcdd29f2356e0c252c4162e444 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php

/**
 * @file
 * Enables users to create menu links.
 */

use Drupal\Core\Url;
use Drupal\menu_link\Entity\MenuLink;
use Drupal\menu_link\MenuLinkInterface;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Component\HttpFoundation\Request;

function menu_link_help($route_name, Request $request) {
  switch ($route_name) {
    case 'help.page.menu_link':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('The Menu Link module allows users to create menu links. It is required by the Menu UI module, which provides an interface for managing menus. See the <a href="!menu-help">Menu UI module help page</a> for more information.', array('!menu-help' => \Drupal::url('help.page', array('name' => 'menu_ui')))) . '</p>';
      return $output;
  }
}

/**
 * Entity URI callback.
 *
 * @param \Drupal\menu_link\Entity\MenuLink $menu_link
 *   A menu link entity.
 */
function menu_link_uri(MenuLink $menu_link) {
  return new Url($menu_link->route_name, $menu_link->route_parameters);
}

/**
 * Loads a menu link entity.
 *
 * This function should never be called from within node_load() or any other
 * function used as a menu object load function since an infinite recursion may
 * occur.
 *
 * @param int $mlid
 *   The menu link ID.
 * @param bool $reset
 *   (optional) Whether to reset the menu_link_load_multiple() cache.
 *
 * @return \Drupal\menu_link\Entity\MenuLink|null
 *   A menu link entity, or NULL if there is no entity with the given ID.
 *
 * @deprecated in Drupal 8.x, will be removed before Drupal 9.0.
 *   Use \Drupal\menu_link\Entity\MenuLink::load().
 */
function menu_link_load($mlid = NULL, $reset = FALSE) {
  if ($reset) {
    \Drupal::entityManager()->getStorage('menu_link')->resetCache(array($mlid));
  }
  return MenuLink::load($mlid);
}

/**
 * Loads menu link entities from the database.
 *
 * @param array $mlids
 *   (optional) An array of entity IDs. If omitted, all entities are loaded.
 * @param bool $reset
 *   (optional) Whether to reset the internal cache.
 *
 * @return array<\Drupal\menu_link\Entity\MenuLink>
 *   An array of menu link entities indexed by entity IDs.
 *
 * @see menu_link_load()
 * @see entity_load_multiple()
 *
 * @deprecated in Drupal 8.x, will be removed before Drupal 9.0.
 *   Use \Drupal\menu_link\Entity\MenuLink::loadMultiple().
 */
function menu_link_load_multiple(array $mlids = NULL, $reset = FALSE) {
  if ($reset) {
    \Drupal::entityManager()->getStorage('menu_link')->resetCache($mlids);
  }
  return MenuLink::loadMultiple($mlids);
}

/**
 * Deletes a menu link.
 *
 * @param int $mlid
 *   The menu link ID.
 *
 * @see menu_link_delete_multiple()
 */
function menu_link_delete($mlid) {
  menu_link_delete_multiple(array($mlid));
}

/**
 * Deletes multiple menu links.
 *
 * @param array $mlids
 *   An array of menu link IDs.
 * @param bool $force
 *   (optional) Forces deletion. Internal use only, setting to TRUE is
 *   discouraged. Defaults to FALSE.
 * @param bool $prevent_reparenting
 *   (optional) Disables the re-parenting logic from the deletion process.
 *   Defaults to FALSE.
 */
function menu_link_delete_multiple(array $mlids, $force = FALSE, $prevent_reparenting = FALSE) {
  if (!$mlids) {
    // If no IDs or invalid IDs were passed, do nothing.
    return;
  }

  $controller = \Drupal::entityManager()
    ->getStorage('menu_link');
  if (!$force) {
    $entity_query = \Drupal::entityQuery('menu_link');
    $group = $entity_query->orConditionGroup()
      ->condition('module', 'system', '<>')
      ->condition('updated', 0, '<>');

    $entity_query->condition('mlid', $mlids, 'IN');
    $entity_query->condition($group);

    $result = $entity_query->execute();
    $entities = $controller->loadMultiple($result);
  }
  else {
    $entities = $controller->loadMultiple($mlids);
  }
  $controller->setPreventReparenting($prevent_reparenting);
  $controller->delete($entities);
}

/**
 * Saves a menu link.
 *
 * After calling this function, rebuild the menu cache using
 * menu_cache_clear_all().
 *
 * @param \Drupal\menu_link\Entity\MenuLink $menu_link
 *   The menu link entity to be saved.
 *
 * @return int|bool
 *   Returns SAVED_NEW or SAVED_UPDATED if the save operation succeeded, or
 *   FALSE if it failed.
 */
function menu_link_save(MenuLink $menu_link) {
  return $menu_link->save();
}

/**
 * Inserts, updates, enables, disables, or deletes an uncustomized menu link.
 *
 * @param string $module
 *   The name of the module that owns the link.
 * @param string $op
 *   Operation to perform: insert, update, enable, disable, or delete.
 * @param string $link_path
 *   The path this link points to.
 * @param string $link_title
 *   (optional) Title of the link to insert or new title to update the link to.
 *   Unused for delete. Defaults to NULL.
 *
 * @return integer|null
 *   The insert op returns the mlid of the new item. Others op return NULL.
 */
function menu_link_maintain($module, $op, $link_path, $link_title = NULL) {
  $menu_link_controller = \Drupal::entityManager()
    ->getStorage('menu_link');
  switch ($op) {
    case 'insert':
      $menu_link = entity_create('menu_link', array(
        'link_title' => $link_title,
        'link_path' => $link_path,
        'module' => $module,)
      );
      return $menu_link->save();

    case 'update':
      $menu_links = entity_load_multiple_by_properties('menu_link', array('link_path' => $link_path, 'module' => $module, 'customized' => 0));
      foreach ($menu_links as $menu_link) {
        $menu_link->original = clone $menu_link;
        if (isset($link_title)) {
          $menu_link->link_title = $link_title;
        }
        $menu_link_controller->save($menu_link);
      }
      break;

    case 'enable':
    case 'disable':
      $menu_links = entity_load_multiple_by_properties('menu_link', array('link_path' => $link_path, 'module' => $module, 'customized' => 0));
      foreach ($menu_links as $menu_link) {
        $menu_link->original = clone $menu_link;
        $menu_link->hidden = ($op == 'disable' ? 1 : 0);
        $menu_link->customized = 1;
        if (isset($link_title)) {
          $menu_link->link_title = $link_title;
        }
        $menu_link_controller->save($menu_link);
      }
      break;

    case 'delete':
      $result = \Drupal::entityQuery('menu_link')->condition('link_path', $link_path)->execute();
      if (!empty($result)) {
        menu_link_delete_multiple($result);
      }
      break;
  }
}

/**
 * Implements hook_system_breadcrumb_alter().
 */
function menu_link_system_breadcrumb_alter(array &$breadcrumb, array $attributes, array $context) {
  // Custom breadcrumb behavior for editing menu links, we append a link to
  // the menu in which the link is found.
  if (!empty($attributes[RouteObjectInterface::ROUTE_NAME]) && $attributes[RouteObjectInterface::ROUTE_NAME] == 'menu_ui.link_edit' && !empty($attributes['menu_link'])) {
    $menu_link = $attributes['menu_link'];
    if (($menu_link instanceof MenuLinkInterface) && !$menu_link->isNew()) {
      // Add a link to the menu admin screen.
      $menu = entity_load('menu', $menu_link->menu_name);
      $breadcrumb[] = Drupal::l($menu->label(), 'menu_ui.menu_edit', array('menu' => $menu->id));
    }
  }
}