You are here

function admin_menu_copy_items in Administration menu 5.2

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

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

Parameters

array $_admin_menu: An array containing the complete administration menu structure, passed by reference.

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

int $target_pid: A target parent menu item id.

string $title: An optional string containing the token !title, that has already been passed through t(), which will be used to dynamically replace previous menu item titles.

bool $tree: Whether to rebuild the complete hierarchy from the source parent menu item or copy menu items flattened. Defaults to TRUE.

1 call to admin_menu_copy_items()
admin_menu_adjust_items in ./admin_menu.inc
Add some hard-coded features for better user experience.

File

./admin_menu.inc, line 298
Cached builder functions for Drupal Administration Menu.

Code

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

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

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

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