You are here

public function EditorFileReference::process in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/editor/src/Plugin/Filter/EditorFileReference.php \Drupal\editor\Plugin\Filter\EditorFileReference::process()

Performs the filter processing.

Parameters

string $text: The text string to be filtered.

string $langcode: The language code of the text to be filtered.

Return value

\Drupal\filter\FilterProcessResult The filtered text, wrapped in a FilterProcessResult object, and possibly with associated assets, cacheability metadata and placeholders.

Overrides FilterInterface::process

See also

\Drupal\filter\FilterProcessResult

File

core/modules/editor/src/Plugin/Filter/EditorFileReference.php, line 70
Contains \Drupal\editor\Plugin\Filter\EditorFileReference.

Class

EditorFileReference
Provides a filter to track images uploaded via a Text Editor.

Namespace

Drupal\editor\Plugin\Filter

Code

public function process($text, $langcode) {
  $result = new FilterProcessResult($text);
  if (stristr($text, 'data-entity-type="file"') !== FALSE) {
    $dom = Html::load($text);
    $xpath = new \DOMXPath($dom);
    $processed_uuids = array();
    foreach ($xpath
      ->query('//*[@data-entity-type="file" and @data-entity-uuid]') as $node) {
      $uuid = $node
        ->getAttribute('data-entity-uuid');

      // Only process the first occurrence of each file UUID.
      if (!isset($processed_uuids[$uuid])) {
        $processed_uuids[$uuid] = TRUE;
        $file = $this->entityManager
          ->loadEntityByUuid('file', $uuid);
        if ($file) {
          $result
            ->addCacheTags($file
            ->getCacheTags());
        }
      }
    }
  }
  return $result;
}