Module Development

Performing Additional Operations When Loading Field Data for Drupal 7

This page is archived

We're keeping this page up as a courtesy to folks who may need to refer to old instructions. We don't plan to update this page.

Alternate resources

Sprout Video

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;
      }
    }
  }
}

Additional resources