public function FilterImageLinkToSource::process in Image Resize Filter 8
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/ FilterImageLinkToSource.php, line 25
Class
- FilterImageLinkToSource
- Provides a filter to link images derivates to source (original) image.
Namespace
Drupal\image_resize_filter\Plugin\FilterCode
public function process($text, $langcode) {
$result = new FilterProcessResult($text);
$dom = Html::load($text);
$xpath = new \DOMXPath($dom);
/** @var \DOMNode $node */
foreach ($xpath
->query('//img') as $node) {
// Read the data-align attribute's value, then delete it.
$width = $node
->getAttribute('width');
$height = $node
->getAttribute('height');
$src = $node
->getAttribute('src');
if (!UrlHelper::isExternal($src)) {
if ($width || $height) {
/** @var \DOMNode $element */
$element = $dom
->createElement('a');
$element
->setAttribute('href', $src);
$node->parentNode
->replaceChild($element, $node);
$element
->appendChild($node);
}
}
}
$result
->setProcessedText(Html::serialize($dom));
return $result;
}