You are here

public function ImageRawFormatter::viewElements in Image Raw Formatter 8

Builds a renderable array for a field value.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.

string $langcode: The language that should be used to render the field.

Return value

array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.

Overrides FormatterInterface::viewElements

File

src/Plugin/Field/FieldFormatter/ImageRawFormatter.php, line 159
Contains \Drupal\image_raw_formatter\Plugin\Field\FieldFormatter\ImageRawFormatter.

Class

ImageRawFormatter
Plugin implementation of the 'image_raw_formatter' formatter.

Namespace

Drupal\image_raw_formatter\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = array();
  $files = $this
    ->getEntitiesToView($items, $langcode);
  $image_style_setting = $this
    ->getSetting('image_style');

  // Determine if Image style is required.
  $image_style = NULL;
  if (!empty($image_style_setting)) {
    $image_style = entity_load('image_style', $image_style_setting);
  }

  //foreach ($items as $delta => $item) {
  foreach ($files as $delta => $file) {
    $image_uri = $file
      ->getFileUri();
    if ($image_style) {
      $absolute_path = $this->imageStyleStorage
        ->load($image_style
        ->getName())
        ->buildUrl($image_uri);
    }
    else {

      // Get absolute path for original image.
      $absolute_path = Url::fromUri(file_create_url($image_uri))
        ->getUri();
    }
    $elements[$delta] = array(
      '#markup' => $absolute_path,
    );
  }
  return $elements;
}