CreditsFormatter.php in Facebook Instant Articles 3.x
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;
class CreditsFormatter extends FormatterBase {
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)) {
$footer = $article
->getFooter();
if (!$footer) {
$footer = Footer::create();
$article
->withFooter($footer);
}
$footer
->withCredits($credits);
}
}
}