Building A Stack Overflow Clone With Drupal - Part 2

After building out the Question and Answer content types in the first step of creating a Stack Overflow clone with Drupal it’s time to create the page that displays the Question, its Answers, and the comments. Ideally this would be done entirely with contrib modules but, there is one missing feature. Luckily it only takes a few lines of code in a custom module to fill that hole for our specific case.

The View

To display all these things together we'll use the Views module. Make sure you have that and the Views UI module installed. Head over to Administer > Site Building > Views > Add, fill in a name for the view, make sure the View type is set to Node, and click Next.

In this new view we want to set a few default options. First, set the Row style to Node with the Build mode as Full node and Display links and Display node comments checked. Set the Items to display to 1. For the arguments add Node: Nid. Set Action to take if argument is not present to Hide view / Page not found (404). For the Validator select Node with a Question type. Oh, and set the title to be %1.

Next, add the filter of Node: Published (with a yes value).

Now, add a page display and give it the path question/%. We just created the page display and you might be asking, why don’t we just use the standard node view? What we have created at this point is basically just that. This next part is where the difference comes in.

The Attachment

On the stack overflow questions they display the answers and the comments to the answers below them. We want to do the same thing.

So, add an Attachment display type. A big fat note. All the changes here should be made as overrides. Do not change the default settings.

Under the Attachment settings set the Position to After and the Attach to to be the Page. Remove the Argument and add a Relationship to the node reference we previously setup (make it required). Then, under Filters add one for the Node: type of Answer.

Finally, click save. We now have our page that will display the questions and their comments alongside the answers and their comments.

The Custom Code

But, we have a problem. When you submit a comment or an answer or reply to one of their comments you're going to end up back on the node display. We want to come back to the view instead. Sadly, there is no Drupal module doing this right now. So, we need a bit of custom code. Below is an example.module file containing the custom code.
<?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 == 'question') {
      drupal_goto('question/'. $node->nid);
    }
  }
}

The code is fairly straight forward. If the node type is a question or answer it redirects to the appropriate view.

Next up, we’ll look at the voting system.