I am trying to add custom class to the link ('a' tag) on menu local task with no success.
This is the code used:

<?php
{#
/**
 * @file
 * Bootstrap Barrio's Theme override for a local task link.
 *
 * Available variables:
 * - attributes: HTML attributes for the wrapper element.
 * - is_active: Whether the task item is an active tab.
 * - link: A rendered link element.
 *
 * Note: This template renders the content for each task item in
 * menu-local-tasks.html.twig.
 *
 * @see template_preprocess_menu_local_task()
 */
#}
<li{{ attributes.addClass(is_active ? 'active', 'nav-item') }}>{{ link(item.title, item.url, {'class' : 'nav-link' }) }}</li>
?>

Throwing this error:

[Fri Nov 13 02:22:39.011213 2015] [:error] [pid 2800:tid 1140] [client ::1:52515] Uncaught PHP Exception InvalidArgumentException: "The URI '' is invalid. You must use a valid URI scheme." at C:\\Bitnami\\drupal-8.0.0.rc2-0\\apps\\drupal\\htdocs\\core\\lib\\Drupal\\Core\\Url.php line 301, referer: http://localhost/drupal/node/2
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

hatuhay created an issue. See original summary.

hatuhay’s picture

Issue summary: View changes
dawehner’s picture

Category: Support request » Task
Status: Active » Needs review
FileSize
1.41 KB

I'm curious whether this is a BC compatible change ...

Status: Needs review » Needs work

The last submitted patch, 3: 2614628-3.patch, failed testing.

Jmdrawneek@googlemail.com’s picture

Status: Needs work » Needs review

Not too sure why this is failing. I've applied it locally and it seemed to apply fine.

hatuhay’s picture

Apply the patch.
This works OK.

<?php
<li{{ attributes.addClass(is_active ? 'active', 'nav-item') }}>{{ link }}</li>
?>
?>

This one throws same error "The URI '' is invalid. You must use a valid URI scheme."

<?php
<li{{ attributes.addClass(is_active ? 'active', 'nav-item') }}>{{ link(item.title, item.url, {'class' : 'nav-link' }) }}</li>
?>
?>
joelpittet’s picture

Version: 8.0.0-rc3 » 8.0.x-dev
Issue tags: +Twig

Seems to have passed. Moving to 8.0. and tagging.

This issue looks handy:)

hatuhay’s picture

Yes, and it is also needed for menu local actions.
But please, the patch still throw errors.

joelpittet’s picture

Title: Add custom class to menu local task » Add custom class to menu local task and local actions
Category: Task » Feature request
Status: Needs review » Needs work

What errors does the patch throw? It looks like a Proof of Concept but it doesn't look to throw errors.

Setting to needs work to tackle both local tasks and local actions.

hatuhay’s picture

This code applied on template_preprocess_menu_local_task()

<?php
<li{{ attributes.addClass(is_active ? 'active', 'nav-item') }}>{{ link(item.title, item.url, {'class' : 'nav-link' }) }}</li>
?>

Throw this error:
"The URI '' is invalid. You must use a valid URI scheme."

joelpittet’s picture

Title: Add custom class to menu local task and local actions » menu local task and local actions

@hatuhay The error has nothing to do with the class it's to do with item.url It's saying that empty string '' is not a valid URL.

Meaning that item.url doesn't exist. @dawehner has been a bit clever and used the Link object which has access to some of those properties which I believe is a brilliant approach.

joelpittet’s picture

Title: menu local task and local actions » menu local task and local actions are missing link context variables

Whoops was middle of clarifying the title a bit.

hatuhay’s picture

OK I got it, will test later today.
Think that this has a semantic issue.
The link object confuse me with the link() function.
To many info in short time.
This is tough to do, so do not expect to be done but my believe is that link object should be renamed to item.

dawehner’s picture

Status: Needs work » Needs review
FileSize
2.01 KB
768 bytes

@hatuhay
You used the template in the wrong way:

<li{{ attributes.addClass(is_active ? 'active', 'nav-item') }}>{{ link(link.text, link.url, {'class' : 'nav-link' }) }}</li>

though works.

This patch here also expands to local actions

Status: Needs review » Needs work

The last submitted patch, 14: 2614628-14.patch, failed testing.

joelpittet’s picture

@dawehner Unfortunately the Link object doesn't allow setting Attributes or any other link #options. That may need to be a follow-up (if it doesn't exist) maybe for now we can just provide the template with some extra context variables?

Your approach seems ideal but maybe a bit of a stretch unless we can get the Link object a bit more useful.

joelpittet’s picture

I may be crazy but here's making Link take attributes. A bit of a WIP and needs tests but may suffice.
#2615660: Allow Link and LinkGenerator::generate() to provide attributes.

hatuhay’s picture

Thanks for the support.
While patch been applied to Core, use this workaround:

<?php
use Drupal\Core\Link;
...
function bootstrap_barrio_preprocess_menu_local_task(&$variables) {
  $link = $variables['element']['#link'];
  $url = $link['url'];
  $options = $url->getOptions();
  $url->setOptions($options + $link['localized_options']);
  $variables['item'] = Link::fromTextAndUrl($link['title'], $url);
}
?>

Then render the item variable

{%
  set classes = [
    'nav-link',
    is_active ? 'active',
  ]
%}
<li{{ attributes.addClass(is_active ? 'active', 'nav-item') }}>{{ link(item.text, item.url, {'class': classes}) }}</li>
joelpittet’s picture

Totally, that is a good work around:)

joelpittet’s picture

Version: 8.0.x-dev » 8.1.x-dev

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.0-beta1 was released on March 2, 2016, which means new developments and disruptive changes should now be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.