You are here

public function TextimageImageFieldFormatter::viewElements in Textimage 8.3

Same name and namespace in other branches
  1. 8.4 src/Plugin/Field/FieldFormatter/TextimageImageFieldFormatter.php \Drupal\textimage\Plugin\Field\FieldFormatter\TextimageImageFieldFormatter::viewElements()

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/TextimageImageFieldFormatter.php, line 207

Class

TextimageImageFieldFormatter
Plugin implementation of the Textimage image field formatter.

Namespace

Drupal\textimage\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];
  $files = $this
    ->getEntitiesToView($items, $langcode);

  // Early opt-out if the field is empty.
  if (empty($files)) {
    return $elements;
  }

  // Get image style.
  $image_style = $this->imageStyleStorage
    ->load($this
    ->getSetting('image_style'));

  // Collect bubbleable metadata.
  $bubbleable_metadata = new BubbleableMetadata();

  // Provide token data for the displayed entity.
  $instance = $items
    ->getFieldDefinition();
  $token_data = [
    $instance
      ->getTargetEntityTypeId() => $items
      ->getEntity(),
  ];

  // Get alt and title text from the formatter settings, and resolve tokens.
  if ($image_alt = $this
    ->getSetting('image_alt')) {
    $image_alt = $this->textimageFactory
      ->processTextString($image_alt, NULL, $token_data, $bubbleable_metadata);
  }
  if ($image_title = $this
    ->getSetting('image_title')) {
    $image_title = $this->textimageFactory
      ->processTextString($image_title, NULL, $token_data, $bubbleable_metadata);
  }

  // Check if the formatter involves a link to the parent entity.
  $entity_url = $this
    ->getSetting('image_link') == 'content' ? $items
    ->getEntity()
    ->toUrl() : NULL;
  foreach ($files as $delta => $file) {
    $textimage = $this->textimageFactory
      ->get($bubbleable_metadata)
      ->setStyle($image_style)
      ->setSourceImageFile($file)
      ->setTokenData($token_data)
      ->process(NULL);

    // Check if the formatter involves a link to the original or derived
    // image.
    if (!$entity_url) {
      switch ($this
        ->getSetting('image_link')) {
        case 'file':
          $url = Url::fromUri(file_create_url($file
            ->getFileUri()));
          break;
        case 'derivative':
          $url = $textimage
            ->getUrl();
          break;
        default:
          $url = NULL;
          break;
      }
    }
    $elements[$delta] = [
      '#theme' => 'textimage_formatter',
      '#item' => $file->_referringItem,
      '#uri' => $textimage
        ->getUri(),
      '#width' => $textimage
        ->getWidth(),
      '#height' => $textimage
        ->getHeight(),
      '#alt' => $image_alt,
      '#title' => $image_title,
      '#anchor_url' => $entity_url ?: $url,
    ];
    $bubbleable_metadata
      ->applyTo($elements[$delta]);
  }
  return $elements;
}