You are here

function relation_dummy_field_field_formatter_view in Relation 8

Same name and namespace in other branches
  1. 7 relation_dummy_field/relation_dummy_field.module \relation_dummy_field_field_formatter_view()

Implements hook_field_formatter_view().

File

relation_dummy_field/relation_dummy_field.module, line 105
A field storing arbitrary relations between entities.

Code

function relation_dummy_field_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_default':
    case 'relation_otherendpoint':
      foreach ($items as $delta => $item) {
        $links = array();
        $relation = (object) $item;
        foreach (array_filter($relation->endpoints[Language::LANGCODE_NOT_SPECIFIED]) as $endpoint) {
          $storage_handler = \Drupal::entityTypeManager()
            ->getStorage($endpoint['entity_type']);
          $related_entities = $storage_handler
            ->loadMultiple(array(
            $endpoint['entity_id'],
          ));
          $related_entity = reset($related_entities);
          if ($endpoint['entity_type'] == $entity_type && $endpoint['entity_id'] == $entity_id) {
            if ($display['type'] == 'relation_otherendpoint') {
              continue;
            }
            $link = array();
          }
          else {
            $link = entity_uri($endpoint['entity_type'], $related_entity);
            $link['href'] = $link['path'];
          }
          $link['title'] = entity_label($endpoint['entity_type'], $related_entity);
          $links[] = $link;
        }
        $uri = entity_uri('relation', $relation);
        $relation_link = l(t('Relation @relation_id', array(
          '@relation_id' => $relation
            ->id(),
        )), $uri['path'], $uri['options']);
        if ($display['type'] == 'relation_default') {

          // Can't use #heading as it's mercilessly check_plain'd.
          $element[$delta]['relation']['heading']['#markup'] = t('<h4>Part of !link</h4>', array(
            '!link' => $relation_link,
          ));
        }

        // We probably need to make this more customisable.
        if ($display['type'] == 'relation_default' || count($links) > 1) {
          $element[$delta]['relation']['links'] = array(
            '#theme' => 'links',
            '#links' => $links,
          );
        }
        else {
          $element[$delta]['relation']['link'] = array(
            '#theme' => 'link',
            '#path' => $links[0]['href'],
            '#text' => $links[0]['title'],
            '#options' => array(
              'attributes' => array(),
              'html' => FALSE,
            ),
          );
        }
      }
      break;
    case 'relation_natural':
      $sentences = array();
      foreach ($items as $delta => $item) {
        list($id, $revision_id, $bundle) = entity_extract_ids($entity_type, $entity);
        $relation = (object) $item;
        $relation_type = RelationType::load($relation->relation_type);
        $subject = entity_label($entity_type, $entity) . ' ';

        // Subject of the sentence.
        $subject_is_source = $relation->endpoints[Language::LANGCODE_NOT_SPECIFIED]['0']['entity_id'] == $id ? TRUE : FALSE;
        $count = 0;

        // For comma separation of objects.
        $duplicate = FALSE;

        // To make sure duplicates of $entity get included in object list.
        $objects = '';

        // Comma separated list of entities that are the object of the sentence.
        // Gramatical predicate of teh sentence.
        $predicate = $relation_type->directional ? $relation_type->reverse_label : $relation_type->label;
        foreach ($relation->endpoints[Language::LANGCODE_NOT_SPECIFIED] as $endpoint) {

          // Add all entities that aren't this entity to the sentence $objects.
          // Check for duplicates of the $subject first.
          if ($endpoint['entity_type'] == $entity_type && $endpoint['entity_id'] == $id && $duplicate == FALSE) {
            $duplicate = TRUE;

            // Use the forward label as sentence predicate if r_index == 0.
            // (only makes a difference if relation is directional).
            if ($endpoint['r_index'] == 0) {
              $predicate = ' ' . $relation_type->label;
            }
          }
          else {

            // If the relation is directional and the subject isn't the source,
            // we want to list the source without any siblings. If it is
            // directional and the subject is a source, list all targets.
            // If non-directional, list everything as normal.
            if (!$relation_type->directional || $subject_is_source || $endpoint['r_index'] == 0) {
              $storage_handler = \Drupal::entityTypeManager()
                ->getStorage($endpoint['entity_type']);
              $object_entities = $storage_handler
                ->loadMultiple(array(
                $endpoint['entity_id'],
              ));
              $object_entity = reset($object_entities);
              $object_label = entity_label($endpoint['entity_type'], $object_entity);
              $object_uri = entity_uri($endpoint['entity_type'], $object_entity);

              // Just add a space before the first element, comma and space before further ones.
              $objects .= $count ? ', ' : ' ';
              $count += 1;
              $objects .= l($object_label, $object_uri['path']);
            }
          }
        }
        $element[$delta]['relation'] = array(
          '#theme' => 'item_list',
          '#items' => array(
            $subject . $predicate . $objects,
          ),
        );
      }
      break;
  }
  return $element;
}