Building A Stack Overflow Clone With Drupal - Part 1

Since I wrote the post about the style of conversation on Stack Overflow being the next revision for forums I've had numerous people ask how to build it in Drupal. Since there are quite a few features, even with its simple interface, I'll be writing a series of posts outlining how to build the clone. And, for those of you who want the same style but something simpler I'll be writing a post about that as well. The goal of this series of posts is to use contributed modules to build everything.

Create Your Content Types

I'll assume you have a default Drupal install waiting to be crafted into something special. (If you need to know how to install Drupal here is a video.) At the core of this we have the Question and Answer content types. The Answer content type can't simply be comments because both Questions and Answers can have comments attached to them.

The Answer content type will need an association to the Question content type. To do this we'll use the CCK Node Reference module and the Node Reference URL module, which will automate the creation of the reference between the nodes. Download and install these modules so we can use them on the content types.

We start by creating the Question content type (go to Administer > Content management > Content types > Add content type on your new site). The name will be Question and the type will be question. (The type is the machine name in Drupal.) Under Submission form settings set the title to Question and update the body field label to something like Description. If you would like to remove the title on the body all together you'll need to remove the name here which will hide the field. Then add a cck text field in its place.

Next, set the comments on the question type to be Flat list - collapsed and to display the oldest first.

The Answer Content Type

The Answer content type doesn't have a title to fill in. To enable Drupal nodes to not have titles we need the Automatic Nodetitles module. Make sure this module is enabled before creating the Answer content type.

Create a new content type for Answer (at Administer > Content management > Content types > Add content type). At the very top under Automatic title generation choose Automatically generate the title and hide the title field. This will handle and remove the title field. For the name and type choose Answer.

Under Submission form settings for the body field label enter Answer and set the comment settings up the same way as the Question content type.

After you've saved the content type you should return to the content types list. Next to the Answer type choose Manage Fields. Create a new field with the label of Answer, name of field_answer, Type of data to store as Node reference, and Form element to edit the data as Reference from URL. Click save.

On the next screen set the Link title to Answer the question, the Return path to The referenced node, set to be required, and set Content types that can be referenced to the Question type. Then save the field settings.

Test It Out

Now is a good time to test your new creation. Go ahead and make a test question. After you create a test question you should see a Answer the question which will bring up the node add form with the referring node already filled in.

question.jpg
A Question Being Viewed.

Next up, we'll make it look a little more like Stack Overflow by creating some views.