3

I have a node page that should redirect to another node's page instead of displaying itself...

How can I do that ?

Many thanks !

2
  • What have you tried so far and did you get an error with what you tried? Posting additional information (code, error message(s) etc) will make it more likely that your question gets answered successfully
    – RobV
    Commented Oct 25, 2012 at 17:48
  • I had tried nothing when I wrote this post, because I had no idea of what could be a good solution for this. Now I've tried a dirty header() in the template, which works, but is definitely not the best solution, I guess.
    – theredled
    Commented Oct 25, 2012 at 18:33

2 Answers 2

1

Rather than using a node to redirect to another one, I would implement hook_menu() to define a new menu callback that renders the other node, or redirect to the other node.
The alternative would be implementing hook_node_view() to add the other node to the node being viewed (when it is the correct one). For example, the following code would work.

function mymodule_node_view($node, $view_mode, $langcode) {
  if ($node->nid == $node_to_alter) {
    $node->content['my_module_additional_content'] = node_view($the_other_node);
  }
}

This is the approach followed by Drupal where, instead of using the PHP filter (which would make a node editable only from Drupal.org administrators), a custom module is used to add content to specific nodes.

A node that redirects to another node would create problems because:

  • If you are doing the redirect when rendering the node, you would create problems with all the functions that render a node. The assumption is that a node is rendered when it is going to be visualized, but that is always true, as (for example) node_search_execute() renders the node while executing a search for a set of key words. There are other 10 functions that call node_view(), including the one rendering multiple nodes; all of them would have a problem, if the user is redirected in the middle of what they are doing, which means PHP stops its execution as exit() is being executed.
    If you are using the PHP filter to execute PHP as node content, that is not suggested to do. If you need to dynamically render a node content, you should implement a module which could be implement an input filter, or other hooks, depending from the needs.

  • If you are using a custom module to redirect the users when the node is being loaded, or rendered (e.g. implementing hook_node_load(), or hook_node_view()), you would have the same problem I described first. Plus, as you are already using a custom module, then you can implement one of the solutions I have proposed.

As for other suggestions:

  • hook_menu_alter() is used to alter the existing menus. For the nodes, the menu callback is the one associated to node/%node, which is invoked for every node. It is possible to use this hook to alter the page callback with a different one that first check which node is being viewed, and then redirect the users to the other node. In this case, there would not be problems, as the function is called when the node is being viewed, not when a node is being loaded, or being rendered in memory.
    IMO, it would be a sub-optimal solution. I would rather implement hook_menu() to define a new menu callback.

  • The Path redirect allows to set static redirects from the UI. This doesn't help if the intent is having a dynamic redirect, which seems the case here.

  • The Rules module can be useful, but I would rather use it if you need it for more than what you are trying to achieve here. It is a better solution that enabling the PHP filter module to execute PHP code inside a node body, but it would probably require you to write PHP code that is executed through eval(), which should generally be avoided.

0

You have several ways of doing this, you could use Path Redirect module to redirect the first page to the second one or in the body of the node use the drupal_goto() function (with PHP input filter enabled), although the first option is by far the best and more "clean".

3
  • Thanks. However, in my case, the second page must be found dynamically (the first node of a list). I guess the second solution is the simplest one for me ?
    – theredled
    Commented Oct 25, 2012 at 18:28
  • 2
    DON'T. Make a module with hook_menu() and register your own page callback and do your drupal_goto() in that page. Trust me, doing a drupal_goto() on nodes will ruin your site (drupal_got() die()'s the request so your search, etc will stop working).
    – AKS
    Commented Oct 25, 2012 at 20:18
  • Right, if you need to search for the content dynamically the method described above will work, although I would use hook_menu_ater() or hook_nodeapi() instead. Maybe the Rules module can help you as well although it's a bit too big.
    – Flupkear
    Commented Oct 26, 2012 at 19:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.