Version 1
[yaffs-website] / web / core / lib / Drupal / Core / Field / Plugin / Field / FieldFormatter / EntityReferenceIdFormatter.php
diff --git a/web/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceIdFormatter.php b/web/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceIdFormatter.php
new file mode 100644 (file)
index 0000000..9fa695d
--- /dev/null
@@ -0,0 +1,44 @@
+<?php
+
+namespace Drupal\Core\Field\Plugin\Field\FieldFormatter;
+
+use Drupal\Core\Field\FieldItemListInterface;
+
+/**
+ * Plugin implementation of the 'entity reference ID' formatter.
+ *
+ * @FieldFormatter(
+ *   id = "entity_reference_entity_id",
+ *   label = @Translation("Entity ID"),
+ *   description = @Translation("Display the ID of the referenced entities."),
+ *   field_types = {
+ *     "entity_reference"
+ *   }
+ * )
+ */
+class EntityReferenceIdFormatter extends EntityReferenceFormatterBase {
+
+  /**
+   * {@inheritdoc}
+   */
+  public function viewElements(FieldItemListInterface $items, $langcode) {
+    $elements = [];
+
+    foreach ($this->getEntitiesToView($items, $langcode) as $delta => $entity) {
+      if ($entity->id()) {
+        $elements[$delta] = [
+          '#plain_text' => $entity->id(),
+          // Create a cache tag entry for the referenced entity. In the case
+          // that the referenced entity is deleted, the cache for referring
+          // entities must be cleared.
+          '#cache' => [
+            'tags' => $entity->getCacheTags(),
+          ],
+        ];
+      }
+    }
+
+    return $elements;
+  }
+
+}