You are here

function relation_endpoint_field_formatter_view in Relation 7

Implements hook_field_formatter_view().

File

./relation_endpoint.module, line 259
Relation endpoints field.

Code

function relation_endpoint_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  switch ($display['type']) {
    case 'relation_endpoint':
      $build[] = _relation_endpoint_field_create_html_table($items);
      break;
    case 'relation_endpoint_full':
      $list_items = array();
      $endpoint_entity_type = '';
      $multiple = TRUE;
      foreach ($items as $delta => $endpoint) {
        if (!$endpoint_entity_type) {
          $endpoint_entity_type = $endpoint['entity_type'];
        }
        if ($endpoint_entity_type == $endpoint['entity_type']) {
          $entity_ids[] = $endpoint['entity_id'];
        }
        else {
          $multiple = FALSE;
          break;
        }
      }
      $view_mode = isset($display['settings']['view_modes'][$endpoint_entity_type]) ? $display['settings']['view_modes'][$endpoint_entity_type] : 'full';
      if ($multiple) {
        $entities = entity_load($endpoint_entity_type, $entity_ids);
        if (function_exists('entity_view')) {
          return array(
            entity_view($endpoint_entity_type, $entities, $view_mode),
          );
        }
        $function = $endpoint_entity_type . '_view_multiple';
        if (function_exists($function)) {
          return array(
            $function($entities, $view_mode),
          );
        }
      }
      $build = array();
      foreach ($items as $delta => $endpoint) {
        if ($multiple) {
          $entity = $entities[$endpoint['entity_id']];
        }
        else {
          $entities = entity_load($endpoint['entity_type'], array(
            $endpoint['entity_id'],
          ));
          $entity = reset($entities);
        }
        if (function_exists('entity_view')) {
          $build[$delta] = entity_view($endpoint['entity_type'], array(
            $entity,
          ), $view_mode);
        }
        else {
          $function = $endpoint['entity_type'] . '_view';
          $build[$delta] = $function($entity, $view_mode);
        }
      }
  }
  return $build;
}