Skip to content

Commit

Permalink
Merge pull request #119 from City-of-Helsinki/PLATTA-4940-LinkProcess…
Browse files Browse the repository at this point in the history
…or-fix

PLATTA-4940 use twig for link title only when needed
  • Loading branch information
oviag authored Aug 14, 2023
2 parents 4c3d285 + 94615cb commit ae834a7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
29 changes: 21 additions & 8 deletions src/Link/LinkProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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);
}

Expand Down
4 changes: 4 additions & 0 deletions templates/helfi-link.html.twig
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
{#
This twig is called only for certain links
see conditions in Drupal\helfi_api_base\Link\LinkProcessor::preRenderLink
#}
{{ title }}

0 comments on commit ae834a7

Please sign in to comment.