You are here

public function ImagecacheExternalImage::viewElements in Imagecache External 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/ImagecacheExternalImage.php, line 144

Class

ImagecacheExternalImage
Plugin implementation of the 'imagecache_external_image' formatter.

Namespace

Drupal\imagecache_external\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];
  $field = $items
    ->getFieldDefinition();
  $field_settings = $this
    ->getFieldSettings();
  $url = NULL;
  $image_link_setting = $this
    ->getSetting('imagecache_external_link');

  // Check if the formatter involves a link.
  if ($image_link_setting == 'content') {
    $entity = $items
      ->getEntity();
    if (!$entity
      ->isNew()) {
      $url = $entity
        ->toUrl();
    }
  }
  elseif ($image_link_setting == 'file') {
    $link_file = TRUE;
  }

  // Check if the field provides a title.
  if ($field
    ->getType() == 'link') {
    if ($field_settings['title'] != DRUPAL_DISABLED) {
      $field_title = TRUE;
    }
  }
  foreach ($items as $delta => $item) {

    // Get field value.
    $values = $item
      ->toArray();
    $image_alt = '';
    if ($field
      ->getType() == 'link') {
      $image_path = imagecache_external_generate_path($values['uri']);

      // If present, use the Link field title to provide the alt text.
      if (isset($field_title)) {

        // The link field appends the url as title when the title is empty.
        // We don't want the url in the alt tag, so let's check this.
        if ($values['title'] != $values['uri']) {
          $image_alt = isset($field_title) ? $values['title'] : '';
        }
      }
    }
    else {
      $image_path = imagecache_external_generate_path($values['value']);
    }

    // Skip rendering this item if there is no image_path.
    if (!$image_path) {
      continue;
    }
    if (isset($link_file)) {
      $url = Url::fromUri(file_create_url($image_path));
    }
    $image = $this->imageFactory
      ->get($image_path);
    $style_settings = $this
      ->getSetting('imagecache_external_style');
    $image_build_base = [
      '#width' => $image
        ->getWidth(),
      '#height' => $image
        ->getHeight(),
      '#uri' => $image_path,
      '#alt' => $image_alt,
      '#title' => '',
    ];
    if (empty($style_settings)) {
      $image_build = [
        '#theme' => 'image',
      ] + $image_build_base;
    }
    else {
      $image_build = [
        '#theme' => 'image_style',
        '#style_name' => $style_settings,
      ] + $image_build_base;
    }
    if ($url) {
      $rendered_image = render($image_build);
      $elements[$delta] = Link::fromTextAndUrl($rendered_image, $url)
        ->toRenderable();
    }
    else {
      $elements[$delta] = $image_build;
    }
  }
  return $elements;
}