You are here

function me_menu_alter in me aliases 6

Same name and namespace in other branches
  1. 8 me.module \me_menu_alter()
  2. 6.2 me.module \me_menu_alter()
  3. 7 me.module \me_menu_alter()

Implementation of hook_menu_alter().

File

./me.module, line 246
Provides 'me' aliases to allow users to enter 'me' in common paths instead of their user id.

Code

function me_menu_alter(&$callbacks) {

  // Loop over each of the paths, finding all %user* loaders,
  // and replace them with a %me equivelant. This should catch
  // all drupal modules that use the %user loader to load up
  // user objects, which should be most well written D6 modules.
  // Certainly all of core.
  $processed = array();

  //XXX: For now, we only handle known user loaders. I might make a module hook, or a configuration

  // area to allow these to be exteneded if users make the requests.
  $handlers = array(
    '%user' => '%me',
    '%user_uid_optional' => '%me_uid_optional',
    '%user_category' => '%me_category',
  );
  foreach ($callbacks as $path => $data) {
    $found = FALSE;
    $parts = explode('/', $path, MENU_MAX_PARTS);
    foreach ($handlers as $user_handler => $me_handler) {
      if (in_array($user_handler, $parts)) {
        $found = TRUE;
        break;
      }
    }
    if ($found) {

      // We need to make sure that the correct files are loaded up. when the path is used.
      if (isset($data['file']) && !isset($data['file path'])) {
        $data['file path'] = drupal_get_path('module', $data['module']);
      }

      // We need to find the right page callback and page arguments to make
      // the me handler work correctly.
      $new_parts = array();
      while (list($key, $val) = each($parts)) {
        if (array_key_exists($val, $handlers)) {
          $val = $handlers[$val];
        }
        $new_parts[] = $val;
      }
      $new_path = implode('/', $new_parts);

      // We need to be careful with load arguments due too http://drupal.org/node/373568.
      // We therefore only add load arguments if there are some there already.
      // The only load argument that needs to be passed by reference is map.
      // We make sure that we have map in the right place to be passed by reference.
      if (is_array($data['load arguments'])) {

        // Find the current map index, and add our load arguments, putting map
        // in the place we expect it to be.
        if (FALSE !== ($map_index = array_search('%map', $data['load arguments']))) {
          unset($data['load arguments'][$map_index]);
          array_unshift($data['load arguments'], '%map', '%index', strval($map_index));
        }
      }

      // First, we need to find the parent.
      $parent_path = implode('/', array_slice($parts, 0, count($parts) - 1));
      if (in_array($parent_path, $processed)) {
        $parts = explode('/', $new_path, MENU_MAX_PARTS);
        $parent_path = implode('/', array_slice($parts, 0, count($parts) - 1));
      }
      $parent = $callbacks[$parent_path];
      if (!isset($data['page callback']) && isset($parent['page callback'])) {
        $data['page callback'] = $parent['page callback'];
        if (!isset($data['page arguments']) && isset($parent['page arguments'])) {
          $data['page arguments'] = $parent['page arguments'];
        }
        if (!isset($data['file']) && isset($parent['file'])) {
          $data['file'] = $parent['file'];
        }
        if (!isset($data['file path']) && isset($parent['file path'])) {
          $data['file path'] = $parent['file path'];
        }
      }
      if (isset($data['page callback'])) {
        if (!is_array($data['page arguments'])) {
          $data['page arguments'] = array();
        }
        $parts = explode('/', $new_path, MENU_MAX_PARTS);
        $data['page arguments'] = array_merge(array(
          $parts,
          $data['page callback'],
        ), $data['page arguments']);
        $data['page callback'] = 'me_handler';
      }
      $callbacks[$new_path] = $data;
      unset($callbacks[$path]);
      $processed[] = $path;
    }
  }
}