It is a very common need to be able to enter a menu path similar to user/[uid]/profile
in the Drupal menu system, ([uid]
being a dynamic argument for the user's id) but that's not possible out of the box, and there are no modules which provide this functionality.
Yesterday I got such functionality working by using the often-unknown custom_url_rewrite_outbound() function.
The first step is to implement hook_menu()
to define a menu path that will be used as an identifier for all argument-supported paths:
What this path actually resolves to (drupal_goto() in this case) doesn't matter - it will never get called. The next part is to add this implementation of custom_url_rewrite_outbound() to your settings.php
file:
This code looks for all paths beginning with the 'yuba' prefix (the one we defined in our custom hook), and modifies them, depending on the specified argument.
The available arguments are account_uid, user_uid, and func_my_module_uid.
Examples in paths:
- yuba/foo/account_uid/bar
- This will replace the token with the UID of the user being viewed (if on a
/user/##
) page, or if you're on a node page (/node/##
), the UID of the node's author - yuba/foo/user_uid
- This will replace the token with the UID of the currently logged in user (in the global
$user
object) - yuba/foo/func_my_module_uid/bar
- This will replace the token with whatever value is returned by the function my_module_uid, which will recieve two arguments (
$original_path
, and$path
); this allows for custom functions that determine what UID is to be used.