Search form

Drupal Development

Notes on Drupal development using Devel and some of its children.

Install and Enable the Devel Module

Devel can be downloaded from Drupal.org or by using the drush pm-download devel/drush pm-enable devel commands.

Drupal Development with Devel

Among other things, devel allows you to see inside the Drupal objects and arrays containing the different properties and values that you can manipulate programmatically. To try it, create a page on your Drupal site and, using the PHP evaluator, load the current node object:

$nid = arg(1);
$node = node_load($nid);

dpm($node);
/* dpm() prints a variable to the messages area on a Drupal page using the Krumo PHP debugging tool. In this case you can also just click a node's Devel tab to see its properties. */

dprint_r($node);
/* dprint_r() gives you a readable version of PHP's print_r() function. In this case it shows you the node's contents as a nicely printed array. */

Devel output:
Drupal Devel output

dprint_r() output:
Drupal Devel dprint_r output

'changed' is one of the properties listed for the node, so you could print its "last updated" date as "September 02, 2013":

print date('F d, Y', $node->changed);
(Use the date() function to convert the Unix timestamp to a human-readable format.) If you wanted to print a node property in a block, you could do something like this:
if (is_object(node_load(arg(1)))) {
    $node = node_load(arg(1));
    /* On non-objects, like the user/login page, calling node_load() will give you the 'Trying to get
       property of non-object in eval()' error. */
    print date('F d, Y', $node->changed);
}
Any modules or extensions that provide additional properties, like the Internationalization module, can be examined with devel and manipulated as properties of node objects. For example, you could build your own language switcher from the available translations of each node:
$node = node_load(arg(1));
$translations = translation_node_get_translations($node->tnid);

//print the available translations as an array
//dprint_r(translation_node_get_translations($node->tnid));

foreach ($translations as $nodes) {
    print '
  • ' . $nodes->language . '
  • '; }
    Use of dpm() and dprint_r() also extends to the Drupal PHP files on your server's filesystem, such as template files and custom modules. For example, if you wanted to create a custom user profile page, create the user-profile.tpl.php file in /sites/yoursite/themes/yourtheme/templates/. You can then pick and choose which of the user object properties to show:
    $account = menu_get_object('user');
    //dpm($account);
    
    $name = $account->name;
    $email = $account->mail;
    print '

    Account Information for ' . $name . '

    '; print '

    ' . $email . '

    '; //dpm($user_profile); //print any custom fields print render($user_profile['field_address']); print render($user_profile['field_age']);
    Using Drupal's hooks, you can create your own code that ties into core Drupal from settings.php, template.php, a custom module, or any other file that Drupal can pick up. Here's a brief example using hook_preprocess_node(), which makes variables available to the node.tpl.php template. Once again, you can use the devel functions to see your data.
    /**
     * Override or insert variables into the node templates.
     *
     * @param $vars
     *   An array of variables to pass to the theme template.
     * @param $hook
     *   The name of the template being rendered ("node" in this case.)
     */
    
    function mymodule_preprocess_node(&$vars, $hook) {
       $vars['disclaimer'] = t('This sites takes no responsibility for user contributed content.');
       //dpm($vars);
    }
    
    Drupal 7 Module Development is the best book I've found so far.

    Categories: