public function SvgEmbed::process in SVG Embed 2.0.x
Same name and namespace in other branches
- 8 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\FilterCode
public function process($text, $langcode) : FilterProcessResult {
$result = new FilterProcessResult($text);
// 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 = [];
$patterns = [];
// 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);
}
$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;
}