You are here

class AdvancedTextFormatter in Advanced Text Formatter 2.0.x

Same name and namespace in other branches
  1. 8 src/Plugin/Field/FieldFormatter/AdvancedTextFormatter.php \Drupal\advanced_text_formatter\Plugin\Field\FieldFormatter\AdvancedTextFormatter
  2. 2.1.x src/Plugin/Field/FieldFormatter/AdvancedTextFormatter.php \Drupal\advanced_text_formatter\Plugin\Field\FieldFormatter\AdvancedTextFormatter

Plugin implementation of the 'advanced_text_formatter' formatter.

Plugin annotation


@FieldFormatter(
  id = "advanced_text",
  module = "advanced_text_formatter",
  label = @Translation("Advanced Text"),
  field_types = {
    "string",
    "string_long",
    "text",
    "text_long",
    "text_with_summary",
  },
  quickedit = {
    "editor" = "plain_text"
  }
)

Hierarchy

Expanded class hierarchy of AdvancedTextFormatter

File

src/Plugin/Field/FieldFormatter/AdvancedTextFormatter.php, line 30

Namespace

Drupal\advanced_text_formatter\Plugin\Field\FieldFormatter
View source
class AdvancedTextFormatter extends FormatterBase {
  const FORMAT_DRUPAL = 'drupal';
  const FORMAT_INPUT = 'input';
  const FORMAT_NONE = 'none';
  const FORMAT_PHP = 'php';
  const FORMAT_LIMIT_HTML = 'limit_html';

  /**
   * {@inheritdoc}
   */
  public static function defaultSettings() {
    return [
      'trim_length' => 600,
      'ellipsis' => 1,
      'word_boundary' => 1,
      'token_replace' => 0,
      'filter' => 'input',
      'format' => 'plain_text',
      'allowed_html' => '<a> <b> <br> <dd> <dl> <dt> <em> <i> <li> <ol> <p> <strong> <u> <ul>',
      'autop' => 0,
      'use_summary' => 0,
    ] + parent::defaultSettings();
  }

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state) {
    $elTrimLengthId = Html::getUniqueId('advanced_text_formatter_trim');
    $elFilterId = Html::getUniqueId('advanced_text_formatter_filter');
    $element['trim_length'] = [
      '#id' => $elTrimLengthId,
      '#type' => 'number',
      '#title' => $this
        ->t('Trim length'),
      '#description' => $this
        ->t("Set this to 0 if you don't want to cut the text. Otherwise, input a positive integer."),
      '#size' => 10,
      '#default_value' => $this
        ->getSetting('trim_length'),
      '#required' => TRUE,
    ];
    $element['ellipsis'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Ellipsis'),
      '#description' => $this
        ->t('If checked, a "..." will be added if a field was trimmed.'),
      '#default_value' => $this
        ->getSetting('ellipsis'),
      '#states' => [
        'visible' => [
          '#' . $elTrimLengthId => [
            '!value' => '0',
          ],
        ],
      ],
    ];
    $element['word_boundary'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Word Boundary'),
      '#description' => $this
        ->t('If checked, this field be trimmed only on a word boundary.'),
      '#default_value' => $this
        ->getSetting('word_boundary'),
      '#states' => [
        'visible' => [
          '#' . $elTrimLengthId => [
            '!value' => '0',
          ],
        ],
      ],
    ];
    $element['use_summary'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Use Summary'),
      '#description' => $this
        ->t('If a summary exists, use it.'),
      '#default_value' => $this
        ->getSetting('use_summary'),
    ];
    $token_link = _advanced_text_formatter_browse_tokens($this->fieldDefinition
      ->getTargetEntityTypeId());
    $element['token_replace'] = [
      '#type' => 'checkbox',
      '#description' => $this
        ->t('Replace text pattern. e.g %node-title-token or %node-author-name-token, by token values.', [
        '%node-title-token' => '[node:title]',
        '%node-author-name-token' => '[node:author:name]',
      ]) . ' ',
      '#title' => $this
        ->t('Token Replace'),
      '#default_value' => $this
        ->getSetting('token_replace'),
    ];
    $element['filter'] = [
      '#id' => $elFilterId,
      '#title' => $this
        ->t('Filter'),
      '#type' => 'select',
      '#options' => [
        static::FORMAT_NONE => $this
          ->t('None'),
        static::FORMAT_INPUT => $this
          ->t('Selected Text Format'),
        static::FORMAT_LIMIT_HTML => $this
          ->t('Limit allowed HTML tags'),
        static::FORMAT_DRUPAL => $this
          ->t('Drupal'),
        static::FORMAT_PHP => $this
          ->t('PHP (Deprecated)'),
      ],
      '#default_value' => $this
        ->getSetting('filter'),
    ];
    $element['format'] = [
      '#title' => $this
        ->t('Format'),
      '#type' => 'select',
      '#options' => [],
      '#default_value' => $this
        ->getSetting('format'),
      '#states' => [
        'visible' => [
          '#' . $elFilterId => [
            'value' => 'drupal',
          ],
        ],
      ],
    ];
    $formats = filter_formats();
    foreach ($formats as $formatId => $format) {
      $element['format']['#options'][$formatId] = $format
        ->get('name');
    }
    $allowedHtml = $this
      ->getSetting('allowed_html');
    if (empty($allowedHtml)) {
      $tags = '';
    }
    elseif (is_string($allowedHtml)) {
      $tags = $allowedHtml;
    }
    else {
      $tags = '<' . implode('> <', $allowedHtml) . '>';
    }
    $element['allowed_html'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Allowed HTML tags'),
      '#description' => $this
        ->t('See <a href="@link" target="_blank">filter_xss()</a> for more information', [
        '@link' => 'http://api.drupal.org/api/drupal/core%21includes%21common.inc/function/filter_xss/8',
      ]),
      '#default_value' => $tags,
      '#element_validate' => [
        '_advanced_text_formatter_validate_allowed_html',
      ],
      '#states' => [
        'visible' => [
          '#' . $elFilterId => [
            'value' => 'php',
          ],
        ],
      ],
    ];
    $element['autop'] = [
      '#title' => $this
        ->t('Converts line breaks into HTML (i.e. &lt;br&gt; and &lt;p&gt;) tags.'),
      '#type' => 'checkbox',
      '#return_value' => 1,
      '#default_value' => $this
        ->getSetting('autop'),
      '#states' => [
        'invisible' => [
          '#' . $elFilterId => [
            '!value' => 'php',
          ],
        ],
      ],
    ];
    $element['br'] = [
      '#markup' => '<br/>',
    ];
    return $element;
  }

  /**
   * {@inheritdoc}
   */
  public function settingsSummary() {
    $summary = [];
    $yes = $this
      ->t('Yes');
    $no = $this
      ->t('No');
    if ($this
      ->getSetting('trim_length') > 0) {
      $summary[] = $this
        ->t('Trim length: @length', [
        '@length' => $this
          ->getSetting('trim_length'),
      ]);
      $summary[] = $this
        ->t('Ellipsis: @ellipsis', [
        '@ellipsis' => $this
          ->getSetting('ellipsis') ? $yes : $no,
      ]);
      $summary[] = $this
        ->t('Word Boundary: @word', [
        '@word' => $this
          ->getSetting('word_boundary') ? $yes : $no,
      ]);
      $summary[] = $this
        ->t('Use Summary: @summary', [
        '@summary' => $this
          ->getSetting('use_summary') ? $yes : $no,
      ]);
    }
    $summary[] = $this
      ->t('Token Replace: @token', [
      '@token' => $this
        ->getSetting('token_replace') ? $yes : $no,
    ]);
    switch ($this
      ->getSetting('filter')) {
      case static::FORMAT_DRUPAL:
        $formats = filter_formats();
        $format = $this
          ->getSetting('format');
        $format = isset($formats[$format]) ? $formats[$format]
          ->get('name') : $this
          ->t('Unknown');
        $summary[] = $this
          ->t('Filter: @filter', [
          '@filter' => $this
            ->t('Drupal'),
        ]);
        $summary[] = $this
          ->t('Format: @format', [
          '@format' => $format,
        ]);
        break;
      case static::FORMAT_PHP:
        \Drupal::messenger()
          ->addNotice(t('The PHP filter has been deprecated. Please use the "Limit allowed HTML tags" filter instead.'));
      case static::FORMAT_LIMIT_HTML:
        $text = [];
        $tags = $this
          ->getSetting('allowed_html');
        $autop = $this
          ->getSetting('autop');
        if (is_array($tags) && !empty($tags)) {
          $tags = '<' . implode('> <', $tags) . '>';
        }
        if (empty($tags)) {
          $text[] = $this
            ->t('Remove all HTML tags.');
        }
        else {
          $text[] = $this
            ->t('Limit allowed HTML tags: @tags.', [
            '@tags' => $tags,
          ]);
        }
        if (!empty($autop)) {
          $text[] = $this
            ->t('Convert line breaks into HTML.');
        }
        $summary[] = $this
          ->t('Filter: @filter', [
          '@filter' => implode(' ', $text),
        ]);
        break;
      case static::FORMAT_INPUT:
        $summary[] = $this
          ->t('Filter: @filter', [
          '@filter' => $this
            ->t('Selected Text Format'),
        ]);
        break;
      default:
        $summary[] = $this
          ->t('Filter: @filter', [
          '@filter' => $this
            ->t('None'),
        ]);
        break;
    }
    $summary = array_filter($summary);
    return $summary;
  }

  /**
   * {@inheritdoc}
   */
  public function viewElements(FieldItemListInterface $items, $langcode) {
    $elements = [];
    $token_data = [
      'user' => \Drupal::currentUser(),
      $items
        ->getEntity()
        ->getEntityTypeId() => $items
        ->getEntity(),
    ];
    foreach ($items as $delta => $item) {
      if ($this
        ->getSetting('use_summary') && !empty($item->summary)) {
        $output = $item->summary;
      }
      else {
        $output = $item->value;
      }
      if ($this
        ->getSetting('token_replace')) {
        $output = \Drupal::token()
          ->replace($output, $token_data);
      }
      switch ($this
        ->getSetting('filter')) {
        case static::FORMAT_DRUPAL:
          $output = check_markup($output, $this
            ->getSetting('format'), $item
            ->getLangcode());
          break;
        case static::FORMAT_PHP:
          \Drupal::messenger()
            ->addNotice(t('The PHP filter has been deprecated. Please use the "Limit allowed HTML tags" filter instead.'));
        case static::FORMAT_LIMIT_HTML:
          $output = Xss::filter($output, $this
            ->getSetting('allowed_html'));
          if ($this
            ->getSetting('autop')) {
            $output = _filter_autop($output);
          }
          break;
        case static::FORMAT_INPUT:
          $output = check_markup($output, $item->format, $item
            ->getLangcode());
          break;
      }
      if ($this
        ->getSetting('trim_length') > 0) {
        $options = [
          'word_boundary' => $this
            ->getSetting('word_boundary'),
          'max_length' => $this
            ->getSetting('trim_length'),
          'ellipsis' => $this
            ->getSetting('ellipsis'),
        ];
        $output = advanced_text_formatter_trim_text($output, $options);
      }
      $elements[$delta] = [
        '#markup' => $output,
        '#langcode' => $item
          ->getLangcode(),
      ];
    }
    return $elements;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AdvancedTextFormatter::defaultSettings public static function Defines the default settings for this plugin. Overrides PluginSettingsBase::defaultSettings
AdvancedTextFormatter::FORMAT_DRUPAL constant
AdvancedTextFormatter::FORMAT_INPUT constant
AdvancedTextFormatter::FORMAT_LIMIT_HTML constant
AdvancedTextFormatter::FORMAT_NONE constant
AdvancedTextFormatter::FORMAT_PHP constant
AdvancedTextFormatter::settingsForm public function Returns a form to configure settings for the formatter. Overrides FormatterBase::settingsForm
AdvancedTextFormatter::settingsSummary public function Returns a short summary for the current formatter settings. Overrides FormatterBase::settingsSummary
AdvancedTextFormatter::viewElements public function Builds a renderable array for a field value. Overrides FormatterInterface::viewElements
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
FormatterBase::$fieldDefinition protected property The field definition.
FormatterBase::$label protected property The label display setting.
FormatterBase::$settings protected property The formatter settings. Overrides PluginSettingsBase::$settings
FormatterBase::$viewMode protected property The view mode.
FormatterBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 12
FormatterBase::getFieldSetting protected function Returns the value of a field setting.
FormatterBase::getFieldSettings protected function Returns the array of field settings.
FormatterBase::isApplicable public static function Returns if the formatter can be used for the provided field. Overrides FormatterInterface::isApplicable 14
FormatterBase::prepareView public function Allows formatters to load information for field values being displayed. Overrides FormatterInterface::prepareView 2
FormatterBase::view public function Builds a renderable array for a fully themed field. Overrides FormatterInterface::view 1
FormatterBase::__construct public function Constructs a FormatterBase object. Overrides PluginBase::__construct 12
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 2
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginSettingsBase::$defaultSettingsMerged protected property Whether default settings have been merged into the current $settings.
PluginSettingsBase::$thirdPartySettings protected property The plugin settings injected by third party modules.
PluginSettingsBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies 6
PluginSettingsBase::getSetting public function Returns the value of a setting, or its default value if absent. Overrides PluginSettingsInterface::getSetting
PluginSettingsBase::getSettings public function Returns the array of settings, including defaults for missing settings. Overrides PluginSettingsInterface::getSettings
PluginSettingsBase::getThirdPartyProviders public function Gets the list of third parties that store information. Overrides ThirdPartySettingsInterface::getThirdPartyProviders
PluginSettingsBase::getThirdPartySetting public function Gets the value of a third-party setting. Overrides ThirdPartySettingsInterface::getThirdPartySetting
PluginSettingsBase::getThirdPartySettings public function Gets all third-party settings of a given module. Overrides ThirdPartySettingsInterface::getThirdPartySettings
PluginSettingsBase::mergeDefaults protected function Merges default settings values into $settings.
PluginSettingsBase::onDependencyRemoval public function Informs the plugin that some configuration it depends on will be deleted. Overrides PluginSettingsInterface::onDependencyRemoval 3
PluginSettingsBase::setSetting public function Sets the value of a setting for the plugin. Overrides PluginSettingsInterface::setSetting
PluginSettingsBase::setSettings public function Sets the settings for the plugin. Overrides PluginSettingsInterface::setSettings
PluginSettingsBase::setThirdPartySetting public function Sets the value of a third-party setting. Overrides ThirdPartySettingsInterface::setThirdPartySetting
PluginSettingsBase::unsetThirdPartySetting public function Unsets a third-party setting. Overrides ThirdPartySettingsInterface::unsetThirdPartySetting
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.