Select Git revision
EntityReferenceIdFormatter.php
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
EntityReferenceIdFormatter.php 1.26 KiB
<?php
/**
* @file
* Contains \Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceIdFormatter.
*/
namespace Drupal\Core\Field\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Component\Utility\String;
/**
* 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) {
$elements = array();
foreach ($this->getEntitiesToView($items) as $delta => $entity) {
if ($entity->id()) {
$elements[$delta] = array(
'#markup' => String::checkPlain($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(),
),
);
}
}
return $elements;
}
}