You are here

function field_extractor_field_formatter_view in Field Extractor 7

Implements hook_field_formatter_view().

File

./field_extractor.module, line 171
Provides a formatter that displays a field from the referenced entities.

Code

function field_extractor_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $settings = field_extractor_settings($field, $instance);
  $extractor_settings = $display['settings'];

  // The formatter hasn't been configured, abort.
  if (empty($extractor_settings['field_name']) || empty($extractor_settings['formatter'])) {
    return array();
  }

  // Extract the entity IDs from the items array and load the entities.
  $entity_ids = array();
  foreach ($items as $item) {
    $entity_ids[] = $item[$settings['column']];
  }

  // No entities found, nothing to do here.
  if (empty($entity_ids)) {
    return array();
  }

  // Retrieve the weight and the label from the view mode settings.
  $instance_display = array(
    'label' => $display['label'],
    'weight' => $display['weight'],
  );

  // Build an alterable EFQ.
  $query = new EntityFieldQuery();
  $query
    ->entityCondition('entity_type', $settings['entity_type'])
    ->entityCondition('entity_id', $entity_ids)
    ->addTag('field_extractor')
    ->addMetaData('field_extractor_entity', $entity);
  $results = $query
    ->execute();
  if (empty($results[$settings['entity_type']])) {
    return array();
  }
  $entities = entity_load($settings['entity_type'], array_keys($results[$settings['entity_type']]));
  return field_extractor_build_element($entities, $settings['entity_type'], $extractor_settings, $instance_display);
}