fb_instant_articles-8.x-2.2/src/Plugin/Field/FieldFormatter/ParagraphFormatter.php
src/Plugin/Field/FieldFormatter/ParagraphFormatter.php
<?php
namespace Drupal\fb_instant_articles\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Facebook\InstantArticles\Elements\InstantArticle;
use Facebook\InstantArticles\Elements\Paragraph;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
/**
* Plugin implementation of the 'fbia_paragraph' formatter.
*
* @FieldFormatter(
* id = "fbia_paragraph",
* label = @Translation("FBIA Paragraph"),
* field_types = {
* "string",
* "string_long"
* }
* )
*/
class ParagraphFormatter extends FormatterBase {
/**
* {@inheritdoc}
*/
public function viewInstantArticle(FieldItemListInterface $items, InstantArticle $article, $region, NormalizerInterface $normalizer, $langcode = NULL) {
foreach ($items as $delta => $item) {
// Paragraphs are only allowed in the content region, add it there
// regardless of the given $region. Note that the FBIA SDK will sanitize
// the value.
$article->addChild(
Paragraph::create()
->appendText($item->value)
);
}
}
}
