Is it possible to use hook_menu() to call a function rather than display a page? The main goal here is to be able to create a link inside of a node body in the WYSIWYG editor and have that link go to a particular tab of the logged in user's profile page. For example, I want to make a link to /user/1/edit/personalinfo if I am logged in as user 1 and have it go to /user/4/edit/personalinfo if I am logged in as user 4 etc. I want to be able to have this link in the body of a node. I can't allow PHP in the node body since users without the allow PHP permission also need to update this node on occasion.

What I was hoping I could do was add a link called something like "myinfo" in the html for the node and then use use hook_menu() to intercept that and get the $user->uid id from the logged in user and redirect to /user/(insert user id)/edit/personalinfo.

Is it possible to do this with hook_menu()? Is there another way to accomplish this?

Thanks for your help.

Comments

jaypan’s picture

Are you trying to display information from another page inside your node body, or are you trying to create a link the user can click to go to another page?

Contact me to contract me for D7 -> D10/11 migrations.

nrackleff’s picture

Not exactly either of those. I need to have link inside of the body of a node - a link that is created by using the WYSIWYG editor. This link needs to take whomever clicked it to their profile editing screen. The problem is, the link has to be the same for everyone because i can't use PHP in the WYSIWYG editor. The link needs to be something like <a href = "/myprofile">my profile</a>. When user number 4 clicks it, it needs to take them to /user/4/edit/personalinfo. If user 6 clicks it, they need to go to /user/6/edit/personalinfo.

I can't make the href dynamic since it's in the body of a node where I cannot allow PHP code. So, I'm looking for a way to intercept that link after it's clicked, determine who the logged in user is and redirect them to the correct tab on their profile edit page.

I was trying to use hook_menu to create a "myprofile" item and have it be a redirect to the users profile, but I don't know how to do that or if I even can do that.

j_ten_man’s picture

You'll probably need to do something like this:

//Inside of hook_menu
 $items['myprofile'] = array(
    'title' => 'My Profile Redirect',
    'type' => MENU_CALLBACK,
    'page callback' => 'myfunction_redirect',
    'access callback' => TRUE,
  );

function myfunction_redirect() {
  global $user;
  if ($user->uid) {
    drupal_goto('user/'. $user->uid .'/edit');
  }
  
  //Not sure what you want to do here if this is non-authenticated user. You could always set up your menu item to make sure the user is logged in
}

If you aren't writing a module, the other workaround is to create a page that has a url alias of myprofile. Then that page is just the code from the myfunction_redirect() function above and it uses the php filter.

nrackleff’s picture

That looks promising. I'll give it a try and let you know how it works. I will put it in a module as I am trying to avoid adding individual php files to the site to do the redirect. It's funny that you mentioned that because that is actually how we are doing it now - with the php page - and we're trying to get away from that.

Thanks for your help,
Nancy

johnhanley’s picture

You CAN use hook_menu to do other things besides defining menu entries. In fact back in the "olden days" of 4.6 this was a somewhat common technique to perform other tasks earlier in the build process. Now-a-days there are more hooks available so hook_menu may or may not be the most appropriate place.

That said, you might want to check out the third-party modules available that handle redirects.

mooffie’s picture

You could use a module like RepTag. This module lets you define tokens which you can then embed in your nodes. You could define a token that translates into the required link.

("Bacteria Man" mentioned hook_menu() not being the only useful hook. Indeed, RepTag, on appropriate occasions, uses a certain hook to alter the node text just before it's displayed.)

hoppurr’s picture

wouldn't something like this be more concise?

<?php
//Inside of hook_menu
  global $user;
if (!$may_cache) {
$items['myprofile'] = array(
    'title' => 'My Profile Redirect',
    'type' => MENU_CALLBACK,
    'page callback' => 'drupal_goto',
    'callback aruments'=>array('user/'. $user->uid .'/edit'),
    'access callback' => $user->uid,
  );
}
  //Not sure what you want to do here if this is non-authenticated user. You could always set up your menu item to make sure the user is logged in
}
?>
j_ten_man’s picture

This is a combination of the Drupal 5 and Drupal 6 menu system. The $may_cache variable was part of D5. As of D6, the menu system is cached always.