RSS

Newsletter

The CodeKarate Newsletter is the best way for technical ninjas to keep their swords sharpened. Don't worry, we won't flood your inbox and your email address will always remain private.
Back to Top

Using the comment_driven_save function in the Comment Driven module

In the past at BEgINr Media, we used a heavily modified version of Open Atrium (a Drupal Distribution) for project management. One of the additions we added on our version of Open Atrium is the CaseTracker Plus Drupal Features module. This Drupal module uses the Driven API and Comment Driven modules to allow changes to the tasks while posting comments. Recently I was in the process of building a very simplified and rudimentary API to allow communication between our Open Atrium project management site and a simple Chrome extension to allow us to access certain pieces of our project management system without actually having to be on the project management site.

I ran into an issue of trying to programmatically create a Drupal 6 comment driven comment the correct way. Needless to say it was not easy and I struggled trying to figure out exactly how to format specific types of fields. Here is a simple example that will hopefully help you out.

/**
 * These are properties that you will need to determine based on your situation
 * I am just hardcoding a random node that will be different for you
 */
$node = node_load(25)
 
//I use the current logged in user in my example
global $user;
 
//Set the comment properties
$comment['subject'] = 'My Comment Subject';
$comment['comment'] = 'This is the body of my comment';
$comment['uid'] = $user->uid; // the author of the comment
$comment['nid'] = $node->nid; // the node to which this comment belongs
 
//Set the node properties
//set the title of the node
$node['title'] = $node->title;
 
//here is an example of updating an integer field
$node['field_integer_example'][0]['value'] = 5;
 
//here is an example of updating a nodereference field
//my example sets the field to blank, however you could provide an array of nids
$node['field_ct_plus_dependencies']['nid']['nid'] = array();
 
//here is an example of updating a userreference field
//my example sets the current user as the assigned user
$node['field_ct_plus_assignees']['uid']['uid'][$user->uid] = $user->uid;
 
//if using casetracker plus, here is the code to update the duration field
//currently my code just keeps the duration field unchanged
$date1 = date_parse($node->field_ct_plus_duration[0]['value']);
$date2 = date_parse($node->field_ct_plus_duration[0]['value2']);
 
//this may vary depending on how you normally enter dates
//into the duration fields
$node['field_ct_plus_duration'][0]['value']['date'] =
  $date1['month'] . '/' . $date1['day'] . '/' . $date1['year'];
$node['field_ct_plus_duration'][0]['value2']['date'] =
  $date2['month'] . '/' . $date2['day'] . '/' . $date2['year'];
 
//save the comment
$comment_save = comment_driven_save($node, $comment, $errors);

Getting the date field to save correctly proved to be the most difficult piece of the puzzle. It turns out that the date format the node saves is different then the format needed by the duration field in the CaseTracker Plus module (which in hindsight makes a lot of sense). The other tricky part is to ensure you build the correct structure for the "field_ct_plus_duration" array.

Enjoy.

Discussions

1
arhak (not verified)

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <h2> <h3> <blockquote> <img>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <css>, <html>, <php>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <mysql>, <python>, <ruby>. PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.