Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x

EntityReferenceIdFormatter.php

Namespace

Drupal\Core\Field\Plugin\Field\FieldFormatter

File

core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceIdFormatter.php

View source
<?php

namespace Drupal\Core\Field\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\Attribute\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * Plugin implementation of the 'entity reference ID' formatter.
 */
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;
    }

}

Classes

Title Deprecated Summary
EntityReferenceIdFormatter Plugin implementation of the 'entity reference ID' formatter.

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal