Skip to content

Commit

Permalink
Fix links array structure (#96)
Browse files Browse the repository at this point in the history
* Fix links array structure

* Move to HookPreprocess for explicit registration of preprocess function

* Remove extra line

* Remove leftover code

---------

Co-authored-by: Matt Glaman <[email protected]>
  • Loading branch information
darrenoh and mglaman authored Dec 20, 2023
1 parent 8a9e897 commit db32c4b
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/Theme/HookPreprocess.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,38 @@ public static function block(array &$variables): void
->load($variables['elements']['#id']);
$variables['block'] = new WrappedConfigEntity($block);
}

/**
* @param array{
* links?: array{title: string, html?: bool, href?: string, attributes?: array{class?: string[]}},
* heading?: mixed[]
* } $variables
*/
public static function links(array &$variables): void
{
if (!empty($variables['links'])) {
foreach ($variables['links'] as $key => &$link) {
$link += ['attributes' => []];
$link['attributes']['class'][] = $key;
if (isset($link['html']) && !empty($link['html'])) {
$link['title'] = ['#markup' => $link['title']];
unset($link['html']);
}
if (
isset($link['href'])
&& ($url = \Drupal::pathValidator()->getUrlIfValidWithoutAccessCheck($link['href']))
) {
$url->mergeOptions($link);
$link['url'] = $url;
unset($link['href']);
}
}
}
if (!empty($variables['heading'])) {
if (!empty($variables['heading']['class'])) {
$variables['heading']['attributes']['class'] = $variables['heading']['class'];
unset($variables['heading']['class']);
}
}
}
}

0 comments on commit db32c4b

Please sign in to comment.