0
votes

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
Can you add to your question the functionality you're trying to accomplish? - user113292
Your condition starts out "if (arg(0) == 'node' ...)" but isn't arg(0) equal to "content" in your first example URL and to "question" in the second example? Or am I not understanding the arg() function correctly? - Dan U.
@Dan U. You might be on to something, but in Drupal, I thought all content types would be true as nodes? Also, if that was what was wrong, wouldn't both be broken, instead of just the poll type? - Michael Morisy
Good point, unless it's something elsewhere in the code that's causing the redirection. I'd suggest debugging by echoing out the arg parameters and also lines within each condition, just to see what's firing (comment out the drupal_goto and add some echo there just to see if you're getting to the conditional block, etc.) - Dan U.

1 Answers

1
votes

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).