You are here

public function EditorFileReference::process in Drupal 9

Same name and namespace in other branches
  1. 8 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 82

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 = [];
    foreach ($xpath
      ->query('//*[@data-entity-type="file" and @data-entity-uuid]') as $node) {
      $uuid = $node
        ->getAttribute('data-entity-uuid');

      // If there is a 'src' attribute, set it to the file entity's current
      // URL. This ensures the URL works even after the file location changes.
      if ($node
        ->hasAttribute('src')) {
        $file = $this->entityRepository
          ->loadEntityByUuid('file', $uuid);
        if ($file instanceof FileInterface) {
          $node
            ->setAttribute('src', $file
            ->createFileUrl());
          if ($node->nodeName == 'img') {

            // Without dimensions specified, layout shifts can occur,
            // which are more noticeable on pages that take some time to load.
            // As a result, only mark images as lazy load that have dimensions.
            $image = $this->imageFactory
              ->get($file
              ->getFileUri());
            $width = $image
              ->getWidth();
            $height = $image
              ->getHeight();
            if ($width !== NULL && $height !== NULL) {
              if (!$node
                ->hasAttribute('width')) {
                $node
                  ->setAttribute('width', $width);
              }
              if (!$node
                ->hasAttribute('height')) {
                $node
                  ->setAttribute('height', $height);
              }
              if (!$node
                ->hasAttribute('loading')) {
                $node
                  ->setAttribute('loading', 'lazy');
              }
            }
          }
        }
      }

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