EntityViewBuilder::viewFieldItem

public EntityViewBuilder::viewFieldItem(FieldItemInterface $item, $display = array())

Builds a renderable array for a single field item.

Parameters

\Drupal\Core\Field\FieldItemInterface $item: FieldItem to be displayed.

string|array $display_options: Can be either the name of a view mode, or an array of display settings. See EntityViewBuilderInterface::viewField() for more information.

Return value

array A renderable array for the field item.

Overrides EntityViewBuilderInterface::viewFieldItem

See also

\Drupal\Core\Entity\EntityViewBuilderInterface::viewField()

File

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

Class

EntityViewBuilder
Base class for entity view builders.

Namespace

Drupal\Core\Entity

Code

public function viewFieldItem(FieldItemInterface $item, $display = array()) {
  $entity = $item->getEntity();
  $field_name = $item->getFieldDefinition()->getName();

  // Clone the entity since we are going to modify field values.
  $clone = clone $entity;

  // Push the item as the single value for the field, and defer to viewField()
  // to build the render array for the whole list.
  $clone->{$field_name}->setValue(array($item->getValue()));
  $elements = $this->viewField($clone->{$field_name}, $display);

  // Extract the part of the render array we need.
  $output = isset($elements[0]) ? $elements[0] : array();
  if (isset($elements['#access'])) {
    $output['#access'] = $elements['#access'];
  }

  return $output;
}
doc_Drupal
2016-10-29 09:08:47
Comments
Leave a Comment

Please login to continue.