You are here

function simplemeta_path_get_ancestors in Simple Meta 8.2

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

Get path ancestors.

Get path ancestors (represented as parts) to find appropriate SimpleMeta entity for the page.

Parameters

string[] $parts: An array of path parts; for example, array('node', '12345', 'edit').

Return value

string[] List of path ancestors.

1 call to simplemeta_path_get_ancestors()
simplemeta_get_page_meta in ./simplemeta.module
Get SimpleMeta entity for a page, by path.

File

./simplemeta.module, line 164
Contains simplemeta.module.

Code

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

  // Menu_masks actually takes defined menu paths (via hook_menu()
  // implementation) into account.
  $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;
}