Tabs template not preserving classes

Created on 29 March 2023, over 1 year ago

Problem/Motivation

I tried to add some extra classes to the node edit local task in a preprocess hook, but it was not rendered.
Switching to default Olivero theme rendered it properly.
Figured the twig template was not preserving the classes.

Steps to reproduce

/**
 * Implements hook_preprocess_menu_local_task().
 */
function MYMODULE_preprocess_menu_local_task(&$variables) {
  /** @var \Drupal\Core\Url $url */
  $url = $variables['link']['#url'];
  if ($url instanceof Url && $url->getRouteName() === 'entity.node.edit_form') {
    $variables['link']['#options']['attributes']['class'][] = 'content-wizard-enabled';
  }
}

Proposed resolution

Preserve existing classes in the twig template.

diff --git a/templates/BASE/components/00-theme/01-atoms/09-menu/tab/_tab.twig b/templates/BASE/components/00-theme/01-atoms/09-menu/tab/_tab.twig
index f4946b5..24c8af9 100755
--- a/templates/BASE/components/00-theme/01-atoms/09-menu/tab/_tab.twig
+++ b/templates/BASE/components/00-theme/01-atoms/09-menu/tab/_tab.twig
@@ -21,6 +21,9 @@
     'tabs__link--local-tasks'
   ]
 %}
+{% if link['#options']['attributes']['class'] is defined %}
+  {% set link_classes = link_classes|merge(link['#options']['attributes']['class']) %}
+{% endif %}
 
 {# if Pattern Lab atoms example, add ul wrapper #}
 {% if example %}

Remaining tasks

Create a fork / patch.

🐛 Bug report
Status

Needs review

Version

3.8

Component

Code

Created by

🇷🇴Romania reszli Târgu Mureș

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024