July 5, 2012 - Comments Off on Digging into Drupal with node_load()

Digging into Drupal with node_load()

So, you've installed Drupal and created a custom content type. Now what?
Well, data goes in and (hopefully) data will come back out. Here are a few ways to see what data is being stored in your nodes and how it is structured. With this information you can manipulate and theme that information the Drupal way.

Every node in Drupal is stored in the backend indexed by its Node ID, or 'nid'. With the nid you can call up the node and make it dance for you. By dissecting the node as it is rendered by Drupal you can get access to the cck fields, referenced nodes, etc.

The quickest way to get node data is from the command line using our friend Drush.
% drush ev 'print_r(node_load(NID))'
This will bootstrap Drupal and evaluate the PHP expression you pass to it. The result will look something like this:

...
[field_gallery_category] => Array
(
  [und] => Array
  (
    [0] => Array
    (
      [tid] => 35
  )
    [1] => Array
    (
      [tid] => 36
    )
  )
)
[field_gallery_main_photo] => Array
(
  [und] => Array
  (
    [0] => Array
    (
      [fid] => 2072
      [alt] =>
      [title] => Antikythera Mechanim
      [width] => 515
      [height] => 438
      [filename] => antikythera-mechanism.png
      [uri] => public://images/antikythera-mechanism.png
      [filemime] => image/jpeg
      [filesize] => 43707
      [status] => 1
    )
  )
)
...

This is just a snippet of the entire node object that is returned. You can see how Taxonomy terms are associated with a node are stored in an array and how information for an attached imagefield file are stored. The parent [und] array is the language designation. An example of how you could use this is to use the image's title text as a div to be a captioned overlay at the bottom of the image. You could likewise, use the taxonomy term ID and get a list of other nodes that are tagged with that term and print out links to them. You could provide a direct download link via your template using the [uri] of the attached image. Understanding how data is presented to the theme layer of Drupal is the key to bending templates to your will.

If you have installed and enabled the Devel module you can evaluate that same php code:

print_r(node_load(...));

right from the webpage.

If you've already created a module and want to print out the node object in a nice collapsable viewer you can add this code:

/**
 * Implements hook_node_load().
 */
function custommodule_node_load($node) {
  if ($node->type == '<NODE_TYPE>') {
    dpm($node);
  }
}

and you will be rewarded with a very convenient display of the node elements.

When you get into creating and modifying custom theme templates for you site and I hope you do, being able to access the node object and its constituent elements will help in fine tuning the output as well as being a good way to see why data is or isn't appearing as expected.

Links:

devel module

node_load

 

Published by: chazcheadle in The Programming Mechanism
Tags:

Comments are closed.