AuthorFormatter.php in Facebook Instant Articles 8.2
File
src/Plugin/Field/FieldFormatter/AuthorFormatter.php
View source
<?php
namespace Drupal\fb_instant_articles\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Facebook\InstantArticles\Elements\Author;
use Facebook\InstantArticles\Elements\Header;
use Facebook\InstantArticles\Elements\InstantArticle;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
class AuthorFormatter extends FormatterBase {
public function viewInstantArticle(FieldItemListInterface $items, InstantArticle $article, $region, NormalizerInterface $normalizer, $langcode = NULL) {
foreach ($items as $delta => $item) {
$author = Author::create()
->withName($item->value);
$header = $article
->getHeader();
if (!$header) {
$header = Header::create();
$article
->withHeader($header);
}
$header
->addAuthor($author);
}
}
}