You are here

public function ImagecacheProportionsFormatter::viewElements in Imagecache Proportions 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 ImageFormatter::viewElements

File

src/Plugin/Field/FieldFormatter/ImagecacheProportionsFormatter.php, line 123
Contains \Drupal\imagecache_proportions\Plugin\Field\FieldFormatter.

Class

ImagecacheProportionsFormatter
Plugin implementation of the 'imagecache_proportions' formatter.

Namespace

Drupal\imagecache_proportions\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items) {
  $elements = array();
  $image_link_setting = $this
    ->getSetting('image_link');

  // Check if the formatter involves a link.
  if ($image_link_setting == 'content') {
    $entity = $items
      ->getEntity();
    if (!$entity
      ->isNew()) {

      // @todo Remove when theme_image_formatter() has support for route name.
      $uri['path'] = $entity
        ->getSystemPath();
      $uri['options'] = $entity
        ->urlInfo()
        ->getOptions();
    }
  }
  elseif ($image_link_setting == 'file') {
    $link_file = TRUE;
  }
  foreach ($items as $delta => $item) {
    $image_style = $this
      ->calculateImageStyle($item->entity
      ->getFileUri());
    if ($item->entity) {
      if (isset($link_file)) {
        $image_uri = $item->entity
          ->getFileUri();
        $uri = array(
          'path' => file_create_url($image_uri),
          'options' => array(),
        );
      }

      // Extract field item attributes for the theme function, and unset them
      // from the $item so that the field template does not re-render them.
      $item_attributes = $item->_attributes;
      unset($item->_attributes);
      $elements[$delta] = array(
        '#theme' => 'image_formatter',
        '#item' => $item,
        '#item_attributes' => $item_attributes,
        '#image_style' => $this
          ->getSetting($image_style),
        '#path' => isset($uri) ? $uri : '',
      );
    }
  }
  return $elements;
}