You are here

public function FilterResponsiveWrappers::process in Responsive wrappers 2.0.x

Same name and namespace in other branches
  1. 8 src/Plugin/Filter/FilterResponsiveWrappers.php \Drupal\responsivewrappers\Plugin\Filter\FilterResponsiveWrappers::process()

Implements filter processor.

Overrides FilterInterface::process

File

src/Plugin/Filter/FilterResponsiveWrappers.php, line 25

Class

FilterResponsiveWrappers
Check the content and add responsive classes and wrappers.

Namespace

Drupal\responsivewrappers\Plugin\Filter

Code

public function process($text, $langcode) {
  $result = new FilterProcessResult($text);
  if ($this->settings['responsive_iframe'] || $this->settings['responsive_table'] || $this->settings['responsive_image']) {

    // The module base config.
    $config = \Drupal::config('responsivewrappers.settings');

    // The field content DOM.
    $dom = Html::load($text);

    // Responsive iframe class and wrapper.
    if ($this->settings['responsive_iframe']) {
      $iframe_pattern = $this->settings['responsive_iframe_pattern'];
      $iframe_wrapper_class = $config
        ->get('iframe_wrapper_class');
      $iframe_class = $config
        ->get('iframe_class');
      $iframes = $dom
        ->getElementsByTagName('iframe');
      foreach ($iframes as $iframe) {

        // Iframe detection pattern.
        $iframe_src = $iframe
          ->getAttribute('src');
        if (preg_match($iframe_pattern, $iframe_src)) {

          // Iframe wrapper.
          $current_iframe_wrapper_class = $iframe->parentNode->tagName === 'div' ? $iframe->parentNode
            ->getAttribute('class') : '';

          // If exists, replace video-embed-field functionality.
          if (strpos($current_iframe_wrapper_class, 'video-embed-field-responsive-video') !== FALSE) {
            $current_iframe_wrapper_class = str_replace('video-embed-field-responsive-video', $iframe_wrapper_class, $current_iframe_wrapper_class);
            $iframe->parentNode
              ->setAttribute('class', $current_iframe_wrapper_class);
          }
          elseif (strpos($current_iframe_wrapper_class, $iframe_wrapper_class) === FALSE) {

            // If not exists the wrapper create it.
            $iframe_wrapper = $dom
              ->createElement('div');
            $iframe_wrapper
              ->setAttribute('class', $iframe_wrapper_class);
            $iframe = $iframe->parentNode
              ->replaceChild($iframe_wrapper, $iframe);
            $iframe_wrapper
              ->appendChild($iframe);
          }

          // Iframe class.
          $current_iframe_class = $iframe
            ->getAttribute('class');
          if (empty($current_iframe_class) || !in_array($iframe_class, explode(' ', $video_class))) {
            $iframe
              ->setAttribute('class', trim($current_iframe_class . ' ' . $iframe_class));
          }
        }
      }
    }

    // Responsive table wrapper and class.
    if ($this->settings['responsive_table']) {
      $table_wrapper_class = $config
        ->get('table_wrapper_class');
      $table_class = $config
        ->get('table_class');
      $tables = $dom
        ->getElementsByTagName('table');
      foreach ($tables as $table) {

        // Table wrapper.
        $current_table_wrapper_class = $table->parentNode->tagName === 'div' ? $table->parentNode
          ->getAttribute('class') : '';
        if (strpos($current_table_wrapper_class, $table_wrapper_class) === FALSE) {
          $table_wrapper = $dom
            ->createElement('div');
          $table_wrapper
            ->setAttribute('class', $table_wrapper_class);
          $table = $table->parentNode
            ->replaceChild($table_wrapper, $table);
          $table_wrapper
            ->appendChild($table);
        }

        // Table class.
        $current_table_class = $table
          ->getAttribute('class');
        if (empty($current_table_class) || !in_array($table_class, explode(' ', $current_table_class))) {
          $table
            ->setAttribute('class', trim($current_table_class . ' ' . $table_class));
        }
      }
    }

    // Responsive image class.
    if ($this->settings['responsive_image']) {
      $image_class = $config
        ->get('image_class');
      $images = $dom
        ->getElementsByTagName('img');
      foreach ($images as $image) {

        // Image class.
        $current_image_class = $image
          ->getAttribute('class');
        if (strpos($current_image_class, $image_class) === FALSE) {
          $image
            ->setAttribute('class', trim($current_image_class . ' ' . $image_class));
        }
      }
    }
    $result
      ->setProcessedText(Html::serialize($dom));

    // Attach responsive CSS if needed.
    if ($config
      ->get('add_css')) {
      if (3 == $config
        ->get('version')) {
        $result
          ->setAttachments([
          'library' => [
            'responsivewrappers/responsivewrappers_v3',
          ],
        ]);
      }
      else {
        $result
          ->setAttachments([
          'library' => [
            'responsivewrappers/responsivewrappers_v4',
          ],
        ]);
      }
    }
  }
  return $result;
}