You are here

function simplemeta_path_get_ancestors in Simple Meta 7

Same name and namespace in other branches
  1. 8.2 simplemeta.module \simplemeta_path_get_ancestors()
  2. 8 simplemeta.module \simplemeta_path_get_ancestors()
  3. 7.2 simplemeta.module \simplemeta_path_get_ancestors()

Get path ancestors of the path (represented as parts) to find page's SimpleMeta configuration.

See also

menu_get_ancestors()

1 call to simplemeta_path_get_ancestors()
simplemeta_get_page_meta in ./simplemeta.module
Get page's meta data.

File

./simplemeta.module, line 667
SimpleMeta module.

Code

function simplemeta_path_get_ancestors($parts) {
  $number_parts = count($parts);
  $ancestors = array();
  $length = $number_parts - 1;
  $end = (1 << $number_parts) - 1;

  // menu_masks actually takes defined menu paths (via hook_menu() implementation) into account.
  // @todo function probably should return list of all possible ancestors (but it's a bigger list actually).
  $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;
    }

    // Path patterns which have less parts than original path must end with %
    // this also includes shorter paths without %
    if ($length < $number_parts - 1 && $i & 1) {
      continue;
    }
    $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;
}