diff --git a/src/Link/LinkProcessor.php b/src/Link/LinkProcessor.php index a49b026d..b2fb19e9 100644 --- a/src/Link/LinkProcessor.php +++ b/src/Link/LinkProcessor.php @@ -5,6 +5,7 @@ namespace Drupal\helfi_api_base\Link; use Drupal\Core\Render\Element\Link; +use Drupal\Core\Render\Markup; use Drupal\Core\Url; /** @@ -16,27 +17,39 @@ final class LinkProcessor extends Link { * {@inheritdoc} */ public static function preRenderLink($element) : array { + $useTemplate = FALSE; + if (isset($element['#url']) && $element['#url'] instanceof Url) { + $url = $element['#url']; + + if ($element['#title'] instanceof Markup) { + $useTemplate = TRUE; + } + /** @var \Drupal\helfi_api_base\Link\InternalDomainResolver $resolver */ $resolver = \Drupal::service('helfi_api_base.internal_domain_resolver'); - $element['#title'] = [ - '#theme' => 'helfi_link', - '#url' => $element['#url'], - '#title' => $element['#title'], - ]; - // We can't set URI's 'external' property to FALSE, because it will // break the URL validation. - if ($resolver->isExternal($element['#url'])) { + if ($resolver->isExternal($url)) { $element['#attributes']['data-is-external'] = 'true'; - if ($scheme = $resolver->getProtocol($element['#url'])) { + if ($scheme = $resolver->getProtocol($url)) { $element['#attributes']['data-protocol'] = $scheme; } + $useTemplate = TRUE; } + } + + if ($useTemplate) { + $element['#title'] = [ + '#theme' => 'helfi_link', + '#url' => $element['#url'], + '#title' => $element['#title'], + ]; $element['#title']['#attributes'] = $element['#attributes'] ?? []; } + return parent::preRenderLink($element); } diff --git a/templates/helfi-link.html.twig b/templates/helfi-link.html.twig index 6d5a89f8..7375bc27 100644 --- a/templates/helfi-link.html.twig +++ b/templates/helfi-link.html.twig @@ -1 +1,5 @@ +{# + This twig is called only for certain links + see conditions in Drupal\helfi_api_base\Link\LinkProcessor::preRenderLink +#} {{ title }}