You are here

public function HoverPreview::viewElements in Hover Preview for ImageCache 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/HoverPreview.php, line 136

Class

HoverPreview
Plugin implementation of the 'hover_preview' formatter.

Namespace

Drupal\hover_preview\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $element = array();
  $settings = $this
    ->getSettings();
  $this->entity = $items
    ->getEntity();

  // Each hover preview item is created with an image element.
  foreach ($items as $delta => $item) {
    $target_id = $item
      ->get('target_id')
      ->getValue();
    $file = File::load($target_id);
    $file_uri = $file
      ->getFileUri();
    $element[$delta]['#theme'] = 'image';

    // The title tag.
    $title = $item
      ->get('title')
      ->getValue();
    if (!empty($title)) {
      $element[$delta]['#title'] = $title;
    }

    // The alt tag.
    $alt = $item
      ->get('alt')
      ->getValue();
    if (!empty($alt)) {
      $element[$delta]['#alt'] = $alt;
    }

    // The image path is contructed based on the image style.
    if (isset($settings['image_style']) && !empty($settings['image_style'])) {

      #$element[$delta]['#path'] = image_style_url($settings['image_style'], $$file_uri);
      $element[$delta]['#uri'] = ImageStyle::load($settings['image_style'])
        ->buildUrl($file_uri);
    }
    else {

      // If no image style is provided, we use the original image.
      $element[$delta]['#uri'] = $file_uri;
    }

    // The hover preview image style.
    if (isset($settings['hover_preview_style']) && !empty($settings['hover_preview_style'])) {
      $hover_uri = ImageStyle::load($settings['hover_preview_style'])
        ->buildUrl($file_uri);
      $element[$delta]['#attributes']['data-hover-preview'] = file_create_url($hover_uri);
    }
    else {

      // If no hover preview style is provided, we use the original image.
      $element[$delta]['#attributes']['data-hover-preview'] = file_create_url($file_uri);
    }

    // Provide the hover-preview class and the action (default is imgpreview).
    $action = isset($settings['hover_preview_action']) && !empty($settings['hover_preview_action']) ? $settings['hover_preview_action'] : 'imgpreview';
    $element[$delta]['#attributes']['class'][] = 'hover-preview-' . $action;
    $element[$delta]['#attributes']['class'][] = 'hover-preview';

    // Special use cases for certain hover preview actions.
    switch ($action) {

      // Image Preview requires the imgPreview library.
      case 'imgpreview':
        $element[$delta]['#attached']['library'][] = 'hover_preview/hover_preview.imgPreview';
        break;

      // Image Preview requires the imgPreview library.
      case 'replace':
        $element[$delta]['#attached']['library'][] = 'hover_preview/hover_preview.imghover';
        break;
    }

    // Check if the formatter involves a link.
    switch ($settings['image_link']) {
      case 'content':

        // Link directly to the entity content.
        $url = $this->entity
          ->toUrl();
        if ($url
          ->isExternal()) {
          $uri = $url
            ->getUri();
        }
        else {
          $uri = '/' . $url
            ->getInternalPath();
        }
        $element[$delta]['#prefix'] = '<a href="' . $uri . '">';
        $element[$delta]['#suffix'] = '</a>';
        break;
      case 'file':

        // Link directly to the file.
        $element[$delta]['#prefix'] = '<a href="' . file_create_url($file_uri) . '">';
        $element[$delta]['#suffix'] = '</a>';
        break;
    }

    // The Hover Preview module requires the JavaScript to load the behaviors.
    $element[$delta]['#attached']['library'][] = 'hover_preview/hover_preview.hover_preview';
  }
  return $element;
}