You are here

CreditsFormatter.php in Facebook Instant Articles 3.x

Same filename and directory in other branches
  1. 8.2 src/Plugin/Field/FieldFormatter/CreditsFormatter.php

File

src/Plugin/Field/FieldFormatter/CreditsFormatter.php
View source
<?php

namespace Drupal\fb_instant_articles\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\FieldItemListInterface;
use Facebook\InstantArticles\Elements\Footer;
use Facebook\InstantArticles\Elements\InstantArticle;
use Facebook\InstantArticles\Elements\Paragraph;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

/**
 * Plugin implementation of the 'fbia_credits' formatter.
 *
 * @FieldFormatter(
 *   id = "fbia_credits",
 *   label = @Translation("FBIA Credits"),
 *   field_types = {
 *     "string",
 *     "string_long"
 *   }
 * )
 */
class CreditsFormatter extends FormatterBase {

  /**
   * {@inheritdoc}
   */
  public function viewInstantArticle(FieldItemListInterface $items, InstantArticle $article, $region, NormalizerInterface $normalizer, $langcode = NULL) {
    $credits = [];
    foreach ($items as $delta => $item) {
      $credits[] = Paragraph::create()
        ->appendText($item->value);
    }
    if (!empty($credits)) {

      // Copyright can only go in the footer, put it there and ignore the given
      // $region.
      $footer = $article
        ->getFooter();
      if (!$footer) {
        $footer = Footer::create();
        $article
          ->withFooter($footer);
      }
      $footer
        ->withCredits($credits);
    }
  }

}

Classes

Namesort descending Description
CreditsFormatter Plugin implementation of the 'fbia_credits' formatter.