public function InlineResponsiveImage::process in Inline responsive images 8
Same name and namespace in other branches
- 7 src/Plugin/Filter/InlineResponsiveImage.php \Drupal\inline_responsive_image\Plugin\Filter\InlineResponsiveImage::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/ InlineResponsiveImage.php, line 20
Class
- InlineResponsiveImage
- Plugin annotation @Filter( id = "inline_responsive_image", title = @Translation("Inline responsive images"), description = @Translation("Filter for inline responsive images"), type =…
Namespace
Drupal\inline_responsive_image\Plugin\FilterCode
public function process($text, $langcode) {
$result = new FilterProcessResult($text);
if (stristr($text, 'data-style') !== FALSE) {
$dom = Html::load($text);
$xpath = new \DOMXPath($dom);
foreach ($xpath
->query('//*[@data-style]') as $node) {
$style = $node
->getAttribute('data-style');
$caption = $node
->getAttribute('data-caption');
$align = $node
->getAttribute('data-align');
$entityUuid = $node
->getAttribute('data-entity-uuid');
list($type, $style_name) = explode(':', $style);
$file = \Drupal::entityManager()
->loadEntityByUuid('file', $entityUuid);
if ($file) {
$responsiveImage = [];
if ($type === 'responsive') {
$responsiveImage = [
'#theme' => 'inline_responsive_image',
'#image' => [
'#type' => 'responsive_image',
'#responsive_image_style_id' => $style_name,
'#uri' => $file
->getFileUri(),
],
'#attributes' => [
'class' => [],
],
'#caption' => !empty($caption) ? $caption : '',
];
}
elseif ($type === 'imagestyle') {
$responsiveImage = [
'#theme' => 'inline_responsive_image',
'#image' => [
'#theme' => 'image_style',
'#style_name' => $style_name,
'#uri' => $file
->getFileUri(),
],
'#attributes' => [
'class' => [],
],
'#caption' => !empty($caption) ? $caption : '',
];
}
if (count($responsiveImage) > 0) {
$responsiveImage['#attributes']['class'][] = $style;
if (!empty($align)) {
$responsiveImage['#attributes']['class'][] = $align;
}
$responsiveImageHtml = \Drupal::service('renderer')
->render($responsiveImage);
$fragment = $dom
->createDocumentFragment();
if ($fragment
->appendXML($responsiveImageHtml)) {
$node->parentNode
->replaceChild($fragment, $node);
}
}
}
}
$result
->setProcessedText(Html::serialize($dom));
}
return $result;
}