From 26eff254f61f23dd37b58fabaab2ec0cfb9b7f8d Mon Sep 17 00:00:00 2001 From: Dragos Dumitrescu Date: Thu, 22 Jun 2023 16:53:12 +0300 Subject: [PATCH 1/4] PLATTA-4940 use twig for link title only when needed --- src/Link/LinkProcessor.php | 34 ++++++++++++++++++++++++++++------ templates/helfi-link.html.twig | 4 ++++ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/Link/LinkProcessor.php b/src/Link/LinkProcessor.php index a49b026d..005cb995 100644 --- a/src/Link/LinkProcessor.php +++ b/src/Link/LinkProcessor.php @@ -20,11 +20,10 @@ public static function preRenderLink($element) : array { /** @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'], - ]; + // In some cases we need to enrich title, so we use #theme helfi_link + // but in cases where this is not needed, having a twig for any title + // adds unnecessary load time, so we need to be specific when to use helfi_link. + $use_helfi_link = FALSE; // We can't set URI's 'external' property to FALSE, because it will // break the URL validation. @@ -35,7 +34,30 @@ public static function preRenderLink($element) : array { $element['#attributes']['data-protocol'] = $scheme; } } - $element['#title']['#attributes'] = $element['#attributes'] ?? []; + + if (!empty($element['#attributes']['data-is-external'])) { + $use_helfi_link = TRUE; + } + if (!empty($element['#attributes']['data-protocol'])) { + $use_helfi_link = TRUE; + } + + /** @var \Drupal\Core\Url $url */ + $url = $element['#url']; + $url_attributes = $url->getOption('attributes'); + if (!empty($url_attributes['data-selected-icon']) + || !empty($element['#attributes']['data-selected-icon'])) { + $use_helfi_link = TRUE; + } + + if ($use_helfi_link) { + $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 }} From b1eee9277fb5924b4d4137148690989bd591f5d3 Mon Sep 17 00:00:00 2001 From: Dragos Dumitrescu Date: Tue, 8 Aug 2023 11:33:56 +0300 Subject: [PATCH 2/4] PLATTA-4940 - fixes after review --- src/Link/LinkProcessor.php | 51 ++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 30 deletions(-) diff --git a/src/Link/LinkProcessor.php b/src/Link/LinkProcessor.php index 005cb995..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,49 +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'); - // In some cases we need to enrich title, so we use #theme helfi_link - // but in cases where this is not needed, having a twig for any title - // adds unnecessary load time, so we need to be specific when to use helfi_link. - $use_helfi_link = FALSE; - // 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 (!empty($element['#attributes']['data-is-external'])) { - $use_helfi_link = TRUE; - } - if (!empty($element['#attributes']['data-protocol'])) { - $use_helfi_link = TRUE; - } - - /** @var \Drupal\Core\Url $url */ - $url = $element['#url']; - $url_attributes = $url->getOption('attributes'); - if (!empty($url_attributes['data-selected-icon']) - || !empty($element['#attributes']['data-selected-icon'])) { - $use_helfi_link = TRUE; - } - - if ($use_helfi_link) { - $element['#title'] = [ - '#theme' => 'helfi_link', - '#url' => $element['#url'], - '#title' => $element['#title'], - ]; - $element['#title']['#attributes'] = $element['#attributes'] ?? []; - } + if ($useTemplate) { + $element['#title'] = [ + '#theme' => 'helfi_link', + '#url' => $element['#url'], + '#title' => $element['#title'], + ]; + $element['#title']['#attributes'] = $element['#attributes'] ?? []; } + return parent::preRenderLink($element); } From f9dcc969e74cbe2ed99fb53f25d0a384546fa8aa Mon Sep 17 00:00:00 2001 From: Dragos Dumitrescu Date: Tue, 8 Aug 2023 11:57:22 +0300 Subject: [PATCH 3/4] PLATTA-4940 - fixes after review --- src/Link/LinkProcessor.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Link/LinkProcessor.php b/src/Link/LinkProcessor.php index b2fb19e9..8540ce8c 100644 --- a/src/Link/LinkProcessor.php +++ b/src/Link/LinkProcessor.php @@ -39,6 +39,12 @@ public static function preRenderLink($element) : array { } $useTemplate = TRUE; } + + $url_attributes = $url->getOption('attributes'); + if (!empty($url_attributes['data-selected-icon']) + || !empty($element['#attributes']['data-selected-icon'])) { + $useTemplate = TRUE; + } } if ($useTemplate) { From 94615cb7bb3c4a134970acdad750bb575c3862c8 Mon Sep 17 00:00:00 2001 From: Dragos Dumitrescu Date: Wed, 9 Aug 2023 13:15:13 +0300 Subject: [PATCH 4/4] PLATTA-4940 - fixes after review --- src/Link/LinkProcessor.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/Link/LinkProcessor.php b/src/Link/LinkProcessor.php index 8540ce8c..b2fb19e9 100644 --- a/src/Link/LinkProcessor.php +++ b/src/Link/LinkProcessor.php @@ -39,12 +39,6 @@ public static function preRenderLink($element) : array { } $useTemplate = TRUE; } - - $url_attributes = $url->getOption('attributes'); - if (!empty($url_attributes['data-selected-icon']) - || !empty($element['#attributes']['data-selected-icon'])) { - $useTemplate = TRUE; - } } if ($useTemplate) {