You are here

private function DrupalInstantArticleDisplay::fieldFormatImageElement in Facebook Instant Articles 7

Formatter for the Image element.

Parameters

$items:

Element $region:

$settings:

1 call to DrupalInstantArticleDisplay::fieldFormatImageElement()
DrupalInstantArticleDisplay::fieldFormatterView in modules/fb_instant_articles_display/src/DrupalInstantArticleDisplay.php

File

modules/fb_instant_articles_display/src/DrupalInstantArticleDisplay.php, line 421
Contains \Drupal\fb_instant_articles_display\DrupalInstantArticleDisplay.

Class

DrupalInstantArticleDisplay
Facebook Instant Article node wrapper class. Builds up an InstantArticle object using field formatters.

Namespace

Drupal\fb_instant_articles_display

Code

private function fieldFormatImageElement($items, $region, $settings) {
  foreach ($items as $delta => $item) {
    if (!empty($settings['style'])) {
      if (empty($item['uri']) && !empty($item['fid'])) {

        // Ensure images work, without requiring a full node load.
        $item['uri'] = file_load($item['fid'])->uri;
      }
      $image_url = image_style_url($settings['style'], $item['uri']);
    }
    else {
      $image_url = file_create_url($item['uri']);
    }
    $image = Image::create()
      ->withURL($image_url);
    if (!empty($settings['caption']) && !empty($item['alt'])) {
      $image
        ->withCaption(Caption::create()
        ->appendText($item['alt']));
    }
    if (!empty($settings['likes'])) {
      $image
        ->enableLike();
    }
    if (!empty($settings['comments'])) {
      $image
        ->enableComments();
    }
    if (!empty($settings['fullscreen'])) {

      // @todo support other presentations.
      $image
        ->withPresentation(Image::FULLSCREEN);
    }
    if ($region instanceof Header) {
      $region
        ->withCover($image);

      // Header can only have one image, break after the first.
      break;
    }
    else {
      if ($region instanceof InstantArticle) {
        $region
          ->addChild($image);
      }
    }
  }
}