diff --git a/modules/oe_theme_helper/config/install/oe_theme_helper.internal_domains.yml b/modules/oe_theme_helper/config/install/oe_theme_helper.internal_domains.yml new file mode 100644 index 0000000000..cb0c2c473a --- /dev/null +++ b/modules/oe_theme_helper/config/install/oe_theme_helper.internal_domains.yml @@ -0,0 +1 @@ +internal_domain: '' diff --git a/modules/oe_theme_helper/config/schema/oe_theme_helper.schema.yml b/modules/oe_theme_helper/config/schema/oe_theme_helper.schema.yml index 46ea42c255..4ea6ee806d 100644 --- a/modules/oe_theme_helper/config/schema/oe_theme_helper.schema.yml +++ b/modules/oe_theme_helper/config/schema/oe_theme_helper.schema.yml @@ -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.' diff --git a/modules/oe_theme_helper/oe_theme_helper.services.yml b/modules/oe_theme_helper/oe_theme_helper.services.yml index 8259407aae..9f990d6b99 100644 --- a/modules/oe_theme_helper/oe_theme_helper.services.yml +++ b/modules/oe_theme_helper/oe_theme_helper.services.yml @@ -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: @@ -42,3 +42,4 @@ services: arguments: ['@entity_type.bundle.info'] oe_theme_helper.external_links: class: Drupal\oe_theme_helper\ExternalLinks + arguments: ['@config.factory'] diff --git a/modules/oe_theme_helper/src/ExternalLinks.php b/modules/oe_theme_helper/src/ExternalLinks.php index 3b8c316807..77e5752488 100644 --- a/modules/oe_theme_helper/src/ExternalLinks.php +++ b/modules/oe_theme_helper/src/ExternalLinks.php @@ -5,6 +5,7 @@ namespace Drupal\oe_theme_helper; use Drupal\Component\Utility\UrlHelper; +use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Url; /** @@ -12,6 +13,23 @@ */ 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} */ @@ -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); } } diff --git a/modules/oe_theme_helper/src/TwigExtension/TwigExtension.php b/modules/oe_theme_helper/src/TwigExtension/TwigExtension.php index 698cd4cd36..da01d999a1 100644 --- a/modules/oe_theme_helper/src/TwigExtension/TwigExtension.php +++ b/modules/oe_theme_helper/src/TwigExtension/TwigExtension.php @@ -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; @@ -45,6 +46,13 @@ class TwigExtension extends AbstractExtension { */ protected $renderer; + /** + * The external links service. + * + * @var \Drupal\oe_theme_helper\ExternalLinksInterface + */ + protected $externalLinks; + /** * Constructs a new TwigExtension object. * @@ -52,10 +60,13 @@ class TwigExtension extends AbstractExtension { * 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; } /** @@ -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 { diff --git a/tests/src/Kernel/AbstractKernelTestBase.php b/tests/src/Kernel/AbstractKernelTestBase.php index 8b7ef87e0b..063dc47921 100644 --- a/tests/src/Kernel/AbstractKernelTestBase.php +++ b/tests/src/Kernel/AbstractKernelTestBase.php @@ -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