menu_link.module

You are here

Enables users to create menu links.

Functions

Namesort descending Description
menu_link_delete Deletes a menu link.
menu_link_delete_multiple Deletes multiple menu links.
menu_link_help
menu_link_load Loads a menu link entity.
menu_link_load_multiple Loads menu link entities from the database.
menu_link_maintain Inserts, updates, enables, disables, or deletes an uncustomized menu link.
menu_link_save Saves a menu link.
menu_link_system_breadcrumb_alter Implements hook_system_breadcrumb_alter().
menu_link_uri Entity URI callback.

File

core/modules/menu_link/menu_link.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Enables users to create menu links.
  5. */
  6. use Drupal\Core\Url;
  7. use Drupal\menu_link\Entity\MenuLink;
  8. use Drupal\menu_link\MenuLinkInterface;
  9. use Symfony\Cmf\Component\Routing\RouteObjectInterface;
  10. function menu_link_help($path, $arg) {
  11. switch ($path) {
  12. case 'admin/help#menu_link':
  13. $output = '';
  14. $output .= '<h3>' . t('About') . '</h3>';
  15. $output .= '<p>' . t('The Menu Link module allows users to create menu links. It is required by the Menu module, which provides an interface for managing menus. See the <a href="!menu-help">Menu module help page</a> for more information.', array('!menu-help' => \Drupal::url('help.page', array('name' => 'menu_ui')))) . '</p>';
  16. return $output;
  17. }
  18. }
  19. /**
  20. * Entity URI callback.
  21. *
  22. * @param \Drupal\menu_link\Entity\MenuLink $menu_link
  23. * A menu link entity.
  24. */
  25. function menu_link_uri(MenuLink $menu_link) {
  26. return new Url($menu_link->route_name, $menu_link->route_parameters);
  27. }
  28. /**
  29. * Loads a menu link entity.
  30. *
  31. * This function should never be called from within node_load() or any other
  32. * function used as a menu object load function since an infinite recursion may
  33. * occur.
  34. *
  35. * @param int $mlid
  36. * The menu link ID.
  37. * @param bool $reset
  38. * (optional) Whether to reset the menu_link_load_multiple() cache.
  39. *
  40. * @return \Drupal\menu_link\Entity\MenuLink|null
  41. * A menu link entity, or NULL if there is no entity with the given ID.
  42. */
  43. function menu_link_load($mlid = NULL, $reset = FALSE) {
  44. return entity_load('menu_link', $mlid, $reset);
  45. }
  46. /**
  47. * Loads menu link entities from the database.
  48. *
  49. * @param array $mlids
  50. * (optional) An array of entity IDs. If omitted, all entities are loaded.
  51. * @param bool $reset
  52. * (optional) Whether to reset the internal cache.
  53. *
  54. * @return array<\Drupal\menu_link\Entity\MenuLink>
  55. * An array of menu link entities indexed by entity IDs.
  56. *
  57. * @see menu_link_load()
  58. * @see entity_load_multiple()
  59. */
  60. function menu_link_load_multiple(array $mlids = NULL, $reset = FALSE) {
  61. return entity_load_multiple('menu_link', $mlids, $reset);
  62. }
  63. /**
  64. * Deletes a menu link.
  65. *
  66. * @param int $mlid
  67. * The menu link ID.
  68. *
  69. * @see menu_link_delete_multiple()
  70. */
  71. function menu_link_delete($mlid) {
  72. menu_link_delete_multiple(array($mlid));
  73. }
  74. /**
  75. * Deletes multiple menu links.
  76. *
  77. * @param array $mlids
  78. * An array of menu link IDs.
  79. * @param bool $force
  80. * (optional) Forces deletion. Internal use only, setting to TRUE is
  81. * discouraged. Defaults to FALSE.
  82. * @param bool $prevent_reparenting
  83. * (optional) Disables the re-parenting logic from the deletion process.
  84. * Defaults to FALSE.
  85. */
  86. function menu_link_delete_multiple(array $mlids, $force = FALSE, $prevent_reparenting = FALSE) {
  87. if (!$mlids) {
  88. // If no IDs or invalid IDs were passed, do nothing.
  89. return;
  90. }
  91. $controller = \Drupal::entityManager()
  92. ->getStorage('menu_link');
  93. if (!$force) {
  94. $entity_query = \Drupal::entityQuery('menu_link');
  95. $group = $entity_query->orConditionGroup()
  96. ->condition('module', 'system', '<>')
  97. ->condition('updated', 0, '<>');
  98. $entity_query->condition('mlid', $mlids, 'IN');
  99. $entity_query->condition($group);
  100. $result = $entity_query->execute();
  101. $entities = $controller->loadMultiple($result);
  102. }
  103. else {
  104. $entities = $controller->loadMultiple($mlids);
  105. }
  106. $controller->setPreventReparenting($prevent_reparenting);
  107. $controller->delete($entities);
  108. }
  109. /**
  110. * Saves a menu link.
  111. *
  112. * After calling this function, rebuild the menu cache using
  113. * menu_cache_clear_all().
  114. *
  115. * @param \Drupal\menu_link\Entity\MenuLink $menu_link
  116. * The menu link entity to be saved.
  117. *
  118. * @return int|bool
  119. * Returns SAVED_NEW or SAVED_UPDATED if the save operation succeeded, or
  120. * FALSE if it failed.
  121. */
  122. function menu_link_save(MenuLink $menu_link) {
  123. return $menu_link->save();
  124. }
  125. /**
  126. * Inserts, updates, enables, disables, or deletes an uncustomized menu link.
  127. *
  128. * @param string $module
  129. * The name of the module that owns the link.
  130. * @param string $op
  131. * Operation to perform: insert, update, enable, disable, or delete.
  132. * @param string $link_path
  133. * The path this link points to.
  134. * @param string $link_title
  135. * (optional) Title of the link to insert or new title to update the link to.
  136. * Unused for delete. Defaults to NULL.
  137. *
  138. * @return integer|null
  139. * The insert op returns the mlid of the new item. Others op return NULL.
  140. */
  141. function menu_link_maintain($module, $op, $link_path, $link_title = NULL) {
  142. $menu_link_controller = \Drupal::entityManager()
  143. ->getStorage('menu_link');
  144. switch ($op) {
  145. case 'insert':
  146. $menu_link = entity_create('menu_link', array(
  147. 'link_title' => $link_title,
  148. 'link_path' => $link_path,
  149. 'module' => $module,)
  150. );
  151. return $menu_link->save();
  152. case 'update':
  153. $menu_links = entity_load_multiple_by_properties('menu_link', array('link_path' => $link_path, 'module' => $module, 'customized' => 0));
  154. foreach ($menu_links as $menu_link) {
  155. $menu_link->original = clone $menu_link;
  156. if (isset($link_title)) {
  157. $menu_link->link_title = $link_title;
  158. }
  159. $menu_link_controller->save($menu_link);
  160. }
  161. break;
  162. case 'enable':
  163. case 'disable':
  164. $menu_links = entity_load_multiple_by_properties('menu_link', array('link_path' => $link_path, 'module' => $module, 'customized' => 0));
  165. foreach ($menu_links as $menu_link) {
  166. $menu_link->original = clone $menu_link;
  167. $menu_link->hidden = ($op == 'disable' ? 1 : 0);
  168. $menu_link->customized = 1;
  169. if (isset($link_title)) {
  170. $menu_link->link_title = $link_title;
  171. }
  172. $menu_link_controller->save($menu_link);
  173. }
  174. break;
  175. case 'delete':
  176. $result = \Drupal::entityQuery('menu_link')->condition('link_path', $link_path)->execute();
  177. if (!empty($result)) {
  178. menu_link_delete_multiple($result);
  179. }
  180. break;
  181. }
  182. }
  183. /**
  184. * Implements hook_system_breadcrumb_alter().
  185. */
  186. function menu_link_system_breadcrumb_alter(array &$breadcrumb, array $attributes, array $context) {
  187. // Custom breadcrumb behavior for editing menu links, we append a link to
  188. // the menu in which the link is found.
  189. if (!empty($attributes[RouteObjectInterface::ROUTE_NAME]) && $attributes[RouteObjectInterface::ROUTE_NAME] == 'menu_ui.link_edit' && !empty($attributes['menu_link'])) {
  190. $menu_link = $attributes['menu_link'];
  191. if (($menu_link instanceof MenuLinkInterface) && !$menu_link->isNew()) {
  192. // Add a link to the menu admin screen.
  193. $menu = entity_load('menu', $menu_link->menu_name);
  194. $breadcrumb[] = Drupal::l($menu->label(), 'menu_ui.menu_edit', array('menu' => $menu->id));
  195. }
  196. }
  197. }