You are here

function admin_menu_copy_items in Administration menu 5

Same name and namespace in other branches
  1. 5.3 admin_menu.inc \admin_menu_copy_items()
  2. 5.2 admin_menu.inc \admin_menu_copy_items()

Recursively copy menu items from a source parent menu item to a target menu item.

Parameters

admin_items: The sliced administration menu array.

source_pid: A source parent menu item id from which children shall be copied.

target_pid: A target parent menu item id.

title: An optional string containing the token !title, that has already been passed through t().

tree: Whether to rebuild hierarchy from source parent menu item. Defaults to true.

1 call to admin_menu_copy_items()
admin_menu_adjust_items in ./admin_menu.inc

File

./admin_menu.inc, line 135

Code

function admin_menu_copy_items(&$admin_items, $source_pid, $target_pid, $title = NULL, $tree = TRUE) {
  global $_menu;
  if (isset($admin_items[$target_pid]) && isset($_menu['items'][$source_pid]['children'])) {
    foreach ($_menu['items'][$source_pid]['children'] as $mid) {
      $admin_items[$mid] = $_menu['items'][$mid];
      if (isset($title)) {
        $admin_items[$mid]['title'] = check_plain(strtr($title, array(
          '!title' => $admin_items[$mid]['title'],
        )));
      }
      $admin_items[$mid]['pid'] = $target_pid;

      // Only add child to target menu item if it does not already exist.
      if (!in_array($mid, $admin_items[$target_pid]['children'])) {
        $admin_items[$target_pid]['children'][] = $mid;
      }

      // Recurse into children.
      if (isset($_menu['items'][$mid]['children']) && count($_menu['items'][$mid]['children'])) {
        if ($tree) {
          admin_menu_copy_items($admin_items, $mid, $mid, $title);
        }
        else {
          admin_menu_copy_items($admin_items, $mid, $target_pid, $title, FALSE);
          unset($admin_items[$mid]['children']);

          // Note:
          // Uncomment following lines to also optionally remove unnecessary
          // parent items.
          // unset($admin_items[$target_pid]['children'][array_search($mid, $admin_items[$target_pid]['children'])]);
          // unset($admin_items[$mid]);
        }
      }
    }

    // sort childs
    usort($admin_items[$target_pid]['children'], '_menu_sort');
  }
}