Same name and namespace in other branches
  1. 8.9.x core/modules/filter/src/Element/ProcessedText.php \Drupal\filter\Element\ProcessedText
  2. 9 core/modules/filter/src/Element/ProcessedText.php \Drupal\filter\Element\ProcessedText

Hierarchy

Expanded class hierarchy of ProcessedText

File

core/modules/filter/src/Element/ProcessedText.php, line 18

Namespace

Drupal\filter\Element
View source
class ProcessedText extends RenderElementBase {

  /**
   * {@inheritdoc}
   */
  public function getInfo() {
    $class = static::class;
    return [
      '#text' => '',
      '#format' => NULL,
      '#filter_types_to_skip' => [],
      '#langcode' => '',
      '#pre_render' => [
        [
          $class,
          'preRenderText',
        ],
      ],
    ];
  }

  /**
   * Pre-render callback: Renders a processed text element into #markup.
   *
   * Runs all the enabled filters on a piece of text.
   *
   * Note: Because filters can inject JavaScript or execute PHP code, security
   * is vital here. When a user supplies a text format, you should validate it
   * using $format->access() before accepting/using it. This is normally done in
   * the validation stage of the Form API. You should for example never make a
   * preview of content in a disallowed format.
   *
   * @param array $element
   *   A structured array with the following key-value pairs:
   *   - #text: containing the text to be filtered
   *   - #format: containing the machine name of the filter format to be used to
   *     filter the text. Defaults to the fallback format.
   *   - #langcode: the language code of the text to be filtered, e.g. 'en' for
   *     English. This allows filters to be language-aware so language-specific
   *     text replacement can be implemented. Defaults to an empty string.
   *   - #filter_types_to_skip: an array of filter types to skip, or an empty
   *     array (default) to skip no filter types. All of the format's filters
   *     will be applied, except for filters of the types that are marked to be
   *     skipped. FilterInterface::TYPE_HTML_RESTRICTOR is the only type that
   *     cannot be skipped.
   *
   * @return array
   *   The passed-in element with the filtered text in '#markup'.
   *
   * @ingroup sanitization
   */
  public static function preRenderText($element) {
    $format_id = $element['#format'];
    $filter_types_to_skip = $element['#filter_types_to_skip'];
    $text = $element['#text'];
    $langcode = $element['#langcode'];
    if (!isset($format_id)) {
      $filter_settings = static::configFactory()
        ->get('filter.settings');
      $format_id = $filter_settings
        ->get('fallback_format');

      // Ensure 'filter.settings' cacheability is respected.
      CacheableMetadata::createFromRenderArray($element)
        ->addCacheableDependency($filter_settings)
        ->applyTo($element);
    }

    /** @var \Drupal\filter\Entity\FilterFormat $format **/
    $format = FilterFormat::load($format_id);

    // If the requested text format doesn't exist or its disabled, the text
    // cannot be filtered.
    if (!$format || !$format
      ->status()) {
      $message = !$format ? 'Missing text format: %format.' : 'Disabled text format: %format.';
      static::logger('filter')
        ->alert($message, [
        '%format' => $format_id,
      ]);
      $element['#markup'] = '';
      return $element;
    }
    $filter_must_be_applied = function (FilterInterface $filter) use ($filter_types_to_skip) {
      $enabled = $filter->status === TRUE;
      $type = $filter
        ->getType();

      // Prevent FilterInterface::TYPE_HTML_RESTRICTOR from being skipped.
      $filter_type_must_be_applied = $type == FilterInterface::TYPE_HTML_RESTRICTOR || !in_array($type, $filter_types_to_skip);
      return $enabled && $filter_type_must_be_applied;
    };

    // Convert all Windows and Mac newlines to a single newline, so filters only
    // need to deal with one possibility.
    $text = str_replace([
      "\r\n",
      "\r",
    ], "\n", $text);

    // Get a complete list of filters, ordered properly.

    /** @var \Drupal\filter\Plugin\FilterInterface[] $filters **/
    $filters = $format
      ->filters();

    // Give filters a chance to escape HTML-like data such as code or formulas.
    foreach ($filters as $filter) {
      if ($filter_must_be_applied($filter)) {
        $text = $filter
          ->prepare($text, $langcode);
      }
    }

    // Perform filtering.
    $metadata = BubbleableMetadata::createFromRenderArray($element);
    foreach ($filters as $filter) {
      if ($filter_must_be_applied($filter)) {
        $result = $filter
          ->process($text, $langcode);
        $metadata = $metadata
          ->merge($result);
        $text = $result
          ->getProcessedText();
      }
    }

    // Filtering and sanitizing have been done in
    // \Drupal\filter\Plugin\FilterInterface. $text is not guaranteed to be
    // safe, but it has been passed through the filter system and checked with
    // a text format, so it must be printed as is. (See the note about security
    // in the method documentation above.)
    $element['#markup'] = FilteredMarkup::create($text);

    // Set the updated bubbleable rendering metadata and the text format's
    // cache tag.
    $metadata
      ->applyTo($element);
    $element['#cache']['tags'] = Cache::mergeTags($element['#cache']['tags'], $format
      ->getCacheTags());
    return $element;
  }

  /**
   * Wraps a logger channel.
   *
   * @param string $channel
   *   The name of the channel.
   *
   * @return \Psr\Log\LoggerInterface
   *   The logger for this channel.
   */
  protected static function logger($channel) {
    return \Drupal::logger($channel);
  }

  /**
   * Wraps the config factory.
   *
   * @return \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected static function configFactory() {
    return \Drupal::configFactory();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 10
MessengerTrait::messenger public function Gets the messenger. 10
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.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 38
ProcessedText::configFactory protected static function Wraps the config factory.
ProcessedText::getInfo public function Returns the element properties for this element. Overrides ElementInterface::getInfo
ProcessedText::logger protected static function Wraps a logger channel.
ProcessedText::preRenderText public static function Pre-render callback: Renders a processed text element into #markup.
RenderElement::preRenderAjaxForm public static function Adds Ajax information about an element to communicate with JavaScript.
RenderElement::preRenderGroup public static function Adds members of this group as actual elements for rendering.
RenderElement::processAjaxForm public static function Form element processing handler for the #ajax form property. 1
RenderElement::processGroup public static function Arranges elements into groups.
RenderElement::setAttributes public static function Sets a form element's class attribute. Overrides ElementInterface::setAttributes
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
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. 1
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.