You are here

function menu_link_delete_multiple in Menu Link (Field) 7

Delete multiple menu links.

@todo Remove this function when http://drupal.org/node/1034732 lands.

Parameters

$mlids array: An array of menu link IDs.

$force boolean: Forces deletion. Internal use only, setting to TRUE is discouraged.

See also

menu_link_delete()

2 calls to menu_link_delete_multiple()
menu_link_field_delete in ./menu_link.field.inc
Implements hook_field_delete().
menu_link_field_update in ./menu_link.field.inc
Implements hook_field_update().

File

./menu_link.module, line 95

Code

function menu_link_delete_multiple(array $mlids, $force = FALSE) {
  if (!empty($mlids)) {
    $query = db_select('menu_links')
      ->fields('menu_links')
      ->condition('mlid', $mlids, 'IN');
    if (!$force) {

      // Exclude links belonging to system module except if they are marked
      // updated (generated during update from Drupal 5).
      $query
        ->condition(db_or()
        ->condition('module', 'system', '<>')
        ->condition('updated', 0, '<>'));
    }
    $links_to_delete = $query
      ->execute()
      ->fetchAllAssoc('mlid', PDO::FETCH_ASSOC);
    if (!empty($links_to_delete)) {
      $links_with_children = array();
      $parent_mlids = array();
      $affected_menus = array();
      foreach ($links_to_delete as $item) {
        if ($item['has_children']) {
          $links_with_children[$item['mlid']] = $item['mlid'];
        }
        $parent_mlids[$item['plid']] = $item['plid'];
        $affected_menus[$item['menu_name']] = $item['menu_name'];
      }
      $parent_mlids = array_diff_key($parent_mlids, array(
        0 => 0,
      ) + array_keys($links_to_delete));

      // Re-parent any children to it's closest parent that is not deleted.
      if (!empty($links_with_children)) {
        $children = menu_link_load_multiple(array(), array(
          'plid' => $links_with_children,
        ));
        foreach ($children as $item) {
          while (isset($links_to_delete[$item['plid']])) {
            $item['plid'] = $links_to_delete[$item['plid']]['plid'];
          }
          menu_link_save($item);
        }
      }
      db_delete('menu_links')
        ->condition('mlid', array_keys($links_to_delete), 'IN')
        ->execute();
      foreach ($links_to_delete as $item) {

        // Notify modules we have deleted the item.
        module_invoke_all('menu_link_delete', $item);

        // Update the has_children status of the parent.
        _menu_update_parental_status($item);
      }

      // Update the has_children status of parents of deleted links.
      // @todo fix query und use this instead of _menu_update_parental_status($item);

      /*if (!empty($parent_mlids)) {
              $exists_query = db_select('menu_links', 'child')
                ->fields('child', array('mlid'))
                ->condition('child.hidden', 0)
                ->where('child.plid = parent.mlid')
                ->where('child.menu_name = parent.menu_name')
                ->range(0, 1);

              db_update('menu_links', 'parent')
                ->fields(array('has_children' => 0))
                ->condition('has_children', 1)
                ->condition('mlid', $parent_mlids, 'IN')
                ->notExists($exists_query)
                ->execute();
            }*/

      // Clear caches.
      foreach ($affected_menus as $menu_name) {
        menu_cache_clear($menu_name);
      }
      _menu_clear_page_cache();
    }
  }
}