You are here

public function UcProductImageFormatter::viewElements in Ubercart 8.4

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

uc_product/src/Plugin/Field/FieldFormatter/UcProductImageFormatter.php, line 101

Class

UcProductImageFormatter
Plugin implementation of the 'uc_product_image' formatter.

Namespace

Drupal\uc_product\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];
  $image_link_setting = $this
    ->getSetting('image_link');

  // Check if the formatter involves a link.
  if ($image_link_setting == 'content') {
    $uri = $items
      ->getEntity()
      ->uri();
  }
  elseif ($image_link_setting == 'file') {
    $link_file = TRUE;
  }
  $first_style = $this
    ->getSetting('first_image_style');
  $other_style = $this
    ->getSetting('other_image_style');
  foreach ($items as $delta => $item) {
    if ($item->entity) {
      if (isset($link_file)) {
        $image_uri = $item->entity
          ->getFileUri();
        $uri = [
          'path' => file_create_url($image_uri),
          'options' => [],
        ];
      }
      $elements[$delta] = [
        '#theme' => 'image_formatter',
        '#item' => $item
          ->getValue(TRUE),
        '#image_style' => $delta == 0 ? $first_style : $other_style,
        '#path' => isset($uri) ? $uri : '',
      ];
    }
  }
  return $elements;
}