Newer
Older
Dries Buytaert
committed
<?php
/**
* @file
Alex Pott
committed
* Contains \Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceIdFormatter.
Dries Buytaert
committed
*/
Alex Pott
committed
namespace Drupal\Core\Field\Plugin\Field\FieldFormatter;
Dries Buytaert
committed
Alex Pott
committed
use Drupal\Core\Field\FieldItemListInterface;
Dries Buytaert
committed
/**
* Plugin implementation of the 'entity reference ID' formatter.
*
Alex Pott
committed
* @FieldFormatter(
Dries Buytaert
committed
* 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 {
/**
Alex Pott
committed
* {@inheritdoc}
Dries Buytaert
committed
*/
Alex Pott
committed
public function viewElements(FieldItemListInterface $items, $langcode) {
Dries Buytaert
committed
$elements = array();
Alex Pott
committed
foreach ($this->getEntitiesToView($items, $langcode) as $delta => $entity) {
if ($entity->id()) {
$elements[$delta] = array(
'#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' => array(
'tags' => $entity->getCacheTags(),
),
);
Dries Buytaert
committed
}
}
return $elements;
}
}