Skip to content

Commit

Permalink
Merge pull request #133 from openeuropa/EWPP-83
Browse files Browse the repository at this point in the history
EWPP-83: Add filter plugin for iframes.
  • Loading branch information
sergepavle committed Jul 27, 2020
2 parents 3c35cfb + 256d5de commit 5b11c45
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 4 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
},
"autoload-dev": {
"psr-4": {
"Drupal\\Tests\\oe_webtools\\": "tests"
"Drupal\\Tests\\oe_webtools\\": "./tests/"
}
},
"extra": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
declare(strict_types = 1);

use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Url;
use Drupal\media\IFrameMarkup;
use Drupal\oe_webtools_cookie_consent\Event\ConfigBannerPopupEvent;
use Drupal\oe_webtools_cookie_consent\Event\ConfigVideoPopupEvent;
Expand Down Expand Up @@ -55,8 +56,13 @@ function oe_webtools_cookie_consent_preprocess_media_oembed_iframe(array &$varia
if ($variables['media'] instanceof IFrameMarkup) {
preg_match('/<iframe.*src=["\']+([^"\']*)["\']+[^<>]*><\/iframe>/', $variables['media']->__toString(), $matches);
if (!empty($matches[1])) {
$new_url = OE_WEBTOOLS_COOKIE_CONSENT_EMBED_COOKIE_URL . '?oriurl=' . $matches[1] . '&lang=' . \Drupal::languageManager()->getCurrentLanguage()->getId();
$new_iframe = str_replace($matches[1], $new_url, $variables['media']);
$url = Url::fromUri(OE_WEBTOOLS_COOKIE_CONSENT_EMBED_COOKIE_URL, [
'query' => [
'oriurl' => $matches[1],
'lang' => \Drupal::languageManager()->getCurrentLanguage()->getId(),
],
]);
$new_iframe = str_replace($matches[1], $url->toString(), $variables['media']);
$variables['media'] = IFrameMarkup::create($new_iframe);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

declare(strict_types = 1);

namespace Drupal\oe_webtools_cookie_consent\Plugin\Filter;

use Drupal\Component\Utility\Html;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Url;
use Drupal\filter\FilterProcessResult;
use Drupal\filter\Plugin\FilterBase;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Provides a filter to prepend Cookie Consent Kit on iframe elements.
*
* @Filter(
* id = "filter_iframe_cck",
* title = @Translation("Apply cookie consent to iframes"),
* description = @Translation("Alters the <code>src</code> attribute of iframes to add the Webtools Cookie Consent Kit."),
* type = Drupal\filter\Plugin\FilterInterface::TYPE_TRANSFORM_REVERSIBLE
* )
*/
class FilterIframeCck extends FilterBase implements ContainerFactoryPluginInterface {

/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;

/**
* Constructs a FilterIframeCck object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin ID for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, LanguageManagerInterface $language_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->languageManager = $language_manager;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('language_manager')
);
}

/**
* {@inheritdoc}
*/
public function process($text, $langcode) {
$result = new FilterProcessResult($text);

if (stristr($text, 'iframe') !== FALSE) {
$dom = Html::load($text);
$xpath = new \DOMXPath($dom);
foreach ($xpath->query('//iframe[@src]') as $node) {
$src = $node->getAttribute('src');
// Prepend the cookie consent kit before the source url.
$url = Url::fromUri(OE_WEBTOOLS_COOKIE_CONSENT_EMBED_COOKIE_URL, [
'query' => [
'oriurl' => $src,
'lang' => $this->languageManager->getCurrentLanguage()->getId(),
],
]);
$node->setAttribute('src', $url->toString());
}
$result->setProcessedText(Html::serialize($dom));
$result->addCacheContexts(['languages:' . LanguageInterface::TYPE_INTERFACE]);
}

return $result;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types = 1);

namespace Drupal\Tests\oe_webtools_cookie_consent\Kernel;

use Drupal\Core\Language\LanguageInterface;
use Drupal\filter\FilterPluginCollection;
use Drupal\KernelTests\KernelTestBase;
use Drupal\language\Entity\ConfigurableLanguage;

/**
* Tests the iframe filter plugin.
*/
class IframeFilterPluginKernelTest extends KernelTestBase {

/**
* Modules to enable.
*
* @var array
*/
protected static $modules = [
'system',
'filter',
'language',
'oe_webtools_cookie_consent',
];

/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installConfig(['system', 'language']);

ConfigurableLanguage::createFromLangcode('hu')->save();
}

/**
* Tests the iframe with cookie consent kit filter.
*/
public function testIframeFilter() {
$manager = $this->container->get('plugin.manager.filter');
$filters = new FilterPluginCollection($manager, []);
$cck_filter = $filters->get('filter_iframe_cck');

$input = '<p><iframe src="https://example.com?q=a+b&p=1" style="width: 400px; height: 200px;"></iframe></p>';

// The default language is English.
$expected = '<p><iframe src="//europa.eu/webtools/crs/iframe/?oriurl=https%3A//example.com%3Fq%3Da%2Bb%26p%3D1&amp;lang=en" style="width: 400px; height: 200px;"></iframe></p>';
$this->assertEquals($expected, $cck_filter->process($input, LanguageInterface::LANGCODE_NOT_SPECIFIED)->getProcessedText());

// Test with a different language.
$this->config('system.site')->set('default_langcode', 'hu')->save();
$expected = '<p><iframe src="//europa.eu/webtools/crs/iframe/?oriurl=https%3A//example.com%3Fq%3Da%2Bb%26p%3D1&amp;lang=hu" style="width: 400px; height: 200px;"></iframe></p>';
$this->assertEquals($expected, $cck_filter->process($input, LanguageInterface::LANGCODE_NOT_SPECIFIED)->getProcessedText());
}

}
2 changes: 1 addition & 1 deletion tests/src/Functional/SmartLoaderDependenciesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types = 1);

namespace Drupal\Tests\oe_webtools_analytics\Functional;
namespace Drupal\Tests\oe_webtools\Functional;

use Drupal\oe_webtools_analytics\AnalyticsEventInterface;
use Drupal\Tests\BrowserTestBase;
Expand Down

0 comments on commit 5b11c45

Please sign in to comment.