You are here

function field_extract_values in Field Extract Values 7

Fetch values of an entity's field

Parameters

$entity_type: The type of the entity from which the values are being extracted.

$entity: The entity the field is part of, from which to extract the values.

$field_name: The field_name of the field to extract the values from.

$options: An optional array of options that control what and how the values are extracted

  • delta if included returns only the specified value from a multi-value field otherwise all values are returned (in the form specified later).
  • key returns a specific array element of the item, rather than the default value for the type. So for a 'body' field you could specify 'safe_summary'. When there is a 'formatter' has no effect.
  • formatter Forces a formatted version of the field to be returned, unlike field_view_field() onely the formatted value is returned, without a label attached.
  • langcode the language to use if not using the default for the entity or site.
  • [other] any other field type specific value. For example the date field type can have a 'format' field.

If the value being fetched is an entity the whole entity will be fetched by default.

1 call to field_extract_values()
field_extract_value in ./field_extract.module
Fetch a single field value from an entity

File

./field_extract.module, line 58
Field extract - Extension of the Field API to allow proper extraction of field values from entities

Code

function field_extract_values($entity_type, $entity, $field_name, array $options = array()) {
  if (!is_object($entity)) {
    return FALSE;
  }
  if (!property_exists($entity, $field_name)) {
    return FALSE;
  }
  if (empty($entity->{$field_name})) {
    return FALSE;
  }

  // Fetch the field type information for this field
  $field_info = field_info_field($field_name);
  $field_type_info = field_info_field_types($field_info['type']);
  if (!isset($field_type_info['extractor'])) {

    // if there is no extractor information just exit
    return FALSE;
  }
  list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  $field_instance = field_read_instance($entity_type, $field_name, $bundle);
  $extractor = $field_type_info['extractor'];

  // Include the extractor's default options
  $options += $extractor['options'];

  // Include the generic default options
  $options += array(
    'delta' => NULL,
    'key' => NULL,
    'langcode' => NULL,
    'formatter' => NULL,
    'entity_type' => NULL,
    'format' => 'default',
    // The formatter and some extractors need this stuff
    'parent_entity_type' => $entity_type,
    'parent_entity' => $entity,
    'parent_entity_info' => entity_get_info($entity_type),
    'parent_bundle' => $bundle,
    'field_name' => $field_name,
    'field_type_info' => $field_type_info,
    'field_info' => $field_info,
    'instance_info' => $field_instance,
  );

  // Do we want to use a formatter rather than an extractor?
  if (is_string($options['formatter'])) {
    return field_extract_formatted($options);
  }

  // Get entity info
  $entity_info = $options['entity_type'] ? entity_get_info($options['entity_type']) : FALSE;

  // Do we need to load the file containing the extractor?
  if (isset($extractor['filepath'])) {
    include_once $extractor['filepath'];
  }

  // Check the function for the extractor actually exists
  // Or use the standard hook, or the default hook.
  $function = isset($extractor['callback']) ? $extractor['callback'] : NULL;
  if (!$function) {
    $function = "{$field_info['module']}_field_extract";
    if (!function_exists($function)) {
      $function = 'field_extract_field_extract';
    }
  }

  // Fetch the items out of the entity
  $items = field_get_items($entity_type, $entity, $field_name, $options['langcode']);
  if (empty($items)) {
    $items = array();
  }

  // Are we doing just one value or all of them?
  if (is_numeric($options['delta'])) {

    // If delta is set make sure it exists before processing it
    if (isset($items[$options['delta']])) {

      // if this has an entity_type, fetch the entity
      if (!isset($options['key']) && $options['entity_type']) {
        $id = $items[$options['delta']][$entity_info['entity keys']['id']];
        $fetched_entity = array_shift(entity_load($options['entity_type'], array(
          $id,
        )));
        foreach ($items[$options['delta']] as $key => $value) {
          if (!property_exists($fetched_entity, $key)) {
            $fetched_entity->{$key} = $value;
          }
        }
        return $fetched_entity;
      }

      // ...otherwise extract it, returning the result
      return $function($items[$options['delta']], $options);
    }
    else {
      return NULL;
    }
  }
  else {

    // if this has an entity_type, fetch all the entities
    if (!isset($options['key']) && $options['entity_type']) {
      $ids = $map = array();
      $id_key = $entity_info['entity keys']['id'];
      foreach ($items as $delta => $item) {
        $ids[] = $item[$id_key];
        $map[$item[$id_key]] = $delta;
      }
      $fetched_entities = entity_load($options['entity_type'], $ids);
      foreach ($fetched_entities as $id => $fetched_entity) {
        foreach ($items[$map[$id]] as $key => $value) {
          if (!property_exists($fetched_entity, $key)) {
            $fetched_entity->{$key} = $value;
          }
        }
      }
      return $fetched_entities;
    }

    // ...otherwise extract each value in turn
    $values = array();
    foreach ($items as $delta => $item) {
      $values[$delta] = $function($item, $options);
    }

    // and return them all.
    return $values;
  }
  return FALSE;
}