views:

40

answers:

1

I have the following module, which affects how users arrive at two different content types, "question" and "poll". Question is a CCK content type, poll is the default poll content type, but it works perfectly for question, taking a user who goes to, i.e.,

http://sparechangenews.net/content/are-you-seeing-evidence-economic-recovery (which is a "question" content type)

to

http://www.sparechangenews.net/question/270 (with 270 being the NID)

but

http://sparechangenews.net/question/what-would-you-see-more-spare-change-news (which is a poll)

just goes to the normal url. I need that redirect because I have a view that checks the NID (pulling it from the URL) for nodes that references it, and uses them as sort of super comments (ripped from this tutorial). Am I referencing poll wrong in the module? Any tips?

<?php
/**
 * @file
 * An example module to redirect the path from a node view for at
 * specific type to a view.
 */

/**
 * Implementation of hook_init().
 */
function example_init() {
  // Using arg() instead of menu_get_item(). Rumor has it menu_get_item
  // can occassionally cause WSOD.
  // We make sure arg(2) is empty so we do not redirect on edit or other
  // node sub pages.
  if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) == '') {
    $node = node_load(arg(1));
    if ($node->type == 'answer') {
      drupal_goto('question/'. $node->field_answer[0]['nid']);
    }
    elseif ($node->type == 'poll' || $node->type == 'question') {
      drupal_goto('question/'. $node->nid);
    }
  }
}
+1  A: 

I'm not sure what is wrong with your code, but an alternative you might want to check out is http://drupal.org/project/path_redirect in conjunction with https://drupal.org/project/pathauto. You can set up automatic aliases for specific content types and also use path redirect to maintain old alias and redirect to new ones.

However, I feel like you probably should be able to achieve what you are trying to do without any redirection. If you are just trying to get the nid as a views argument, why not check under "Provide default argument" the "Node ID from URL", or alternatively check "PHP" and write some custom code like you have above:

$nid = arg(1);

Regardless, I would recommend tackling the issue with getting whatever you need into views without redirection as it will cut down on the complexity (and overhead).

bkildow

related questions