$menu_link->link_path, ); } /** * 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\Plugin\Core\Entity\MenuLink|false * A menu link entity, or FALSE if there is no entity with the given ID. */ function menu_link_load($mlid = NULL, $reset = FALSE) { return entity_load('menu_link', $mlid, $reset); } /** * 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\Plugin\Core\Entity\MenuLink> * An array of menu link entities indexed by entity IDs. * * @see menu_link_load() * @see entity_load_multiple() */ function menu_link_load_multiple(array $mlids = NULL, $reset = FALSE) { return entity_load_multiple('menu_link', $mlids, $reset); } /** * 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_container()->get('plugin.manager.entity') ->getStorageController('menu_link'); if (!$force) { $entity_query = entity_query('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->load($result); } else { $entities = $controller->load($mlids); } $controller->preventReparenting($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\Plugin\Core\Entity\MenuLink $menu_link * The menu link entity to be saved. */ function menu_link_save(MenuLink $menu_link) { $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_container()->get('plugin.manager.entity') ->getStorageController('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 = entity_query('menu_link')->condition('link_path', $link_path)->execute(); if (!empty($result)) { menu_link_delete_multiple($result); } break; } }