You are here

public function SvgEmbed::process in SVG Embed 8

Same name and namespace in other branches
  1. 2.0.x src/Plugin/Filter/SvgEmbed.php \Drupal\svg_embed\Plugin\Filter\SvgEmbed::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

src/Plugin/Filter/SvgEmbed.php, line 74

Class

SvgEmbed
Provides a filter to embed and translate SVG images.

Namespace

Drupal\svg_embed\Plugin\Filter

Code

public function process($text, $langcode) {
  $result = new FilterProcessResult($text);

  //TODO: Do we have at least one SVG reference in the $text?
  if (stripos($text, 'data-entity-type="file"') !== FALSE) {
    $dom = Html::load($text);
    $xpath = new DOMXPath($dom);
    $processed_uuids = array();

    //TODO: Identify the SVG nodes.

    /** @var \DOMNode $node */
    foreach ($xpath
      ->query('//*[@data-entity-type="file" and @data-entity-uuid]') as $node) {

      /** @noinspection PhpPossiblePolymorphicInvocationInspection */
      $uuid = $node
        ->getAttribute('data-entity-uuid');

      // Only process the first occurrence of each file UUID.
      if (!isset($processed_uuids[$uuid])) {
        $processed_uuids[$uuid] = $this->svgEmbedProcess
          ->translate($uuid, $langcode);

        //TODO: Stick the content into the $processed_uuids[$uuid].
      }
    }
  }
  return $result;
}