Skip to content

Commit

Permalink
EWPP-2069: Add config file to set internal domain regex.
Browse files Browse the repository at this point in the history
  • Loading branch information
22Alexandra committed May 10, 2022
1 parent 8965551 commit 2a6bd2d
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 24 deletions.
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.'
3 changes: 2 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 @@ -42,3 +42,4 @@ services:
arguments: ['@entity_type.bundle.info']
oe_theme_helper.external_links:
class: Drupal\oe_theme_helper\ExternalLinks
arguments: ['@config.factory']
44 changes: 23 additions & 21 deletions modules/oe_theme_helper/src/ExternalLinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,31 @@
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 configuration factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;

/**
* Constructs an ExternalLinks object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The configuration factory.
*/
public function __construct(ConfigFactoryInterface $config_factory) {
$this->configFactory = $config_factory;
}

/**
* {@inheritdoc}
*/
Expand All @@ -28,28 +46,12 @@ public function isExternalLink($url): bool {
return $external;
}

// If it's external link, make sure its domain is not considered internal.
$internal_domains = $this->internalDomains();
foreach ($internal_domains as $internal_domain) {
if (strpos($path, $internal_domain)) {
$external = FALSE;
break;
}
// If it's an external link, make sure its domain is not internal.
$internal_domain_expression = $this->configFactory->get('oe_theme_helper.internal_domains')->get('internal_domain');
if (!$internal_domain_expression) {
return $external;
}

return $external;
}

/**
* Defines a list of domain considered internal.
*
* @return array|null
* The list of internal domains or NULL if there is none.
*/
protected function internalDomains(): ?array {
return [
'europa.eu',
];
return !preg_match_all($internal_domain_expression, $path);
}

}
15 changes: 13 additions & 2 deletions modules/oe_theme_helper/src/TwigExtension/TwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,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 +46,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 Down Expand Up @@ -535,7 +546,7 @@ public function getLinkIcon(array $context, string $path, string $size = 's'): a
'size' => $size,
'color' => 'primary',
];
if (\Drupal::service('oe_theme_helper.external_links')->isExternalLink($path)) {
if ($this->externalLinks->isExternalLink($path)) {
$icon['name'] = 'external';
}
else {
Expand Down
1 change: 1 addition & 0 deletions tests/src/Kernel/AbstractKernelTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ protected function setUp(): void {
$this->container->get('theme_installer')->install(['oe_theme']);
$this->config('system.theme')->set('default', 'oe_theme')->save();
$this->container->set('theme.registry', NULL);
$this->config('oe_theme_helper.internal_domains')->set('internal_domain', '/(^|^[^:]+:\/\/|[^\.]+\.)europa\.eu/m')->save();

// @todo Drupal 9 ignores settings in settings.testing.php in kernel tests.
// See https://www.drupal.org/project/drupal/issues/3190974. Need to
Expand Down

0 comments on commit 2a6bd2d

Please sign in to comment.