menu_get_ancestors
Definition
menu_get_ancestors($parts)
includes/menu.inc, line 200
Description
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
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
| Name | Description |
|---|---|
| Menu system | Define the navigation menus, and route page requests to code based on URLs. |
Code
<?php
function menu_get_ancestors($parts) {
$number_parts = count($parts);
$placeholders = array();
$ancestors = array();
$length = $number_parts - 1;
$end = (1 << $number_parts) - 1;
$masks = variable_get('menu_masks', array());
// 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--) {
if ($i & (1 << $j)) {
$current .= $parts[$length - $j];
}
else {
$current .= '%';
}
if ($j) {
$current .= '/';
}
}
$placeholders[] = "'%s'";
$ancestors[] = $current;
}
return array($ancestors, $placeholders);
}
?> 