protected function EntityViewBuilder::getSingleFieldDisplay

protected EntityViewBuilder::getSingleFieldDisplay($entity, $field_name, $display_options)

Gets an EntityViewDisplay for rendering an individual field.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity.

string $field_name: The field name.

string|array $display_options: The display options passed to the viewField() method.

Return value

\Drupal\Core\Entity\Display\EntityViewDisplayInterface

File

core/lib/Drupal/Core/Entity/EntityViewBuilder.php, line 430

Class

EntityViewBuilder
Base class for entity view builders.

Namespace

Drupal\Core\Entity

Code

protected function getSingleFieldDisplay($entity, $field_name, $display_options) {
  if (is_string($display_options)) {
    // View mode: use the Display configured for the view mode.
    $view_mode = $display_options;
    $display = EntityViewDisplay::collectRenderDisplay($entity, $view_mode);
    // Hide all fields except the current one.
    foreach (array_keys($entity->getFieldDefinitions()) as $name) {
      if ($name != $field_name) {
        $display->removeComponent($name);
      }
    }
  }
  else {
    // Array of custom display options: use a runtime Display for the
    // '_custom' view mode. Persist the displays created, to reduce the number
    // of objects (displays and formatter plugins) created when rendering a
    // series of fields individually for cases such as views tables.
    $entity_type_id = $entity->getEntityTypeId();
    $bundle = $entity->bundle();
    $key = $entity_type_id . ':' . $bundle . ':' . $field_name . ':' . hash('crc32b', serialize($display_options));
    if (!isset($this->singleFieldDisplays[$key])) {
      $this->singleFieldDisplays[$key] = EntityViewDisplay::create(array(
        'targetEntityType' => $entity_type_id,
        'bundle' => $bundle,
        'status' => TRUE,
      ))->setComponent($field_name, $display_options);
    }
    $display = $this->singleFieldDisplays[$key];
  }

  return $display;
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Entity!EntityViewBuilder.php/function/EntityViewBuilder::getSingleFieldDisplay/8.1.x