You are here

BlazyTextFormatter.php in Blazy 8.2

Same filename and directory in other branches
  1. 7 src/Plugin/Field/FieldFormatter/BlazyTextFormatter.php

File

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

namespace Drupal\blazy\Plugin\Field\FieldFormatter;

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\blazy\BlazyDefault;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Plugin implementation of the 'Blazy Grid Text' formatter.
 *
 * @FieldFormatter(
 *   id = "blazy_text",
 *   label = @Translation("Blazy Grid"),
 *   field_types = {
 *     "text",
 *     "text_long",
 *     "text_with_summary",
 *   },
 *   quickedit = {"editor" = "disabled"}
 * )
 */
class BlazyTextFormatter extends FormatterBase {
  use BlazyFormatterTrait;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {

    /**
     * @var \Drupal\blazy\Plugin\Field\FieldFormatter\BlazyTextFormatter
     */
    $instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
    $instance->formatter = $container
      ->get('blazy.manager');
    return $instance;
  }

  /**
   * {@inheritdoc}
   */
  public static function defaultSettings() {
    return BlazyDefault::baseSettings() + BlazyDefault::gridSettings();
  }

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

    // Early opt-out if the field is empty.
    if ($items
      ->isEmpty()) {
      return [];
    }

    // Build the settings.
    $settings = $this
      ->buildSettings();
    $settings['lazy'] = FALSE;
    $settings['langcode'] = $langcode;
    $settings['_grid'] = $settings['_unblazy'] = TRUE;

    // The ProcessedText element already handles cache context & tag bubbling.
    // @see \Drupal\filter\Element\ProcessedText::preRenderText()
    $build = [
      'settings' => $settings,
    ];
    foreach ($items as $item) {
      $build[] = [
        '#type' => 'processed_text',
        '#text' => $item->value,
        '#format' => $item->format,
        '#langcode' => $item
          ->getLangcode(),
      ];
    }

    // Pass to manager for easy updates to all Blazy formatters.
    return $this->formatter
      ->build($build);
  }

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state) {
    $element = [];
    $this
      ->admin()
      ->buildSettingsForm($element, $this
      ->getScopedFormElements());
    return $element;
  }

  /**
   * Defines the scope for the form elements.
   */
  public function getScopedFormElements() {
    return [
      'grid_form' => TRUE,
      'grid_required' => TRUE,
      'no_image_style' => TRUE,
      'no_layouts' => TRUE,
      'responsive_image' => FALSE,
      'style' => TRUE,
    ] + $this
      ->getCommonScopedFormElements();
  }

  /**
   * {@inheritdoc}
   */
  public static function isApplicable(FieldDefinitionInterface $field_definition) {
    return $field_definition
      ->getFieldStorageDefinition()
      ->isMultiple();
  }

}

Classes

Namesort descending Description
BlazyTextFormatter Plugin implementation of the 'Blazy Grid Text' formatter.