Pull merge.
[yaffs-website] / web / core / lib / Drupal / Core / Field / Plugin / Field / FieldFormatter / EntityReferenceIdFormatter.php
1 <?php
2
3 namespace Drupal\Core\Field\Plugin\Field\FieldFormatter;
4
5 use Drupal\Core\Field\FieldItemListInterface;
6
7 /**
8  * Plugin implementation of the 'entity reference ID' formatter.
9  *
10  * @FieldFormatter(
11  *   id = "entity_reference_entity_id",
12  *   label = @Translation("Entity ID"),
13  *   description = @Translation("Display the ID of the referenced entities."),
14  *   field_types = {
15  *     "entity_reference"
16  *   }
17  * )
18  */
19 class EntityReferenceIdFormatter extends EntityReferenceFormatterBase {
20
21   /**
22    * {@inheritdoc}
23    */
24   public function viewElements(FieldItemListInterface $items, $langcode) {
25     $elements = [];
26
27     foreach ($this->getEntitiesToView($items, $langcode) as $delta => $entity) {
28       if ($entity->id()) {
29         $elements[$delta] = [
30           '#plain_text' => $entity->id(),
31           // Create a cache tag entry for the referenced entity. In the case
32           // that the referenced entity is deleted, the cache for referring
33           // entities must be cleared.
34           '#cache' => [
35             'tags' => $entity->getCacheTags(),
36           ],
37         ];
38       }
39     }
40
41     return $elements;
42   }
43
44 }