function FilterAlign::process

Same name in other branches
  1. 9 core/modules/filter/src/Plugin/Filter/FilterAlign.php \Drupal\filter\Plugin\Filter\FilterAlign::process()
  2. 10 core/modules/filter/src/Plugin/Filter/FilterAlign.php \Drupal\filter\Plugin\Filter\FilterAlign::process()
  3. 11.x core/modules/filter/src/Plugin/Filter/FilterAlign.php \Drupal\filter\Plugin\Filter\FilterAlign::process()

Overrides FilterInterface::process

File

core/modules/filter/src/Plugin/Filter/FilterAlign.php, line 24

Class

FilterAlign
Provides a filter to align elements.

Namespace

Drupal\filter\Plugin\Filter

Code

public function process($text, $langcode) {
    $result = new FilterProcessResult($text);
    if (stristr($text, 'data-align') !== FALSE) {
        $dom = Html::load($text);
        $xpath = new \DOMXPath($dom);
        foreach ($xpath->query('//*[@data-align]') as $node) {
            // Read the data-align attribute's value, then delete it.
            $align = $node->getAttribute('data-align');
            $node->removeAttribute('data-align');
            // If one of the allowed alignments, add the corresponding class.
            if (in_array($align, [
                'left',
                'center',
                'right',
            ])) {
                $classes = $node->getAttribute('class');
                $classes = strlen($classes) > 0 ? explode(' ', $classes) : [];
                $classes[] = 'align-' . $align;
                $node->setAttribute('class', implode(' ', $classes));
            }
        }
        $result->setProcessedText(Html::serialize($dom));
    }
    return $result;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.