Check your version

This video covers a topic in Drupal 7 which may or may not be the version you're using. We're keeping this tutorial online as a courtesy to users of Drupal 7, but we consider it archived.

Alternate resources: 

Performing Additional Operations When Loading Field Data

Video loading...

Join Drupalize.Me to watch this video

Join today and gain instant access to our entire video library.

Log in Sign up
  • 0:01
    Performing Additional Operations When Loading Field Data
  • 0:05
    with Joe Shindelar
  • 0:06
    Sometimes, we need to make use of the collected
  • 0:09
    and stored field data in order to calculate additional values that
  • 0:12
    can be used when displaying a field.
  • 0:15
    The Amazon ASIN field, for example, can query the Amazon API
  • 0:19
    and get additional information about a product,
  • 0:21
    like a thumbnail of the book the ASIN number represents, to display
  • 0:25
    alongside the value, the idea being store the bare minimum information
  • 0:30
    necessary to dynamically calculate the information you really need.

Performing Additional Operations When Loading Field Data

Loading...

Sometimes we need to make use of the collected and stored field data in order to calculate additional values that can be used when displaying a field. The Amazon ASIN field for example can query the Amazon API and get additional information about a product like a thumbnail to display alongside the ASIN value. Using hook_field_load() we can perform additional operations on the stored field values at the time the Field API loads, or requests, the value of our field and present that calculated data long with the stored data.

Should the data be loaded during "view" or "load" operations? In my mind that depends on what it's needed for. A good question to ask might be, "if someone was accessing the content of this field as JSON would they want this data included?". If the answer is yes, you probably want to use hook_field_load() to perform addition load operations.

When dealing with implementations of hook_field_load() the most interesting paramater is probably the $items array. An multi-dimentional array that contains a record for each value value for this particular field for the entity being viewed. Because $items can contain one or more values you'll need to loop over the values within $items and update them accordingly.

Implementations of hook_field_load() don't need to return any value. Instead they should update the $items array which is passed in by reference.

In this example we'll be querying the Google Search API, which returns JSON data. The search API is accessible at URLs like the following: https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=663399. The most important part being the &q=663399, which we'll replace with our specific search term.

Values that are added to the $items array will be available to implementations of hook_field_formatter_view() in the $items parameter passed to those functions. From there, you can make use of any added data when displaying the field for end users. Because the data is added during the load operation for the entity that the field is attached to it's also available anytime you're making use of the $entity object.

Example:


/**
 * Implements hook_field_load().
 */
function rgb_field_load($entity_type, $entities, $field, $instances, $langcode, &$items, $age) {
  dsm('rgb_field_load');
  foreach ($items as $entity_id => $field_values) {
    foreach ($field_values as $delta => $value) {
      $url = 'https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=' . $value['rgb'];
      $response = drupal_http_request($url);
      if ($response->code == 200) {
        $data = drupal_json_decode($response->data);
        $links = array();
        foreach ($data['responseData']['results'] as $result) {
          $links[] = l($result['titleNoFormatting'], $result['url']);
        }

        $items[$entity_id][$delta]['google_links'] = $links;
      }
    }
  }
}
Downloads: 
Log in or sign up to download companion files.
Additional resources: