public function ImageFilter::process in Bootstrap Utilities - Text editor filters 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/ ImageFilter.php, line 24
Class
- ImageFilter
- Add Bootstrap Class to any image.
Namespace
Drupal\bootstrap_utilities\Plugin\FilterCode
public function process($text, $langcode) {
$result = new FilterProcessResult($text);
if (stristr($text, 'img') !== FALSE) {
$setting_classes = [];
$setting_classes[] = 'img-fluid';
$dom = Html::load($text);
$xpath = new \DOMXPath($dom);
$img_elements = $xpath
->query('//img');
if (!is_null($img_elements)) {
foreach ($img_elements as $element) {
$existing_classes = [];
$with_setting_classes = [];
if ($element
->getAttribute('class')) {
$existing_classes[] = $element
->getAttribute('class');
}
$with_setting_classes = array_unique(array_merge($existing_classes, $setting_classes), SORT_REGULAR);
$all_classes = implode(' ', $with_setting_classes);
$element
->setAttribute('class', $all_classes);
}
}
$result
->setProcessedText(Html::serialize($dom));
}
return $result;
}