Forwarding a node URL to a Drupal view

Events happening in the community are now at Drupal community events on www.drupal.org.
morisy's picture

I recently inherited some great work done by folks that allowed Stackoverflow-style Q&A on our Drupal site using this tutorial. We'd like to expand the great work they did by adding in polls as a question type, so that there is a poll that people can respond to, and then they can also provide more in depth answers below. The normal question type works perfectly, but the poll type allows people to do everything except see submitted answers: You can see both examples on our live site now.

I think what's going wrong is that the node being displayed for the Poll is using the actual node page, and not forwarding to /question/[Node ID], which is what happens for the question content type, and which is done by the following module:

<?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') {
      drupal_goto('question/'. $node->nid);
    }
    elseif ($node->type == 'question') {
      drupal_goto('question/'. $node->nid);
    }
  }
}

Have I screwed up something in the PHP? Do I not know how to do a PHP Ifelse to save my life? Am I barking up the wrong tree entirely? Thanks for any pointers.

Comments

Hmm...

nancydru's picture

Couldn't you just enable comments for this node and use them for the additional answers? Then they should show automatically.

So the project also calls for

morisy's picture

So the project also calls for extra fields than comment gives you, like a drop down for who you buy your newspaper from, where you live, and name optional, plus we are considering doing some special stuff like mapping these comments or something in the future; that's why but I might end up going with that if these continues the frustration :) I tried node comment but it didn't sit well with some other elements on the system, I can't remember what but it might have been the voting or something.

Web guy, SpareChangeNews.net
Twitter: @morisy / @sparechangenews

Sounds like a job for...

forestmars's picture

Hmm, fieldable comments...?

Sounds like someone might be well-advised to look into Drupal 7!

~ Forest Mars

Just in case ya don't know,

hefox's picture

Just in case ya don't know, ya can easily add views to node pages/ node content, ie via panels, views attach, or core blocks, context, views_embed_view() in hook_nodeapi $op = view or hook_preprocess_node, etc.

I use menu_get_object() in general to get the node, and !arg(2) but that shouldn't matte, but it looks fine, so debugging, test statements to see wth is wrong is my suggestion.

So we actually have a views

morisy's picture

So we actually have a views attach there, which is what is supposed to be pulling in the "node comments", but to pull in the right comments, it checks against the URL for the node ID. This works fine in the first case, but maybe I should double check that node attach now that I think about it ...

Web guy, SpareChangeNews.net
Twitter: @morisy / @sparechangenews

Boston

Group categories

More Specifically

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: