Hello,

I am looking for a way to customize 'edit', 'view', 'delete' links.

I use bootstrap theme, but i use bootstrap v4 styles for it. I want to add 'nav-link' class to each link.

And find a way to display somewhere, not just below post title.

I couldnt find a tutorial about it. Where should i look for this? template parts (twig) or themename.theme file?

Comments

Jeff Burnz’s picture

And find a way to display somewhere, not just below post title.

In Drupal 8 local tasks are a block, you can place them anywhere you want.

Is 'nav-link' meant to go on the anchor or the list item? There is a template menu-local-task.html.twig where you can easily add classes to the list item.

weboloper’s picture

hello, i want to add classes to anchors.

<ul>
  <li><a class="something">edit</a></li>
</ul>
Jeff Burnz’s picture

Use preprocess, I can never seem to get setOption to work in templates...

/**
 * Preprocess variables for the menu local task template.
 * @param $variables
 */
function HOOK_preprocess_menu_local_task(&$variables) {
  $variables['element']['#link']['url']->setOption('attributes', array('class' => array('nav-link')));
}
kay.beissert’s picture

I need some Icons for my local task links. Therefor I need unique classes or ids for the local task items. I know, I can add classes in menu-local-task.html.twig. But how can I make them unique?

Thank you

bohemier’s picture

I'm trying to do the same thing here... there does not seem to be any variable that hold a unique ID for each local task. My first approach was to output the title as a classname in the theme hook, ie: 

$title = \Drupal\Component\Utility\Html::getClass($variables['element']['#link']['title']);

but in a multilingual site that title changes...

J-F Bohémier
Angelicode.com

Anonymous’s picture

Sorry if it has been answered elsewere, but this worked for me to add "nav-link" to each link.

I've added it to the THEMENAME.theme file:

function THEMENAME_preprocess_menu_local_task(&$variables) {
  $variables['link']['#options']['attributes']['class'][] = 'nav-link';
}
cesarg’s picture

Thanks for sharing, just what I needed.

harishst’s picture

I have created a preprocess hook for menu-local-task. And altered the #link and its attributes.

/**
 * Implements hook_preprocess_menu_local_task
 * Prepares variables for the menu-local-task.html.twig
 *
 * @param array $variables
 */
function hook_preprocess_menu_local_task(&$variables) {
    $variables['link']['#title'] = $variables['element']['#link']['title'];
    $variables['link']['#options']['attributes']['class'][] = 'nav-link';
    if (!empty($variables['element']['#active'])) {
        $variables['link']['#options']['attributes']['class'][] = 'active';
    }
}

Hope this helps someone.