View 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\H2;
use Facebook\InstantArticles\Elements\Header;
use Facebook\InstantArticles\Elements\InstantArticle;
use Facebook\InstantArticles\Transformer\Rules\AnchorRule;
use Facebook\InstantArticles\Transformer\Rules\BoldRule;
use Facebook\InstantArticles\Transformer\Rules\ItalicRule;
use Facebook\InstantArticles\Transformer\Rules\PassThroughRule;
use Facebook\InstantArticles\Transformer\Rules\TextNodeRule;
use Facebook\InstantArticles\Transformer\Transformer;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
class SubtitleFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
protected $renderer;
protected $transformerFactory;
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;
}
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'));
}
public function viewInstantArticle(FieldItemListInterface $items, InstantArticle $article, $region, NormalizerInterface $normalizer, $langcode = NULL) {
$header = $article
->getHeader();
if (!$header) {
$header = Header::create();
$article
->withHeader($header);
}
if (!$items
->isEmpty()) {
$item = $items
->get(0);
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);
$subtitle = H2::create();
$transformer = $this
->getTransformer();
$transformer
->transformString($subtitle, $subtitle_string);
$this->transformerFactory
->flushTransformerLogs($transformer);
}
else {
$subtitle = $item
->getString();
}
$header
->withSubTitle($subtitle);
}
}
protected function getTransformer() : Transformer {
$transformer = $this->transformerFactory
->getTransformer();
$transformer
->setRules([
TextNodeRule::createFrom([]),
PassThroughRule::createFrom([
'selector' => '//html|//body|//p|//div|//blockquote|//h1|//h2|//h3|//h4',
]),
ItalicRule::createFrom([
'selector' => 'i',
]),
ItalicRule::createFrom([
'selector' => 'em',
]),
BoldRule::createFrom([
'selector' => 'b',
]),
BoldRule::createFrom([
'selector' => 'strong',
]),
AnchorRule::createFrom([
'selector' => 'a',
'properties' => [
AnchorRule::PROPERTY_ANCHOR_HREF => [
'type' => 'string',
'selector' => 'a',
'attribute' => 'href',
],
AnchorRule::PROPERTY_ANCHOR_REL => [
'type' => 'string',
'selector' => 'a',
'attribute' => 'rel',
],
],
]),
]);
return $transformer;
}
}