How to Create Drupal Forums and Containers Programmatically

Creating a Container or a Forum in Drupal using the API is very easy. However, the lack of useful examples makes this seem almost impossible for beginners. The example below will show you how to create Drupal forums and containers Programmatically.

We begin as always by bootstrapping Drupal into our PHP script. (This isn't required if your building a modules as Drupal will already be loaded.)

<?php
//Load and init the Drupal System
require_once 'includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
?>

Adding a forum or container requires that the forum admin module gets loaded, so in addition to the usual, we must add the following code.

<?php
require_once 'modules/forum/forum.admin.inc';
?>

Now we get to the good part, lets begin by adding a container. Just like we do for nodes, we must create an array that stores the required form fields to create a container object. The first two fields are self explanatory.

The "parent" and "weight" fields are exactly what they are when you use the GUI. For this example we will leave the parent field's value as 0 so the container is created on the root. We leave weight as 0 since we are not interested in it at this time.

The "vid" field must match the taxonomy vocabulary id that is assigned to "Forum Topics" type. You can look that up by going to the Taxonomy area and hovering your mouse over "edit vocabulary". The number at the end of the URL is the ID you need. In this example, that ID is "1".

<?php
$forum_container_fields
= array();
$forum_container_fields['values']['name'] = 'Test';
$forum_container_fields['values']['description'] = 'Test';
$forum_container_fields['values']['parent'][0] = 0;
$forum_container_fields['values']['weight'] = 0;
$forum_container_fields['values']['vid'] = 1;
?>

The best way to create the container is to use the built in "forum_form_container" and "forum_form_submit" functions. The first function generates an abstract form object of the "forum container" type. Strangely, this function does not setup the "form_id" field properly, so we do that manually. We must save this form object as a variable that we pass to the second function which does the actual creation.

<?php
$container
= forum_form_container($forum_container_fields);
$container['form_id']['#value'] = 'forum_form_container';
forum_form_submit($container, $forum_container_fields);
?>

To make sure everything went well, we can take a look at any status or error messages Drupal sets in our session variable by using the "drupal_get_messages" function. Note that this function will return an array of messages (if any).

<?php
print_r
(drupal_get_messages());
?>

The same concept applies when creating forums. However, in this example, I'll choose to create the forum inside the container I just made above.

First I need to get the ID of the container so I can use it in the parent field. In essence, all we are doing here when creating forums and containers is creating vocabulary terms. So my container named "Test" is actually a term named "Test". We can use the Drupal function "taxonomy_get_term_by_name" to get the ID.

<?php
$containerID
= taxonomy_get_term_by_name('Test');
?>

This will return an array of term objects that match the name "Test". If you organize your Drupal design properly, this shouldn't be a problem, and therefore, you'll get a single array (0).

Lets build the fields for our forum, and use the ID of our recently created container in the "parent" field. The ID is retrieved as shown. If you've read some of the other Drupal HelpFiles, you'll know what the structure of the term object is, and therefore see that the field "tid" is the value you want.

<?php
$forum_topic_fields
= array();
$forum_topic_fields['values']['name'] = 'Test4';
$forum_topic_fields['values']['description'] = 'Test';
$forum_topic_fields['values']['parent'][0] = $containerID[0]->tid;
$forum_topic_fields['values']['weight'] = 0;
$forum_topic_fields['values']['vid'] = 1;
?>

Similar to what we did with the container, use the "forum_form_forum" function to generate the abstract forum object. Then we use the "forum_form_submit" function to create our forum. Finally, print out any error or status messages.

<?php
$forum
= forum_form_forum($forum_topic_fields);
forum_form_submit($forum, $forum_topic_fields);
print_r(drupal_get_messages());
?>

Comments

D7 API changes

In D7 the forum_form_container function has changed as: forum_form_container($form, &$form_state, $edit = array()).

How to adapt your script to the new D7 api ?

The next step

Pretty cool. This creates containers and forum taxonomy entries, but how the heck do I create a node (and its replies) that lives inside the newly created "test" entries? I'm probably confused, but I thought the hierarchy was Container->Forum->ForumTopic with the FT being an actual node linked to a taxonomy Forum node. Example: /node/add/forum/51 is the url I see when adding a new "forum topic" in a previously created forum. The forum_topic_fields above in your example wind up creating a sub-forum under 51 instead of creating a new forum topic node linked to forum taxonomy 51. I think where I'm missing it is in the forum_form_forum (say that fast 3 times :) call. Is there a forum API I can reference somewhere or just dig through the .module? Thanks!

More detail please

Nice tutorial, but it's not define for which version of drupal is it and that's also nice to have the creation date of the content dispaly on the page.

Pacy
Powered by Pacy