Skip to content

Commit

Permalink
Merge pull request #1130 from openeuropa/update-EPIC-EWPP-2106-Carousel
Browse files Browse the repository at this point in the history
EWPP-2106: Update Carousel epic branch.
  • Loading branch information
sergepavle authored Jun 22, 2022
2 parents c0b0b0e + 7d8a230 commit 69f0e72
Show file tree
Hide file tree
Showing 90 changed files with 1,016 additions and 169 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"nikic/php-parser": "^4.12.0",
"openeuropa/behat-transformation-context": "~0.1",
"openeuropa/code-review": "^2.0",
"openeuropa/oe_contact_forms": "^1.2",
"openeuropa/oe_contact_forms": "dev-master",
"openeuropa/oe_content": "^3.0.0",
"openeuropa/oe_corporate_blocks": "^4.4",
"openeuropa/oe_corporate_countries": "~2.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,17 @@ public function viewElements(ContentEntityInterface $entity) {
/** @var \Drupal\link\Plugin\Field\FieldType\LinkItem $link */
$link = $entity->get('oe_event_online_link')->first();
$value = $link->getValue();
$build['#url'] = $link->getUrl();
$url = $link->getUrl();
$url->setOptions([
'attributes' => [
'class' => [
'ecl-u-mt-l',
'ecl-u-mb-l',
'ecl-u-d-inline-block',
],
],
]);
$build['#url'] = $url;
$build['#label'] = $value['title'];

return $build;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ public function getLabel() {

/**
* {@inheritdoc}
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function viewElements(ContentEntityInterface $entity) {
if ($entity->get('oe_event_programme')->isEmpty()) {
Expand Down Expand Up @@ -150,6 +153,12 @@ public function viewElements(ContentEntityInterface $entity) {
continue;
}

$access = $programme->access('view', NULL, TRUE);
$cache->addCacheableDependency($access);
if (!$access->isAllowed()) {
continue;
}

$programme = $this->entityRepository->getTranslationFromContext($programme);
$cache->addCacheableDependency($programme);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@
type: 'standalone',
label: items.media_more_link.title,
path: items.media_more_link.url,
},
icon: {
'path': ecl_icon_path,
'name': 'external',
'size': 's',
external: items.media_more_link.url.toString()|is_external_url,
icon_path: ecl_icon_path
}
} %}
{% include '@ecl-twig/link' with _link only %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
<div {% if hidden %}class="ecl-u-d-none"{% endif %} data-livestream-element="true">
<h3 class="ecl-u-type-heading-3">{{ 'Livestream'|t }}</h3>
{{ description }}
<a href="{{ url }}" class="ecl-u-mt-l ecl-u-mb-l ecl-link ecl-link--cta ecl-u-d-inline-block">{{ label }}</a>
{{ pattern('link', {
variant: 'cta',
text: label,
url: url,
external_link: url.toString()|is_external_url,
icon_path: ecl_icon_path
}) }}
</div>
{% endif %}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,27 @@
<div id="event-registration-block">
{% if show_button %}
{% if registration_day %}
<a href="{{ url }}" class="ecl-u-mt-2xl ecl-link ecl-link--cta{% if not enabled %} ecl-u-d-none{% else %} ecl-u-d-inline-block{% endif %}" data-registration-active-element="true">{{ label }}</a>
<span class="ecl-u-mt-2xl{% if not enabled %} ecl-u-d-none{% else %} ecl-u-d-inline-block{% endif %}" data-registration-active-element="true">
{{ pattern('link', {
variant: 'cta',
text: label,
url: url,
external_link: url.toString()|is_external_url,
icon_path: ecl_icon_path
}) }}
</span>
<button type="submit" class="ecl-button ecl-button--call ecl-u-mt-2xl ecl-link ecl-link--cta{% if enabled %} ecl-u-d-none{% endif %}" disabled="" data-registration-upcoming-element="true">{{ label }}</button>
{% else %}
{% if enabled %}
<a href="{{ url }}" class="ecl-u-mt-2xl ecl-link ecl-link--cta ecl-u-d-inline-block">{{ label }}</a>
<span class="ecl-u-mt-2xl ecl-u-d-inline-block">
{{ pattern('link', {
variant: 'cta',
text: label,
url: url,
external_link: url.toString()|is_external_url,
icon_path: ecl_icon_path
}) }}
</span>
{% else %}
<button type="submit" class="ecl-button ecl-button--call ecl-u-mt-2xl ecl-link ecl-link--cta" disabled="">{{ label }}</button>
{% endif %}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
internal_domain: ''
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,10 @@ condition.plugin.oe_theme_helper_current_component_library:
mapping:
component_library:
type: string

oe_theme_helper.internal_domains:
type: config_object
mapping:
internal_domain:
type: string
label: 'Regular expression to identify internal domains.'
5 changes: 4 additions & 1 deletion modules/oe_theme_helper/oe_theme_helper.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ services:
parent: default_plugin_manager
oe_theme_helper.twig_extension.twig_extension:
class: Drupal\oe_theme_helper\TwigExtension\TwigExtension
arguments: ['@language_manager', '@renderer']
arguments: ['@language_manager', '@renderer', '@oe_theme_helper.external_links']
tags:
- { name: twig.extension }
oe_theme_helper.node_metadata_event_subscriber:
Expand All @@ -40,3 +40,6 @@ services:
class: Drupal\oe_theme_helper\MediaDataExtractorPluginManager
parent: default_plugin_manager
arguments: ['@entity_type.bundle.info']
oe_theme_helper.external_links:
class: Drupal\oe_theme_helper\ExternalLinks
arguments: ['@config.factory']
61 changes: 61 additions & 0 deletions modules/oe_theme_helper/src/ExternalLinks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types = 1);

namespace Drupal\oe_theme_helper;

use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Url;

/**
* Verifies if a URL is considered external or internal.
*/
class ExternalLinks implements ExternalLinksInterface {

/**
* The internal domain regex.
*
* @var string
*/
protected string $internalDomainExpression;

/**
* Constructs an ExternalLinks object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The configuration factory.
*/
public function __construct(ConfigFactoryInterface $config_factory) {
$this->internalDomainExpression = $config_factory->get('oe_theme_helper.internal_domains')->get('internal_domain') ?? '';
}

/**
* {@inheritdoc}
*/
public function isExternalLink($url = NULL): bool {
// If no value is provided or the value si not a proper link, we'll
// return FALSE as it can't be evaluated.
if (!$url) {
return FALSE;
}
if ($url instanceof Url) {
$external = $url->isExternal();
$path = UrlHelper::parse($url->toString())['path'];
}
else {
$external = UrlHelper::isExternal($url);
$path = UrlHelper::parse($url)['path'];
}
if (!$external) {
return $external;
}

// If it's an external link, make sure its domain is not internal.
if (!$this->internalDomainExpression) {
return $external;
}
return !preg_match_all($this->internalDomainExpression, $path);
}

}
25 changes: 25 additions & 0 deletions modules/oe_theme_helper/src/ExternalLinksInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types = 1);

namespace Drupal\oe_theme_helper;

use Drupal\Core\Url;

/**
* Interface for the external links service.
*/
interface ExternalLinksInterface {

/**
* Checks whether a link is considered external.
*
* @param \Drupal\Core\Url|string $url
* The url object.
*
* @return bool
* TRUE if the link is external, FALSE otherwise.
*/
public function isExternalLink(Url $url): bool;

}
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ protected function getIntroductionMetadata(string $field_name): array {
// We strip the tags because the component expects only one paragraph of
// text and the field is using a text format which adds paragraph tags.
'#type' => 'inline_template',
'#template' => '{{ summary|render|striptags("<strong><a><em>")|raw }}',
'#template' => '{{ summary|render|striptags("<strong><a><em><svg><use>")|raw }}',
'#context' => [
'summary' => [
'#type' => 'processed_text',
Expand Down
31 changes: 27 additions & 4 deletions modules/oe_theme_helper/src/TwigExtension/TwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Drupal\oe_theme_helper\TwigExtension;

use Drupal\Component\Render\MarkupInterface;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Language\LanguageManager;
use Drupal\Core\Language\LanguageManagerInterface;
Expand All @@ -16,6 +15,7 @@
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Template\Attribute;
use Drupal\oe_theme_helper\EuropeanUnionLanguages;
use Drupal\oe_theme_helper\ExternalLinksInterface;
use Drupal\smart_trim\Truncate\TruncateHTML;
use Drupal\Core\Template\TwigExtension as CoreTwigExtension;
use Twig\Environment;
Expand Down Expand Up @@ -45,17 +45,27 @@ class TwigExtension extends AbstractExtension {
*/
protected $renderer;

/**
* The external links service.
*
* @var \Drupal\oe_theme_helper\ExternalLinksInterface
*/
protected $externalLinks;

/**
* Constructs a new TwigExtension object.
*
* @param \Drupal\Core\Language\LanguageManagerInterface $languageManager
* The language manager.
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer service.
* @param \Drupal\oe_theme_helper\ExternalLinksInterface $external_links
* The external links service.
*/
public function __construct(LanguageManagerInterface $languageManager, RendererInterface $renderer) {
public function __construct(LanguageManagerInterface $languageManager, RendererInterface $renderer, ExternalLinksInterface $external_links) {
$this->languageManager = $languageManager;
$this->renderer = $renderer;
$this->externalLinks = $external_links;
}

/**
Expand All @@ -71,7 +81,7 @@ public function getFilters(): array {
new TwigFilter('to_date_status', [$this, 'toDateStatus']),
new TwigFilter('to_ecl_attributes', [$this, 'toEclAttributes']),
new TwigFilter('smart_trim', [$this, 'smartTrim'], ['needs_environment' => TRUE]),
new TwigFilter('is_external_url', [UrlHelper::class, 'isExternal']),
new TwigFilter('is_external_url', [$this, 'isExternal']),
new TwigFilter('filter_empty', [$this, 'filterEmpty']),
new TwigFilter('create_markup', [$this, 'createMarkup']),
];
Expand Down Expand Up @@ -535,7 +545,7 @@ public function getLinkIcon(array $context, string $path, string $size = 's'): a
'size' => $size,
'color' => 'primary',
];
if (UrlHelper::isExternal($path)) {
if ($this->externalLinks->isExternalLink($path)) {
$icon['name'] = 'external';
}
else {
Expand Down Expand Up @@ -595,6 +605,19 @@ public function eclFooterLinks(array $context, array $links): array {
return $ecl_links;
}

/**
* Checks if a given path is external or not.
*
* @param string $path
* The path to be checked.
*
* @return bool
* Whether the path is external.
*/
public function isExternal(string $path): bool {
return $this->externalLinks->isExternalLink($path);
}

/**
* Creates a Markup object.
*
Expand Down
37 changes: 37 additions & 0 deletions modules/oe_theme_helper/tests/src/Kernel/ExternalLinksTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types = 1);

namespace Drupal\Tests\oe_theme_helper\Kernel;

use Drupal\Core\Url;
use Drupal\Tests\oe_theme\Kernel\AbstractKernelTestBase;

/**
* Provides test coverage for the ExternalLinks service.
*
* @group batch1
*/
class ExternalLinksTest extends AbstractKernelTestBase {

/**
* Covers isExternalLink method.
*/
public function testIsExternalLink(): void {
$external_links = $this->container->get('oe_theme_helper.external_links');
// Assert internal URL.
$this->assertFalse($external_links->isExternalLink(Url::fromUserInput('/user')));
// Assert external string path.
$this->assertTrue($external_links->isExternalLink('https://example.com'));
// Assert external string path under EU domain.
$this->assertFalse($external_links->isExternalLink('https://example.europa.eu'));
$this->assertFalse($external_links->isExternalLink('www.ec.europa.eu/info'));
// Assert null and empty value.
$this->assertFalse($external_links->isExternalLink());
$this->assertFalse($external_links->isExternalLink(''));
// Assert incorrect paths.
$this->assertFalse($external_links->isExternalLink('www. incorrect . com'));
$this->assertFalse($external_links->isExternalLink('www. ec . europa . eu'));
}

}
13 changes: 12 additions & 1 deletion modules/oe_theme_helper/tests/src/Unit/TwigExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Render\Renderer;
use Drupal\Core\Template\Loader\StringLoader;
use Drupal\oe_theme_helper\ExternalLinksInterface;
use Drupal\oe_theme_helper\TwigExtension\TwigExtension;
use Drupal\oe_theme_helper\EuropeanUnionLanguages;
use Drupal\Tests\UnitTestCase;
Expand Down Expand Up @@ -54,6 +55,13 @@ class TwigExtensionTest extends UnitTestCase {
*/
protected $twig;

/**
* The external links service.
*
* @var \Drupal\oe_theme_helper\ExternalLinksInterface
*/
protected $externalLinks;

/**
* {@inheritdoc}
*/
Expand All @@ -79,8 +87,11 @@ protected function setUp(): void {
// Create Renderer service mock.
$this->renderer = $this->prophesize(Renderer::class);

// Create the external link service mock.
$this->externalLinks = $this->prophesize(ExternalLinksInterface::class);

// Instantiate the system under test.
$this->extension = new TwigExtension($this->languageManager->reveal(), $this->renderer->reveal());
$this->extension = new TwigExtension($this->languageManager->reveal(), $this->renderer->reveal(), $this->externalLinks->reveal());

// For convenience, make a version of the Twig environment available that
// has the tested extension preloaded.
Expand Down
Loading

0 comments on commit 69f0e72

Please sign in to comment.