You are here

function _responsivewrappers_process in Responsive wrappers 7

Process callback for the responsive wrappers filter.

1 string reference to '_responsivewrappers_process'
responsivewrappers_filter_info in ./responsivewrappers.module
Implements hook_filter_info().

File

./responsivewrappers.module, line 101
Responsive bootstrap wrappers input filter.

Code

function _responsivewrappers_process($text, $filter) {
  if ($filter->settings['responsive_iframe'] || $filter->settings['responsive_table'] || $filter->settings['responsive_image']) {

    // Bootstrap version output.
    $version = variable_get('responsivewrappers_output_version', 3);

    // Iframe wrappers.
    if ($filter->settings['responsive_iframe']) {
      $pattern = $filter->settings['responsive_iframe_pattern'];
      preg_match_all($pattern, $text, $matches);
      if (!empty($matches[0])) {
        $aspect_ratio = $filter->settings['responsive_iframe_aspect_ratio'];
        foreach ($matches[0] as $match) {

          // Only add the wrapper if not exists.
          if (strpos('embed-responsive', $match) === FALSE) {
            $match_replace = _responsivewrappers_add_class($match, 'iframe', 'embed-responsive-item');
            $text = str_replace($match, '<div class="embed-responsive ' . $aspect_ratio . '">' . $match_replace . '</div>', $text);
          }
        }
      }
    }

    // Table wrappers.
    if ($filter->settings['responsive_table']) {
      preg_match_all('#<table.*>.*</table>#ims', $text, $matches);
      if (!empty($matches[0])) {
        foreach ($matches[0] as $match) {

          // Only add the wrapper if not exists.
          if (strpos('table-responsive', $match) === FALSE) {
            if ($version == 4) {
              $match_replace = _responsivewrappers_add_class($match, 'table', 'table table-responsive');
              $text = str_replace($match, $match_replace, $text);
            }
            else {
              $match_replace = _responsivewrappers_add_class($match, 'table', 'table');
              $text = str_replace($match, '<div class="table-responsive">' . $match_replace . '</div>', $text);
            }
          }
        }
      }
    }

    // Image wrappers.
    if ($filter->settings['responsive_image']) {
      preg_match_all('#<img[^>]+>#ui', $text, $matches);
      if (!empty($matches[0])) {
        $responsive_class = $version == 4 ? 'img-fluid' : 'img-responsive';
        foreach ($matches[0] as $match) {

          // Only add the class if not exists.
          if (strpos($responsive_class, $match) === FALSE) {
            $match_replace = _responsivewrappers_add_class($match, 'img', $responsive_class);
            $text = str_replace($match, $match_replace, $text);
          }
        }
      }
    }
  }
  return $text;
}