3

I've created a view "myview" for a custom content-type.

Now I want: Any node of that content-type to be automatically redirected to that view page.

For example, if a user types: http://my-drupal-site/node/4 and that node is of my desired content-type, it needs to be redirected to http://my-drupal-site/myview/4

What I can do:

What I can do now is writing in my theme's template_preprocess_node() function following lines:

function myCustomTheme_preprocess_node(&$variables) {
    $arg1 = (int) arg(1);
    $isArg1Int = (is_int($arg1) && $arg1) ? (true) : (false);

    if (isset($variables['node']->type) && $isArg1Int) {
        if ($variables['node']->type == "MY-DESIRED-CONTENT-TYPE") {
            // REDIRECT!
            drupal_goto('myview/' . $variables['node']->nid);
        }
    }
}

That's pure hard-code, and I don't like it. Is there some elegant solution, may be using URL aliases or something like that?

5

You can use Panels like Patrick suggests, but Panels requires a lot of resources and it's a bit of a waste to only use it for this simple case.

There are different approaches to this, one could be to react to the full view of the node:

/**
 * Implements hook_node_view().
 */
function hook_node_view($node, $view_mode, $langcode) {
  if ($node->type == 'type' && $view_mode == 'full' && arg(0) == 'node') {
    drupal_goto('url/' . $node->nid);
  }
}

The if statement if true if the node is of the correct type, is being view in full view and the url is node/..., which should limit to node being view the standard way.

Another way of doing this, would be to use the redirect module or something similar to setup redirects for all nodes of the type to the view. A weakness to this approach could be path alias. But that way you would make a permanent redirect which is more SEO friendly.

2
  • Thanks Googletorp for the comment, however, to me it seems similar to the 'template_preprocess_node' approach I mentioned in my question - & I'm looking for solution which does not involve coding this way. However, can you say performance related issues on this approach?
    – Shafiul
    Mar 31 '12 at 17:39
  • 1
    @giga never ever use your theme for logic like this. If you want to avoide code you could use redirect, but then panels would be better since its code free. The price you pay here is performance (RAM needed by the server).
    – googletorp
    Apr 1 '12 at 6:44
2

You can definitely do this in Panels by adding a variant to the default node display override and then a selection rule for "content type = my_content_type."

You may also be able to just use Page Manager, part of Ctools and a dependency of Panels, to set this up but I'm not sure how to do it off the top of my head.

Node One has an excellent screencast on learning Page Manager and Panels.

4
  • 1
    I don't have panels module installed. Shouldn't it'll be an overkill to install a module like that (I'm assuming it's a heavy-weight module capable of doing many other things) to redirect? I was thinking something like Path or Url Aliases
    – Shafiul
    Mar 31 '12 at 6:21
  • If you are using pathauto, etc. to set your URLs, have you tried just overriding the path in views? For an example of this, look at the "Taxonomy term" view which is installed with views by default. Mar 31 '12 at 6:36
  • Thanks Patrick. That works in general. Say, I can have a path like 'node/%' (% is Node-ID) in my view for a custom content-type, but doing this results in: for OTHER content-types node/% shows "Page not found". Because, view for my custom content-type registers it's path in node/% :(
    – Shafiul
    Mar 31 '12 at 17:36
  • 1
    Use pathauto to set a special content type for nodes of that content type (e.g., node/content_type_x/%). Of course, this approach is not very good for SEO if you have already launched your site, as it will break URLs, but you can mitigate this somewhat using the drupal.org/project/redirect module. Apr 1 '12 at 1:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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