-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #133 from openeuropa/EWPP-83
EWPP-83: Add filter plugin for iframes.
- Loading branch information
Showing
5 changed files
with
160 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
modules/oe_webtools_cookie_consent/src/Plugin/Filter/FilterIframeCck.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
59 changes: 59 additions & 0 deletions
59
modules/oe_webtools_cookie_consent/tests/src/Kernel/IframeFilterPluginKernelTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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&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&lang=hu" style="width: 400px; height: 200px;"></iframe></p>'; | ||
$this->assertEquals($expected, $cck_filter->process($input, LanguageInterface::LANGCODE_NOT_SPECIFIED)->getProcessedText()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters