Drupal Tweaks

Drupal Tweaks

June 16, 2009

It’s time to share some Drupal code because after all, sharing is caring. The following are hurdles that had to be overcome to get the user experience we wanted out of our latest Drupal site, they’re are basic enough but took a good number of hours digging through posts and comments on the Drupal forums to figure out. All code is for Drupal 6.x only. Major modules used: CCKViewsPanels.

I was hesitant at first to go the route of creating a custom module, but highly recommend it now after discovering how simple it is. Of course It also makes it simpler to break Drupal, but let’s not worry about that. Some of these techniques can be applied in either a custom module or to your theme, this is just what worked for us with the timelines we had:

#1 Doing something after custom content creation (redirect)

For a front-end video, photo and blog sharing section (all of which were CCK custom content types), we wanted to redirect users to a page thanking them for their contribution. Module-based code [mycustom.module].

function mycustom_form_alter(&$form, $form_state, $form_id)
{
$form['buttons']['#weight'] = 100;    // Stop bug that causes form buttons to appear in the middle of all forms

// If form is for the creation of one of our custom content types, add a handler to submission
if ($form_id == 'photo_node_form' || $form_id == 'video_node_form')
$form['buttons']['submit']['#submit'][] = 'mycustom_redirect_handler';
}

function mycustom_redirect_handler($form, &$form_state)
{
if ($form_state['nid'])
{
// Load our node from the $form_state
$node = node_load(array('nid' => $form_state['nid']));
// Alter $form_state's redirect to point to a different page, depending on node type
switch($node->type)
{
case 'photo':
$form_state['redirect'] = "photo_add/thanks";
break;
case 'video':
$form_state['redirect'] = "video_add/thanks";
break;
}
}
}

#2 Doing something during custom content creation (run an external script referencing $node)

In this instance, we needed to run our own custom PHP script while Drupal is saving a custom node so that we could pass values entered into that node to our script. Drupal’s Node API allowed us to do this. Module-based code [mycustom.module].

function mycustom_nodeapi(&$node, $op, $teaser = NULL, $page = NULL)
{
switch ($op)
{
case 'insert':
// When inserting a custom content type of 'video'
if ($node->type == 'video')
{
// If the user has checked off the custom field 'field_newsletter'
if ($node->field_newsletter[0]['value'] == '1')
{
$node_title = $node->title;
$node_email = $node->field_email[0]['value'];
include("crm.php");			// Include our script with the crm() function
crm($node_title, $node_email);	// Run it with our node values
}
}
break;
}
}

#3 Doing something during user sign-up (create custom content belonging to $user)

Because we required that users post their photos to a personal gallery of their choice, we wanted to create a default gallery for every user as soon as they register, eliminating the needless step of creating their first gallery if they weren’t interested in categorizing everything. We did this through node_save (drupal_execute may also be used in similar situations but no author will be given to the content). Module-based code [mycustom.module].

function mycustom_user($op, &$edit, &$account, $category=NULL)
{
switch ($op)
{
case 'insert':                            // When performing 'insert' of a user
$old_user = $user;                    // Preserve current $user
$user = user_load($edit['uid']);    // Load newly registered $user

// Create $node
$node = array(
'uid' => $user->uid,            // Make new user the owner
'name' => (isset($user->name) ? $user->name : ''),
'type' => 'photo_album',        // Use custom content type of 'photo_album'
'language' => ''
);

// Prepare $node
if (!function_exists("node_object_prepare"))
include_once(drupal_get_path('module', 'node') . '/node.pages.inc');
$node = (object)$node;
node_object_prepare($node);

// Give $node a title of 'Personal Photos'
$node->title = 'Personal Photos';
$node->body = '';
$node->name = $user->name;
$node->uid = $user->uid;

// Create the $node
node_save($node);

// Revert $user
$user = $old_user;
break;
}
}

#4 Changing layout and values on custom content type and comment forms

We found that the forms that Drupal spits out for creating new nodes of your custom content type are generally way too cluttered to be intuitive to your average user so our goal was to dumb it down and hide a lot of the extra things Drupal adds. There’s a lot of trial and error involved in finding out exactly what value to change (I found print_r_tree($form) effective) but it becomes more obvious after time. Theme-based code [theme.php].

// Tell our theme to run custom functions for displaying the comment, 'photo' and 'video' forms
function mytheme_theme()
{
return array(
'comment_form' => array('arguments' => array('form' => NULL)),
'photo_node_form' => array('arguments' => array('form' => NULL))
);
}

// Alter $form for commenting, then render it
function mytheme_comment_form($form)
{
unset($form['preview']);                                            // Remove Preview button
unset($form['comment_filter']['format']['format']['guidelines']);   // Remove extra instructions
unset($form['comment_filter']['format'][2]);                        // ...
$form['submit']['#value'] = "Post Comment";                         // Change submit button to say 'Post Comment'
return drupal_render($form);                                        // Return rendered form
}

// Alter $form for 'photo', then render it
function mytheme_photo_node_form($form)
{
unset($form['buttons']['preview']);                                 // Remove Preview button
unset($form['body_field']['teaser_include']);                       // Remove teaser/summary view
unset($form['body_field']['format']['format']['guidelines']);       // Remove extra instructions
unset($form['body_field']['format'][2]);                            // ...
$form['body_field']['body']['#rows'] = 5;                           // Change size of Body field
$form['buttons']['submit']['#value'] = "Post Photo";                // Change submit button to say 'Post Photo'
return drupal_render($form);                                        // Return rendered form
}

Leave a Reply

Your email address will not be published. Required fields are marked *