You are here

function me_menu_alter in me aliases 7

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

Implements hook_menu_alter().

File

./me.module, line 230
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 (isset($user_handler) && 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();
      foreach ($parts as $key => $val) {
        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 (isset($data['load arguments']) && 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 (!array_key_exists($parent_path, $callbacks)) {
        $parent_path = $path;
      }
      if (in_array($parent_path, $processed)) {
        $parts = explode('/', $new_path, MENU_MAX_PARTS);
        $parent_path = implode('/', array_slice($parts, 0, count($parts) - 1));
        if (!array_key_exists($parent_path, $callbacks)) {
          $parent_path = $path;
        }
      }
      if (isset($callbacks[$parent_path])) {
        $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 (isset($data['page arguments']) && !is_array($data['page arguments'])) {
          $data['page arguments'] = array();
        }
        $parts = explode('/', $new_path, MENU_MAX_PARTS);
        if (isset($data['page arguments'])) {
          $data['page arguments'] = array_merge(array(
            $data['page callback'],
            $parts,
          ), $data['page arguments']);
        }
        else {
          $data['page arguments'] = array(
            $data['page callback'],
            $parts,
          );
        }
        $data['page callback'] = 'me_handler';
      }
      $callbacks[$new_path] = $data;
      unset($callbacks[$path]);
      $processed[] = $path;
    }
  }
}