You are here

protected function WebformEntityReferenceFormatterBase::getEntitiesToView in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Plugin/Field/FieldFormatter/WebformEntityReferenceFormatterBase.php \Drupal\webform\Plugin\Field\FieldFormatter\WebformEntityReferenceFormatterBase::getEntitiesToView()

Returns the referenced entities for display.

The method takes care of:

  • checking entity access,
  • placing the entities in the language expected for display.

It is thus strongly recommended that formatters use it in their implementation of viewElements($items) rather than dealing with $items directly.

For each entity, the EntityReferenceItem by which the entity is referenced is available in $entity->_referringItem. This is useful for field types that store additional values next to the reference itself.

Parameters

\Drupal\Core\Field\EntityReferenceFieldItemListInterface $items: The item list.

string $langcode: The language code of the referenced entities to display.

Return value

\Drupal\Core\Entity\EntityInterface[] The array of referenced entities to display, keyed by delta.

Overrides EntityReferenceFormatterBase::getEntitiesToView

See also

::prepareView()

3 calls to WebformEntityReferenceFormatterBase::getEntitiesToView()
WebformEntityReferenceEntityFormatter::viewElements in src/Plugin/Field/FieldFormatter/WebformEntityReferenceEntityFormatter.php
Builds a renderable array for a field value.
WebformEntityReferenceLinkFormatter::viewElements in src/Plugin/Field/FieldFormatter/WebformEntityReferenceLinkFormatter.php
Builds a renderable array for a field value.
WebformEntityReferenceUrlFormatter::viewElements in src/Plugin/Field/FieldFormatter/WebformEntityReferenceUrlFormatter.php
Builds a renderable array for a field value.

File

src/Plugin/Field/FieldFormatter/WebformEntityReferenceFormatterBase.php, line 60

Class

WebformEntityReferenceFormatterBase
Base class for 'Webform Entity Reference formatter' plugin implementations.

Namespace

Drupal\webform\Plugin\Field\FieldFormatter

Code

protected function getEntitiesToView(EntityReferenceFieldItemListInterface $items, $langcode) {

  /** @var \Drupal\webform\WebformInterface[] $entities */
  $entities = parent::getEntitiesToView($items, $langcode);
  foreach ($entities as $entity) {

    /** @var \Drupal\webform\Plugin\Field\FieldType\WebformEntityReferenceItem $item */
    $item = $entity->_referringItem;

    // Only override an open webform.
    if ($entity
      ->isOpen()) {
      if (isset($item->open)) {
        $entity
          ->set('open', $item->open);
      }
      if (isset($item->close)) {
        $entity
          ->set('close', $item->close);
      }
      if (isset($item->status)) {
        $entity
          ->setStatus($item->status);
      }

      // Directly call set override to prevent the altered webform from being
      // saved.
      if (isset($item->open) || isset($item->close) || isset($item->status)) {
        $entity
          ->setOverride();
      }
    }
  }
  return $entities;
}