function menu_get_ancestors

×

Error message

User warning: The following module is missing from the file system: theme/theme. For information about how to fix this, see the documentation page. in _drupal_trigger_error_with_delayed_logging() (line 1156 of /var/www/html/elmsln_community/api.elmsln.org/includes/bootstrap.inc).
cis7 menu.inc menu_get_ancestors($parts)
cle7 menu.inc menu_get_ancestors($parts)
elmsmedia7 menu.inc menu_get_ancestors($parts)
icor7 menu.inc menu_get_ancestors($parts)
meedjum_blog7 menu.inc menu_get_ancestors($parts)
mooc7 menu.inc menu_get_ancestors($parts)

Returns the ancestors (and relevant placeholders) for any given path.

For example, the ancestors of node/12345/edit are:

  • node/12345/edit
  • node/12345/%
  • node/%/edit
  • node/%/%
  • node/12345
  • node/%
  • node

To generate these, we will use binary numbers. Each bit represents a part of the path. If the bit is 1, then it represents the original value while 0 means wildcard. If the path is node/12/edit/foo then the 1011 bitstring represents node/%/edit/foo where % means that any argument matches that part. We limit ourselves to using binary numbers that correspond the patterns of wildcards of router items that actually exists. This list of 'masks' is built in menu_rebuild().

Parameters

$parts: An array of path parts; for the above example, array('node', '12345', 'edit').

Return value

An array which contains the ancestors and placeholders. Placeholders simply contain as many '%s' as the ancestors.

Related topics

2 calls to menu_get_ancestors()
menu_get_item in includes/menu.inc
Gets a router item.
_menu_find_router_path in includes/menu.inc
Finds the router path which will serve this path.

File

includes/menu.inc, line 319
API for the Drupal menu system.

Code

function menu_get_ancestors($parts) {
  $number_parts = count($parts);
  $ancestors = array();
  $length = $number_parts - 1;
  $end = (1 << $number_parts) - 1;
  $masks = variable_get('menu_masks');
  // If the optimized menu_masks array is not available use brute force to get
  // the correct $ancestors and $placeholders returned. Do not use this as the
  // default value of the menu_masks variable to avoid building such a big
  // array.
  if (!$masks) {
    $masks = range(511, 1);
  }
  // Only examine patterns that actually exist as router items (the masks).
  foreach ($masks as $i) {
    if ($i > $end) {
      // Only look at masks that are not longer than the path of interest.
      continue;
    }
    elseif ($i < (1 << $length)) {
      // We have exhausted the masks of a given length, so decrease the length.
      --$length;
    }
    $current = '';
    for ($j = $length; $j >= 0; $j--) {
      // Check the bit on the $j offset.
      if ($i & (1 << $j)) {
        // Bit one means the original value.
        $current .= $parts[$length - $j];
      }
      else {
        // Bit zero means means wildcard.
        $current .= '%';
      }
      // Unless we are at offset 0, add a slash.
      if ($j) {
        $current .= '/';
      }
    }
    $ancestors[] = $current;
  }
  return $ancestors;
}
Error | ELMSLN API

Error

×

Error message

  • Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/elmsln_community/api.elmsln.org/includes/common.inc:2791) in drupal_send_headers() (line 1499 of /var/www/html/elmsln_community/api.elmsln.org/includes/bootstrap.inc).
  • Error: Call to undefined function apc_delete() in DrupalAPCCache->clear() (line 289 of /var/www/html/elmsln_community/api.elmsln.org/sites/all/modules/apc/drupal_apc_cache.inc).
The website encountered an unexpected error. Please try again later.