You are here

protected function ImageDeltaFormatter::getEntitiesToView in Image Delta Formatter 8

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 ImageFormatterBase::getEntitiesToView

See also

::prepareView()

File

src/Plugin/Field/FieldFormatter/ImageDeltaFormatter.php, line 79

Class

ImageDeltaFormatter
Plugin implementation of the 'image_delta_formatter' formatter.

Namespace

Drupal\image_delta_formatter\Plugin\Field\FieldFormatter

Code

protected function getEntitiesToView(EntityReferenceFieldItemListInterface $items, $langcode) {
  $files = parent::getEntitiesToView($items, $langcode);

  // Prepare an array of selected deltas from the entered string.
  if (mb_strpos($this
    ->getSetting('deltas'), ',')) {
    $deltas = explode(',', $this
      ->getSetting('deltas'));
    $deltas = array_map('trim', $deltas);
  }
  else {
    $delta = trim($this
      ->getSetting('deltas'));
    $deltas = [
      $delta,
    ];
  }
  foreach (array_keys($files) as $delta) {
    if (!in_array($delta, $deltas)) {
      unset($files[$delta]);
    }
  }

  // Reverse the items if needed.
  if ($this
    ->getSetting('deltas_reversed')) {
    $files = array_reverse($files);
  }
  return $files;
}