diff --git a/src/Theme/HookPreprocess.php b/src/Theme/HookPreprocess.php index 31c1a7ea..2aa0a277 100644 --- a/src/Theme/HookPreprocess.php +++ b/src/Theme/HookPreprocess.php @@ -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']); + } + } + } }