SvgEmbed.php in SVG Embed 2.0.x
File
src/Plugin/Filter/SvgEmbed.php
View source
<?php
namespace Drupal\svg_embed\Plugin\Filter;
use DOMXPath;
use Drupal\Component\Utility\Html;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\filter\FilterProcessResult;
use Drupal\filter\Plugin\FilterBase;
use Drupal\svg_embed\SvgEmbedProcessInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SvgEmbed extends FilterBase implements ContainerFactoryPluginInterface {
protected $entityTypeManager;
protected $svgEmbedProcess;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, SvgEmbedProcessInterface $svg_embed_process) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
$this->svgEmbedProcess = $svg_embed_process;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('svg_embed.process'));
}
public function process($text, $langcode) : FilterProcessResult {
$result = new FilterProcessResult($text);
if (stripos($text, 'data-entity-type="file"') !== FALSE) {
$dom = Html::load($text);
$xpath = new DOMXPath($dom);
$processed_uuids = [];
$patterns = [];
foreach ($xpath
->query('//*[@data-entity-type="file" and @data-entity-uuid]') as $node) {
$uuid = $node
->getAttribute('data-entity-uuid');
if (!isset($processed_uuids[$uuid])) {
$processed_uuids[$uuid] = $this->svgEmbedProcess
->translate($uuid, $langcode);
}
$patterns[(string) $node] = $uuid;
}
$text = Html::serialize($dom);
foreach ($patterns as $pattern => $uuid) {
$text = str_replace($pattern, $processed_uuids[$uuid], $text);
}
$result
->setProcessedText($text);
}
return $result;
}
}
Classes
Name |
Description |
SvgEmbed |
Provides a filter to embed and translate SVG images. |