function menu_link_maintain

Inserts, updates, enables, disables, or deletes an uncustomized menu link.

Parameters

string $module: The name of the module that owns the link.

string $op: Operation to perform: insert, update, enable, disable, or delete.

string $link_path: The path this link points to.

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 value

integer|null The insert op returns the mlid of the new item. Others op return NULL.

4 calls to menu_link_maintain()
aggregator_save_category in drupal/core/modules/aggregator/aggregator.module
Adds/edits/deletes aggregator categories.
MenuRouterTest::testMenuItemHooks in drupal/core/modules/system/lib/Drupal/system/Tests/Menu/MenuRouterTest.php
Test menu maintenance hooks.
MenuRouterTest::testMenuLinkMaintain in drupal/core/modules/system/lib/Drupal/system/Tests/Menu/MenuRouterTest.php
Tests for menu_link_maintain().
standard_install in drupal/core/profiles/standard/standard.install
Implements hook_install().

File

drupal/core/modules/menu_link/menu_link.module, line 143
Enables users to create menu links.

Code

function menu_link_maintain($module, $op, $link_path, $link_title = NULL) {
  $menu_link_controller = Drupal::entityManager()
    ->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 = Drupal::entityQuery('menu_link')
        ->condition('link_path', $link_path)
        ->execute();
      if (!empty($result)) {
        menu_link_delete_multiple($result);
      }
      break;
  }
}