You are here

function relation_add_field_formatter_view in Relation add 7

Implements hook_field_formatter_view().

File

./relation_add.module, line 1222
Relation Add module file.

Code

function relation_add_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  list($entity_id) = entity_extract_ids($entity_type, $entity);
  switch ($display['type']) {
    case 'relation_add_endpoints_and_fields':
      foreach ($items as $delta => $item) {
        $links = array();
        $relation = (object) $item;
        if (count($relation->endpoints[LANGUAGE_NONE]) > 1) {
          foreach (array_filter($relation->endpoints[LANGUAGE_NONE]) as $endpoint) {
            $related_entities = entity_load($endpoint['entity_type'], array(
              $endpoint['entity_id'],
            ));
            $related_entity = reset($related_entities);
            if (!($endpoint['entity_type'] == $entity_type && $endpoint['entity_id'] == $entity_id)) {
              $link = entity_uri($endpoint['entity_type'], $related_entity);
              $link['href'] = $link['path'];
              $link['title'] = entity_label($endpoint['entity_type'], $related_entity);
              $links[] = $link;
            }
          }
          $endpoint_title = '';
          switch ($instance['widget']['settings']['relation_endpoint_label']) {
            case 'endpoint':
              $relation_instance = field_info_instance('relation', 'endpoints', $relation->relation_type);

              // @codingStandardsIgnoreStart
              $endpoint_title = t(check_plain($relation_instance['label']));

              // @codingStandardsIgnoreEnd
              break;
            case 'custom':

              // @codingStandardsIgnoreStart
              $endpoint_title = t($instance['widget']['settings']['relation_endpoint_custom_label']);

              // @codingStandardsIgnoreEnd
              break;
          }
          $endpoint_title .= $instance['widget']['settings']['relation_endpoint_label_delta'] ? ' ' . ($delta + 1) : '';

          // @codingStandardsIgnoreStart
          $element[$delta]['relation']['heading']['#markup'] = t(check_plain($endpoint_title));

          // @codingStandardsIgnoreEnd
          $element[$delta]['relation']['links'] = array(
            '#theme' => 'links',
            '#links' => $links,
          );
        }
        $relation_view = relation_view($relation);
        $relation_instances = field_info_instances('relation', $relation->relation_type);
        foreach (array_keys($relation_instances) as $relation_field_name) {
          if ($relation_field_name !== 'endpoints') {
            if (isset($relation_view[$relation_field_name])) {
              $element[$delta]['relation']['fields'][] = $relation_view[$relation_field_name];
            }
          }
        }
      }
      break;
  }
  return $element;
}