You are here

public function LinkImageFieldFormatter::viewElements in Link Image Field 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

lib/Drupal/linkimagefield/Plugin/Field/FieldFormatter/LinkImageFieldFormatter.php, line 33

Class

LinkImageFieldFormatter
Plugin implementation of the 'image' formatter.

Namespace

Drupal\linkimagefield\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items) {
  $elements = array();
  $settings = $this
    ->getSettings();
  foreach ($items as $delta => $item) {

    // 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' => 'linkimage_formatter',
      '#item' => $item,
      '#image_style' => $settings['image_style'],
      '#item_attributes' => $item_attributes,
    );
  }
  return $elements;
}