You are here

AmpTextFormatter.php in Accelerated Mobile Pages (AMP) 8

File

src/Plugin/Field/FieldFormatter/AmpTextFormatter.php
View source
<?php

/**
 * @file
 * Contains \Drupal\amp\Plugin\Field\FieldFormatter\AmpTextFormatter.
 */
namespace Drupal\amp\Plugin\Field\FieldFormatter;

use Drupal\text\Plugin\Field\FieldFormatter\TextDefaultFormatter;
use Drupal\Core\Field\FieldItemListInterface;

/**
 * Plugin implementation of the 'amp_text' formatter.
 *
 * @FieldFormatter(
 *   id = "amp_text",
 *   label = @Translation("AMP Text"),
 *   description = @Translation("Display AMP text."),
 *   field_types = {
 *     "string",
 *     "text",
 *     "text_long",
 *     "text_with_summary",
 *   },
 * )
 */
class AmpTextFormatter extends TextDefaultFormatter {

  /**
   * {@inheritdoc}
   */
  public function viewElements(FieldItemListInterface $items, $langcode) {
    $elements = array();

    // The ProcessedText element already handles cache context & tag bubbling.
    // @see \Drupal\filter\Element\ProcessedText::preRenderText()
    // The AmpProcessed text element extends that to pass #markup through the
    // amp library for processing markup into AMP HTML.
    foreach ($items as $delta => $item) {
      $elements[$delta] = array(
        '#type' => 'amp_processed_text',
        '#text' => $item->value,
        '#format' => $item->format,
        '#langcode' => $item
          ->getLangcode(),
      );
    }
    return $elements;
  }

}

Classes

Namesort descending Description
AmpTextFormatter Plugin implementation of the 'amp_text' formatter.