You are here

public function DrupalInstantArticleDisplay::fieldFormatterView in Facebook Instant Articles 7

Parameters

$entity_type:

$entity:

$field:

$instance:

$langcode:

$items:

$display:

File

modules/fb_instant_articles_display/src/DrupalInstantArticleDisplay.php, line 154
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

public function fieldFormatterView($field, $instance, $langcode, $items, $display) {
  $settings = $display['settings'];
  $active_region = 'none';
  $header = $this->instantArticle
    ->getHeader();
  $footer = $this->instantArticle
    ->getFooter();

  // Determine which region this field belongs to.
  $regions = $this->layoutSettings->settings['regions'];
  foreach ($regions as $region => $fields) {
    if (in_array($field['field_name'], $fields)) {
      $active_region = $region;
      break;
    }
  }

  // We might not have added a footer. If the active region is a footer,
  // ensure it exists before continuing.
  if ($active_region === 'footer' && empty($footer)) {
    $footer = Footer::create();
    $this->instantArticle
      ->withFooter($footer);
  }

  // For each FBIA formatter, place according to the set region, only if it
  // actually makes sense.  ie you can't put a Kicker into the footer.
  switch ($display['type']) {
    case 'fbia_subtitle_formatter':
      if ($active_region === 'header') {
        $this
          ->fieldFormatSubtitle($items, $header);
      }
      break;
    case 'fbia_kicker_formatter':
      if ($active_region === 'header') {
        $this
          ->fieldFormatKicker($items, $header);
      }
      break;
    case 'fbia_author_formatter':
      if ($active_region === 'header') {
        $this
          ->fieldFormatAuthor($items, $header);
      }
      break;
    case 'fbia_ad_formatter':
      if ($active_region === 'header') {
        $this
          ->fieldFormatAdElement($items, $header, $settings);
      }
      break;
    case 'fbia_image_formatter':

      // Images are only allowed in the header and body.
      $pass_region = null;
      if ($active_region === 'header') {
        $pass_region = $header;
      }
      elseif ($active_region === 'body') {
        $pass_region = $this->instantArticle;
      }
      if ($pass_region) {
        $this
          ->fieldFormatImageElement($items, $pass_region, $settings);
      }
      break;
    case 'fbia_video_formatter':
      $pass_region = null;
      if ($active_region === 'header') {
        $pass_region = $header;
      }
      else {
        if ($active_region === 'body') {
          $pass_region = $header;
        }
      }
      if ($pass_region) {
        $this
          ->fieldFormatVideoElement($items, $pass_region, $settings);
      }
      break;
    case 'fbia_blockquote_formatter':
      if ($active_region === 'body') {
        $this
          ->fieldFormatTextContainer($items, $this->instantArticle, Blockquote::create());
      }
      break;
    case 'fbia_pullquote_formatter':
      if ($active_region === 'body') {
        $this
          ->fieldFormatTextContainer($items, $this->instantArticle, PullQuote::create());
      }
      break;
    case 'fbia_analytics_formatter':
      if ($active_region === 'body') {
        $this
          ->fieldFormatAnalyticsElement($items, $this->instantArticle, $settings);
      }
      break;
    case 'fbia_interactive_formatter':
      if ($active_region === 'body') {
        $this
          ->fieldFormatInteractiveElement($items, $this->instantArticle, $settings);
      }
      break;
    case 'fbia_list_formatter':
      if ($active_region === 'body') {
        $this
          ->fieldFormatListElement($items, $this->instantArticle, $settings);
      }
      break;
    case 'fbia_social_formatter':
      if ($active_region === 'body') {
        $this
          ->fieldFormatSocialElement($items, $this->instantArticle);
      }
      break;
    case 'fbia_credits_formatter':
      if ($active_region === 'footer') {
        $this
          ->fieldFormatCredits($items, $footer);
      }
      break;
    case 'fbia_copyright_formatter':
      if ($active_region === 'footer') {
        $this
          ->fieldFormatCopyright($items, $footer);
      }
      break;
    case 'fbia_transformer_formatter':
      if ($active_region === 'body') {
        $this
          ->fieldFormatTransfomer($items, $this->instantArticle, $instance, $langcode);
      }
      break;
  }
}