I have a node page that should redirect to another node's page instead of displaying itself...
How can I do that ?
Many thanks !
I have a node page that should redirect to another node's page instead of displaying itself...
How can I do that ?
Many thanks !
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.
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".