TransformerFormatter.php in Facebook Instant Articles 3.x
Same filename and directory in other branches
File
src/Plugin/Field/FieldFormatter/TransformerFormatter.phpView source
<?php
namespace Drupal\fb_instant_articles\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\fb_instant_articles\TransformerFactory;
use Facebook\InstantArticles\Elements\InstantArticle;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
/**
* Plugin implementation of the 'fbia_transformer' formatter.
*
* @FieldFormatter(
* id = "fbia_transformer",
* label = @Translation("FBIA Transformer"),
* field_types = {
* "text",
* "text_long",
* "text_with_summary",
* }
* )
*/
class TransformerFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
/**
* Renderer service.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/**
* Transformer factory.
*
* @var \Drupal\fb_instant_articles\TransformerFactory
*/
protected $transformerFactory;
/**
* Create a new instance of TransformerFormatter.
*
* @param string $plugin_id
* The plugin_id for the formatter.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
* The definition of the field to which the formatter is associated.
* @param array $settings
* The formatter settings.
* @param string $label
* The formatter label display setting.
* @param string $view_mode
* The view mode.
* @param array $third_party_settings
* Any third party settings settings.
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer service.
* @param \Drupal\fb_instant_articles\TransformerFactory $transformer_factory
* Transformer factory.
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, RendererInterface $renderer, TransformerFactory $transformer_factory) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->renderer = $renderer;
$this->transformerFactory = $transformer_factory;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['label'], $configuration['view_mode'], $configuration['third_party_settings'], $container
->get('renderer'), $container
->get('fb_instant_articles.transformer_factory'));
}
/**
* {@inheritdoc}
*/
public function viewInstantArticle(FieldItemListInterface $items, InstantArticle $article, $region, NormalizerInterface $normalizer, $langcode = NULL) {
foreach ($items as $delta => $item) {
$markup = [
'#type' => 'processed_text',
'#text' => $item->value,
'#format' => $item->format,
'#langcode' => $item
->getLangcode(),
];
$markup = (string) $this->renderer
->renderPlain($markup);
// Note that by passing $article as the first argument, we are implicitly
// ignoring the $region param and assuming this content goes into the
// body are exclusively. The Facebook SDK currently only supports using
// the transformer for body elements.
$transformer = $this->transformerFactory
->getTransformer();
$transformer
->transformString($article, $markup);
$this->transformerFactory
->flushTransformerLogs($transformer);
}
}
}
Classes
Name![]() |
Description |
---|---|
TransformerFormatter | Plugin implementation of the 'fbia_transformer' formatter. |