View source
<?php
namespace Drupal\responsivewrappers\Plugin\Filter;
use Drupal\filter\FilterProcessResult;
use Drupal\filter\Plugin\FilterBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Utility\Html;
class FilterResponsiveWrappers extends FilterBase {
public function process($text, $langcode) {
$result = new FilterProcessResult($text);
if ($this->settings['responsive_iframe'] || $this->settings['responsive_table'] || $this->settings['responsive_image']) {
$config = \Drupal::config('responsivewrappers.settings');
$dom = Html::load($text);
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_src = $iframe
->getAttribute('src');
if (preg_match($iframe_pattern, $iframe_src)) {
$current_iframe_wrapper_class = $iframe->parentNode->tagName === 'div' ? $iframe->parentNode
->getAttribute('class') : '';
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) {
$iframe_wrapper = $dom
->createElement('div');
$iframe_wrapper
->setAttribute('class', $iframe_wrapper_class);
$iframe = $iframe->parentNode
->replaceChild($iframe_wrapper, $iframe);
$iframe_wrapper
->appendChild($iframe);
}
$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));
}
}
}
}
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) {
$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);
}
$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));
}
}
}
if ($this->settings['responsive_image']) {
$image_class = $config
->get('image_class');
$images = $dom
->getElementsByTagName('img');
foreach ($images as $image) {
$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));
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;
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$form['responsive_iframe'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Responsive video'),
'#default_value' => isset($this->settings['responsive_iframe']) ? $this->settings['responsive_iframe'] : '',
'#description' => $this
->t('Add responsive class and wrapper for videos.'),
];
$form['responsive_iframe_pattern'] = [
'#type' => 'textfield',
'#title' => $this
->t('Source video pattern detection'),
'#default_value' => isset($this->settings['responsive_iframe_pattern']) ? $this->settings['responsive_iframe_pattern'] : '#.*(youtube.|vimeo.).*#ui',
'#description' => $this
->t('Regular expresion for source video detection. This pattern evaluates scr iframe attribute.'),
];
$form['responsive_table'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Responsive tables'),
'#default_value' => isset($this->settings['responsive_table']) ? $this->settings['responsive_table'] : '',
'#description' => $this
->t('Add responsive wrapper and class for tables.'),
];
$form['responsive_image'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Responsive images'),
'#default_value' => isset($this->settings['responsive_image']) ? $this->settings['responsive_image'] : '',
'#description' => $this
->t('Add responsive class for images.'),
];
return $form;
}
}