Skip to content

Commit

Permalink
Merge pull request #270 from openeuropa/OEL-2002
Browse files Browse the repository at this point in the history
OEL-2002: Preserve Drupal links.
  • Loading branch information
brummbar committed Oct 12, 2022
2 parents 6e808ad + ffc7cab commit f5b0c71
Show file tree
Hide file tree
Showing 20 changed files with 845 additions and 281 deletions.
151 changes: 13 additions & 138 deletions includes/menu.inc
Original file line number Diff line number Diff line change
Expand Up @@ -7,165 +7,40 @@

declare(strict_types = 1);

use Drupal\Core\Template\Attribute;
use Drupal\oe_bootstrap_theme\MenuPreprocess;

/**
* Implements hook_preprocess_menu().
*/
function oe_bootstrap_theme_preprocess_menu(&$variables, $hook) {
if ($hook === 'menu__toolbar') {
return;
}

$variables['pattern_items'] = [];
foreach ($variables['items'] as $key => $item) {
$pattern_item = [
'label' => $item['title'],
'path' => $item['url']->toString(),
'attributes' => new Attribute([
'class' => $item['in_active_trail'] ? ['active'] : [],
]),
];
if (empty($item['below'])) {
$variables['pattern_items'][$key] = $pattern_item;
continue;
}

$submenu = $pattern_item;
$submenu['attributes'] = new Attribute(['class' => 'nav-link']);
$pattern_item += [
'standalone' => TRUE,
'dropdown' => TRUE,
'link' => TRUE,
'alignment' => 'dropdown-menu_item-sm-start',
'trigger' => $submenu,
];
foreach ($item['below'] as $key_sub_item => $sub_item) {
$pattern_item['items'][$key_sub_item] = [
'label' => $sub_item['title'],
'path' => $sub_item['url']->toString(),
'attributes' => new Attribute([
'class' => $sub_item['in_active_trail'] ? ['active'] : [],
]),
];
}
$variables['pattern_items'][$key] = $pattern_item;
}

/** @var \Drupal\oe_bootstrap_theme\MenuPreprocess $menu_preprocess */
$menu_preprocess = \Drupal::classResolver(MenuPreprocess::class);
$menu_preprocess->preprocessMenu($variables, $hook);
}

/**
* Implements template_preprocess_menu_local_action().
*/
function oe_bootstrap_theme_preprocess_menu_local_action(&$variables) {
// Add button classes.
$variables['link']['#options']['attributes']['class'][] = 'btn';
$variables['link']['#options']['attributes']['class'][] = 'btn-sm';
$variables['link']['#options']['attributes']['class'][] = 'btn-primary';
/** @var \Drupal\oe_bootstrap_theme\MenuPreprocess $menu_preprocess */
$menu_preprocess = \Drupal::classResolver(MenuPreprocess::class);
$menu_preprocess->preprocessMenuLocalAction($variables);
}

/**
* Implements template_preprocess_links__dropbutton().
*/
function oe_bootstrap_theme_preprocess_links__dropbutton(&$variables) {
$links = &$variables['links'];

// Do nothing if we have no links.
if (!count($links)) {
return;
}

// Get the first link and use it for the dropbutton.
$link = reset($links);

/** @var \Drupal\Core\Url $url */
$variables['split'] = FALSE;
$button = $link['text'];
if (isset($link['link']) && ($url = $link['link']['#url'])) {
$button = $link['link'];

if ($url->getRouteName() !== '<nolink>') {
$variables['split'] = TRUE;
$button['#options']['attributes']['class'][] = 'btn';
$button['#options']['attributes']['class'][] = 'btn-sm';
$button['#options']['attributes']['class'][] = 'btn-outline-primary';
}

// Remove first link from links.
array_shift($links);
}

$variables['button'] = $button;
oe_bootstrap_theme_add_dropbutton_classes($links);
}

/**
* Adds required classes to dropbutton links.
*
* @param array $links
* Array with links items.
*/
function oe_bootstrap_theme_add_dropbutton_classes(array &$links): void {
foreach ($links as $key => $link) {
if (isset($links[$key]['link']) && is_array($links[$key]['link'])) {
$links[$key]['link']['#options']['attributes']['class'][] = 'dropdown-item';
}
if (isset($links[$key]['text_attributes'])) {
$links[$key]['text_attributes']->addClass('dropdown-item');
}
if (isset($links[$key]['attributes'])) {
$links[$key]['attributes']->addClass('dropdown-item');
}
if (isset($links[$key]['text']) && is_array($links[$key]['text'])) {
$links[$key]['text']['#attributes']['class'][] = 'dropdown-item';
}
}
/** @var \Drupal\oe_bootstrap_theme\MenuPreprocess $menu_preprocess */
$menu_preprocess = \Drupal::classResolver(MenuPreprocess::class);
$menu_preprocess->preprocessLinksDropbutton($variables);
}

/**
* Implements hook_preprocess_menu_local_tasks().
*/
function oe_bootstrap_theme_preprocess_menu_local_tasks(array &$variables): void {

// Sort local tasks by weight.
uasort($variables['primary'], [
'\Drupal\Component\Utility\SortArray',
'sortByWeightProperty',
]);
uasort($variables['secondary'], [
'\Drupal\Component\Utility\SortArray',
'sortByWeightProperty',
]);

foreach ($variables['primary'] as $link) {
oe_bootstrap_theme_prepare_menu_local_task_links($link, $variables, 'primary');
}

foreach ($variables['secondary'] as $link) {
oe_bootstrap_theme_prepare_menu_local_task_links($link, $variables, 'secondary');
}
}

/**
* Prepare menu local tasks render links.
*
* @param array $link
* Array with the links items to be prepared.
* @param array $variables
* The render array.
* @param string $type
* Discriminate between primary or secondary.
*/
function oe_bootstrap_theme_prepare_menu_local_task_links(array $link, array &$variables, string $type): void {
/** @var Drupal\Core\Url $url */
$url = $link['#link']['url'];
if ($url->access($variables['user'])) {
$variables[$type . '_links'][] = [
'link_style' => $type,
'path' => $url->toString(),
'label' => $link['#link']['title'],
'standalone' => TRUE,
'attributes' => $link['#active'] ? new Attribute(['class' => 'active']) : [],
];
}
/** @var \Drupal\oe_bootstrap_theme\MenuPreprocess $menu_preprocess */
$menu_preprocess = \Drupal::classResolver(MenuPreprocess::class);
$menu_preprocess->preprocessMenuLocalTasks($variables);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@

use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Link;
use Drupal\Core\Render\Markup;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Template\Attribute;
use Drupal\Core\Template\TwigExtension as CoreTwigExtension;
use Drupal\Core\Url;
use Drupal\oe_bootstrap_theme_helper\EuropeanUnionLanguages;
use Twig\Environment;
use Twig\Extension\AbstractExtension;
use Twig\Markup as TwigMarkup;
use Twig\TwigFilter;
use Twig\TwigFunction;

/**
* Collection of extra Twig extensions as filters and functions.
Expand Down Expand Up @@ -63,6 +69,17 @@ public function getFilters(): array {
];
}

/**
* {@inheritdoc}
*/
public function getFunctions(): array {
return [
new TwigFunction('bcl_link', [$this, 'bclLink'], [
'needs_environment' => TRUE,
]),
];
}

/**
* Twig filter callback for 'bcl_card_list'.
*
Expand Down Expand Up @@ -235,4 +252,37 @@ public function toInternalLanguageId(string $language_code): string {
return $language_code;
}

/**
* Alter a link with BCL logic.
*
* @param \Twig\Environment $env
* The env.
* @param string $label
* The link text for the anchor tag as a translated string.
* @param \Drupal\Core\Url|string $path
* The URL object or string used for the link.
* @param array|\Drupal\Core\Template\Attribute $attributes
* An optional array or Attribute object of link attributes.
*
* @return array
* The link render array.
*/
public function bclLink(Environment $env, $label, $path, Attribute $attributes): array {
if (is_string($path)) {
// The text has been processed by twig already, convert it to a safe
// object for the render system.
if ($label instanceof TwigMarkup) {
$label = Markup::create($label);
}
return [
'#type' => 'html_tag',
'#tag' => 'a',
'#value' => $label,
'#attributes' => $attributes->setAttribute('href', $path),
];
}

return $env->getExtension(CoreTwigExtension::class)->getLink($label, $path, $attributes);
}

}
Loading

0 comments on commit f5b0c71

Please sign in to comment.