You are here

LinkImageFieldFormatter.php in Link Image Field 8

File

lib/Drupal/linkimagefield/Plugin/Field/FieldFormatter/LinkImageFieldFormatter.php
View source
<?php

namespace Drupal\linkimagefield\Plugin\Field\FieldFormatter;

use Drupal\image\Plugin\Field\FieldFormatter\ImageFormatter;
use Drupal\Core\Field\FieldItemListInterface;

/**
 * Plugin implementation of the 'image' formatter.
 *
 * @FieldFormatter(
 *   id = "linkimagefield",
 *   label = @Translation("Link Image"),
 *   field_types = {
 *     "linkimagefield"
 *   }
 * )
 */
class LinkImageFieldFormatter extends ImageFormatter {

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, array &$form_state) {
    $element = parent::settingsForm($form, $form_state);
    unset($element['image_link']);
    return $element;
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

}

Classes

Namesort descending Description
LinkImageFieldFormatter Plugin implementation of the 'image' formatter.