You are here

function me_handler in me aliases 8

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

A special menu callback function that either redirects to a page with the uid in the path, or calls the real menu handler.

Parameters

$parts: The menu parts we are working with.

$callback: The page callback to call.

...: count($parts) arguments for each part of the actual path

...: Any extra arguments will be the real page arguments.

Return value

mixed Whatever the real page callback returns.

2 string references to 'me_handler'
me_menu_alter in ./me.module
Implements hook_menu_alter().
_me_check_arg in ./me.module
A Helper function to check for the 'me' alias.

File

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

Code

function me_handler($parts, $callback) {

  // Get the arguments, and shift off $parts and $callback.

  /*$parts and $callback are left here for compatability, they are not going to be used after the fix for
   * http://drupal.org/node/1630044*/
  $args = func_get_args();
  $callback = array_shift($args);
  $parts = array_shift($args);

  // If we want the uid shown in the address bar, we need to do a redirect.
  if (me_variable_get('me_redirect') || _me_user_disabled() || !_me_handle_path($_GET['q'])) {
    $redirect = FALSE;

    // Get the menu path arguments.
    $menu_parts = explode('/', $_GET['q'], MENU_MAX_PARTS);

    // Loop over each part. If it's a %me wildcard, then
    // check the corresponding menu part for the me alias,
    // if so, replace it out with the user id so we can redirect correctly.
    // If no changes are required, then call the required function.
    foreach ($parts as $key => $val) {
      if (0 === strpos($val, '%me') && _me_is_alias($menu_parts[$key])) {
        $redirect = TRUE;
        $menu_parts[$key] = $GLOBALS['user']->uid;
      }
    }
    if ($redirect) {
      $path = implode('/', $menu_parts);

      // Save on an extra redirect by also checking the anonymous redirect here.
      $redirect_path = me_variable_get('me_redirect_anonymous');
      if ($GLOBALS['user']->uid == 0 && !empty($redirect_path)) {
        $path = $redirect_path;
      }
      drupal_goto($path);
    }
  }

  // Before going any further, set the current menu router item to include
  // paths with %user, which allows modules to use menu_get_object() instead
  // of arg() in blocks and the like.
  $router_item = menu_get_item();
  foreach ($router_item['load_functions'] as $index => $function) {

    // If the function is a me handled function, then swap the handler out with user.
    if (0 === strpos($function, 'me')) {
      $router_item['load_functions'][$index] = 'user_load';
    }
  }
  menu_set_item($_GET['q'], $router_item);
  return call_user_func_array($callback, $args);
}