fb_instant_articles-8.x-2.2/src/Plugin/Field/FieldFormatter/KickerFormatter.php
src/Plugin/Field/FieldFormatter/KickerFormatter.php
<?php
namespace Drupal\fb_instant_articles\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Facebook\InstantArticles\Elements\Header;
use Facebook\InstantArticles\Elements\InstantArticle;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
/**
* Plugin implementation of the 'fbia_kicker' formatter.
*
* @FieldFormatter(
* id = "fbia_kicker",
* label = @Translation("FBIA Kicker"),
* field_types = {
* "string",
* "string_long"
* }
* )
*/
class KickerFormatter extends FormatterBase {
/**
* {@inheritdoc}
*/
public function viewInstantArticle(FieldItemListInterface $items, InstantArticle $article, $region, NormalizerInterface $normalizer, $langcode = NULL) {
// Kickers are only added to the header. Get the header, creating it if need
// be.
$header = $article->getHeader();
if (!$header) {
$header = Header::create();
$article->withHeader($header);
}
// Note that there can only be one kicker. We use the first value as the
// kicker.
if ($item = $items->get(0)) {
$header->withKicker($items->get(0)->value);
}
}
}
