EntityReferenceFormatterBase.php 3.35 KB
Newer Older
1
2
3
4
5
6
7
8
9
<?php

/**
 * @file
 * Contains \Drupal\entity_reference\Plugin\field\formatter\EntityReferenceFormatterBase.
 */

namespace Drupal\entity_reference\Plugin\field\formatter;

10
use Drupal\field\Annotation\FieldFormatter;
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
use Drupal\Core\Annotation\Translation;
use Drupal\Core\Entity\EntityInterface;
use Drupal\field\Plugin\Type\Formatter\FormatterBase;

/**
 * Parent plugin for entity reference formatters.
 */
abstract class EntityReferenceFormatterBase extends FormatterBase {

  /**
   * Overrides \Drupal\field\Plugin\Type\Formatter\FormatterBase::prepareView().
   *
   * Mark the accessible IDs a user can see. We do not unset unaccessible
   * values, as other may want to act on those values, even if they can
   * not be accessed.
   */
  public function prepareView(array $entities, $langcode, array &$items) {
    $target_ids = array();
    $revision_ids = array();

    // Collect every possible entity attached to any of the entities.
    foreach ($entities as $id => $entity) {
      foreach ($items[$id] as $delta => $item) {
        if (!empty($item['revision_id'])) {
          $revision_ids[] = $item['revision_id'];
        }
        elseif (!empty($item['target_id'])) {
          $target_ids[] = $item['target_id'];
        }
      }
    }

43
    $target_type = $this->getFieldSetting('target_type');
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

    $target_entities = array();

    if ($target_ids) {
      $target_entities = entity_load_multiple($target_type, $target_ids);
    }

    if ($revision_ids) {
      // We need to load the revisions one by-one.
      foreach ($revision_ids as $revision_id) {
        $entity = entity_revision_load($target_type, $revision_id);
        // Use the revision ID in the key.
        $identifier = $entity->id() . ':' . $revision_id;
        $target_entities[$identifier] = $entity;
      }
    }

    // Iterate through the fieldable entities again to attach the loaded data.
    foreach ($entities as $id => $entity) {
      $rekey = FALSE;
      foreach ($items[$id] as $delta => $item) {
        // If we have a revision ID, the key uses it as well.
        $identifier = !empty($item['revision_id']) ? $item['target_id'] . ':' . $item['revision_id'] : $item['target_id'];
67
        if ($item['target_id'] !== 0) {
68
69
70
71
72
73
74
75
76
77
          if (!isset($target_entities[$identifier])) {
            // The entity no longer exists, so remove the key.
            $rekey = TRUE;
            unset($items[$id][$delta]);
            continue;
          }

          $entity = $target_entities[$identifier];
          $items[$id][$delta]['entity'] = $entity;

78
          if (!$entity->access('view')) {
79
80
81
82
            continue;
          }
        }
        else {
83
          // This is an "auto_create" item, just leave the entity in place.
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
        }

        // Mark item as accessible.
        $items[$id][$delta]['access'] = TRUE;
      }

      if ($rekey) {
        // Rekey the items array.
        $items[$id] = array_values($items[$id]);
      }
    }
  }

  /**
   * Overrides \Drupal\field\Plugin\Type\Formatter\FormatterBase::viewElements().
   *
   * @see \Drupal\entity_reference\Plugin\field\formatter\EntityReferenceFormatterBase::viewElements().
   */
  public function viewElements(EntityInterface $entity, $langcode, array $items) {
    // Remove un-accessible items.
    foreach ($items as $delta => $item) {
      if (empty($item['access'])) {
        unset($items[$delta]);
      }
    }
    return array();
  }
}