public function SubtitleFormatter::viewInstantArticle in Facebook Instant Articles 8.2
Same name and namespace in other branches
- 3.x src/Plugin/Field/FieldFormatter/SubtitleFormatter.php \Drupal\fb_instant_articles\Plugin\Field\FieldFormatter\SubtitleFormatter::viewInstantArticle()
Modifies the given instant article with the contents of this field.
Parameters
\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.
\Facebook\InstantArticles\Elements\InstantArticle $article: Instant article object to modify, rendering the contents of this field into it.
string $region: The Instant Article region name that the contents of this field should be rendered into.
\Symfony\Component\Serializer\Normalizer\NormalizerInterface $normalizer: Normalizer in case the formatter needs to recursively normalize, eg. in the case of a entity reference field.
string $langcode: (optional) The language that should be used to render the field. Defaults to the current content language.
Overrides InstantArticleFormatterInterface::viewInstantArticle
File
- src/
Plugin/ Field/ FieldFormatter/ SubtitleFormatter.php, line 123
Class
- SubtitleFormatter
- Plugin implementation of the 'fbia_subtitle' formatter.
Namespace
Drupal\fb_instant_articles\Plugin\Field\FieldFormatterCode
public function viewInstantArticle(FieldItemListInterface $items, InstantArticle $article, $region, NormalizerInterface $normalizer, $langcode = NULL) {
// Subtitles only go in the header. Create one if it doesn't exist yet and
// ignore the given $region.
$header = $article
->getHeader();
if (!$header) {
$header = Header::create();
$article
->withHeader($header);
}
// Note that there can only be one subtitle. We use the first value as the
// subtitle.
if (!$items
->isEmpty()) {
$item = $items
->get(0);
// For formatted text, pass the text through the filters and then through
// the FBIA transformer, before adding it to the article.
// in the subtitle.
if (in_array($items
->getFieldDefinition()
->getType(), [
'text',
'text_long',
'text_with_summary',
])) {
$subtitle_render_array = [
'#type' => 'processed_text',
'#text' => $item->value,
'#format' => $item->format,
'#langcode' => $item
->getLangcode(),
];
$subtitle_string = (string) $this->renderer
->renderPlain($subtitle_render_array);
// Here we create a Facebook H2 element, passing it as context to the
// transformer. It will therefore append any allowed elements, per the
// rules defined in the constructor (only a, i, b, em and strong tags),
// to the header. What that means is that tags will be stripped from the
// input string except a, i, b, em and strong.
$subtitle = H2::create();
$this->transformer
->transformString($subtitle, $subtitle_string);
}
else {
$subtitle = $item
->getString();
}
$header
->withSubTitle($subtitle);
}
}