yannickoo / menu_link_attributes Goto Github PK
View Code? Open in Web Editor NEW:droplet: Contains the Menu Link Attributes module for Drupal.
Home Page: https://www.drupal.org/project/menu_link_attributes
:droplet: Contains the Menu Link Attributes module for Drupal.
Home Page: https://www.drupal.org/project/menu_link_attributes
Added Capability for Additional Field Types, ex: textarea
Link to bitbucket patch: https://bitbucket.org/!api/2.0/snippets/texascreative/yoeRr/1be075a526b0e53cae6956b444af8c3091938649/files/textarea_for_type-1-1.patch
Currently you see a warning after saving, it's only a warning but we should fix it :)
Warning: Creating default object from empty value in Drupal\menu_link_content\Plugin\Menu\MenuLinkContent->updateLink() (line 220 of core/modules/menu_link_content/src/Plugin/Menu/MenuLinkContent.php).
Attributes are not translatable. For example using xml:lang will set the translation variable as the same for all translations
I'm using the D8 Menu Link Attributes module to add an attribute to a menu item using the yml configuration offered by the module ex:
attributes:
title:
label: 'Tooltip text'
type: file
The code Above works fine, but the file is always downloaded to a temporary location, I have tried to set upload_location key but it seems not working :
attributes:
image2:
label: Image2
type: managed_file
upload_location: 'public://images/'
It would be great if you could access menu link attributes from the main admin without having to go through an actual menu link to get to it.
Along with that, would it be possible to stop redirecting on save? Makes adding multiple attributes a little tedious.
Hi,
I'd like to add the 'download' attribute to a link. The attribute doesn't have a value, but looks like this for example: <a href="doc.pdf" download>Link</a>
I've found the editor at /admin/config/menu_link_attributes/config, but haven't had any luck setting this up.
Hi!
I was looking for it because I added In my `THEME_preprocess_menu` this attributes as array and got this error `Error: [] operator not supported for strings` after enabling module `menu_link_attributes'
I used this code:
```
$attrs = $item['url']->getOption('attributes');
// Error on this line and $attrs['class'] = "btn btn-primary text-light"; (I was set it on the menu option)
$attrs['class'][] = 'nav-link';
```
I'm not sure that I assign the attribute class correctly as an array, but stackoverflow says so :)
I read the documentation, but I can't seem to get it to work. This is my YAML file. Then after I use the form to add some classes to that menu item, the classes don't get added to the <li>. The class for the <a> shows up though so I know that's working.
attributes:
container_class:
label: 'Class(es) to add to container <li>'
description: ''
class:
label: 'Class(es) to add to the link <a>'
description: ''
target:
label: ''
description: ''
options:
_blank: 'New window (_blank)'
_self: 'Same window (_self)'
After enabling "admin_toolbar_links_access_filter" (a submodule of the forthcoming 1.21 admin_toolbar module), it's not possible anymore to login as admin. The Drupal site crashes. The reason seems to be that "original_link" sometimes doesnt exist. Independent of this specific incompatiblity, I think it's good style anyway to check whether $item["original_link"] is not empty. So I'm proposing the following patch:
diff -ur menu_link_attributes.bak/menu_link_attributes.module menu_link_attributes/menu_link_attributes.module
--- menu_link_attributes.bak/menu_link_attributes.module 2016-07-01 10:27:41.000000000 +0200
+++ menu_link_attributes/menu_link_attributes.module 2017-11-17 15:22:44.689375000 +0100
@@ -114,17 +114,19 @@
*/
function menu_link_attibutes_set_attributes($items) {
foreach ($items as &$item) {
- $menu_link_attributes = menu_link_attributes_get_attributes($item['original_link']);
+ if (!empty($item['original_link'])) {
+ $menu_link_attributes = menu_link_attributes_get_attributes($item['original_link']);
- if (count($menu_link_attributes)) {
- $url_attributes = $item['url']->getOption('attributes') ?: [];
- $attributes = array_merge($url_attributes, $menu_link_attributes);
+ if (count($menu_link_attributes)) {
+ $url_attributes = $item['url']->getOption('attributes') ?: [];
+ $attributes = array_merge($url_attributes, $menu_link_attributes);
- $item['url']->setOption('attributes', $attributes);
- }
+ $item['url']->setOption('attributes', $attributes);
+ }
- if (!empty($item['below'])) {
- menu_link_attibutes_set_attributes($item['below']);
+ if (!empty($item['below'])) {
+ menu_link_attibutes_set_attributes($item['below']);
+ }
}
}
}
Drupal 8 allows you to create empty links, without any page behind.
You simply have to leave the link field empty.
But whenever you edit this menu link, menu_link_attributes will raise these errors:
Notice: Trying to get property of non-object in menu_link_attributes_menu_link_content_form_submit() (line 99 of modules/contrib/menu_link_attributes/menu_link_attributes.module).
Warning: array_merge(): Argument #1 is not an array in menu_link_attributes_menu_link_content_form_submit() (line 100 of modules/contrib/menu_link_attributes/menu_link_attributes.module).
Warning: Creating default object from empty value in menu_link_attributes_menu_link_content_form_submit() (line 100 of modules/contrib/menu_link_attributes/menu_link_attributes.module).
Related lines:
$menu_link_options = $menu_link->link->first()->options;
$menu_link->link->first()->options = array_merge($menu_link_options, $options);
$menu_link->save();
I currently avoid errors with this code:
$link = $menu_link->link->first();
if ($link) {
$menu_link_options = $link->options;
$link->options = array_merge($menu_link_options, $options);
$menu_link->save();
}
But this does not fix the problem. We need to find a way to add attributes on empty links.
And if this is not possible, the attributes block should at least be disabled for these links.
function menu_link_attibutes_set_attributes must be menu_link_attributes_set_attributes.
Any chance of getting support for adding the onclick attribute? The "Menu attributes" module does this for Drupal 7 and I'm looking for a solution for Drupal 8.
Hi,
Not setting the Target attribute to a value leads to a w3c html validation error: Bad value for attribute target on element a: Browsing context name must be at least one character long.
If possible, I would like to suggest adding a default option to the target attribute in the menu_link_attributes.config.yml file and perhaps even setting this value to _self to avoid this validation error.
Is this something that would be doable for the menu link attributes module, or is this module going to fully merge into the menu attributes module?
Sincerely,
LaravZ
PS. My bad, this seems to be a duplicate of issue #11.
Hi new to d8, is there a way to print the attributes created with this modules in menu.html.twig?
For example, I have an attribute like this:
material-icon:
label: 'Material Icon'
description: ''
When this field is provided, the menu link is rendered like this:
<a href="/" material-icon="home" data-drupal-link-system-path="<front>" class="is-active">Home</a>
I would like to do something like this in my template:
{% if item.attributes.material_icon is defined %}
<i class="material-icons">{{ item.material_icon }}</i>
{% endif %}
How do I get something like this into my template? So far, I cannot get that <i>
tag to render in menu.html.twig. What is the best way to use attributes created with this module?
Thanks in advance for your help and an awesome module :)
After updating to 1.0 stable, custom classes that have been added to menu items are not being rendered on menu items when viewing the site.
Because there is no specified field character value, it gets Drupals inherited 128 max value
You will see that the 3rd level item doesn't get the container attributes.
I traced the problem back to the array_merge call, and the attached patch fixes the problem for me. It seems array_merge (and array_merge_recursive) doesn't solve the merge of multidimensional arrays correctly.
Sometimes it can be useful to provide a default value for an attribute. For example, if somehow you want links to have target="_blank"
by default.
We have one site that uses this module and we have recurring warning and notice on logs:
Warning: Invalid argument supplied for foreach() in menu_link_attributes_preprocess_menu()
Notice: Undefined index: attributes in menu_link_attributes_preprocess_menu()
In order to prevent this, we've created a patch for it:
index 15bc4fa5d..2e8aeaa93 100644
--- a/web/modules/contrib/menu_link_attributes/menu_link_attributes.module
+++ b/web/modules/contrib/menu_link_attributes/menu_link_attributes.module
@@ -189,7 +189,7 @@ function menu_link_attributes_preprocess_menu(&$variables) {
// After updating the module, some classes stop working.
// So, based on the old version of the module we've put this else.
// It should have an else to set the class into element that already has another classes set
- else {
+ elseif (isset($options['attributes'])) {
// Foreach to see if there is more than one attribute set.
foreach ($options['attributes'] as $attribute => $value) {
// Take the options that are already there.
Hello, I am the maintainer of the Barrio Theme [https://www.drupal.org/project/bootstrap_barrio].
Attributes are not being printed on menus using this theme.
The template for the main menu is pretty standard just adding Bootstrap marckup.
I wonder if I am missing something here to make your module work.
Attached code:
{#
/**
- @file
- Bootstrap Barrio's override to display a menu.
*
- Available variables:
- \- menu_name: The machine name of the menu.
- \- items: A nested list of menu items. Each menu item contains:
- - attributes: HTML attributes for the menu item.
- - below: The menu item child items.
- - title: The menu link title.
- - url: The menu link url, instance of \Drupal\Core\Url
- - localized_options: Menu link localized options.
- - is_expanded: TRUE if the link has visible children within the current
- menu tree.
- - is_collapsed: TRUE if the link has children within the current menu tree
- that are not currently visible.
- - in_active_trail: TRUE if the link is in the active trail.
*/
#}
{% import _self as menus %}
{#
We call a macro which calls itself to render the full tree.
@see http://twig.sensiolabs.org/doc/tags/macro.html
# }
{{ menus.menu_links(items, attributes, 0) }}
{% macro menu_links(items, attributes, menu_level) %}
{% import _self as menus %}
{% if items %}
{% if menu_level == 0 %}
<ul{{ attributes.addClass('nav navbar-nav') }}>
{% else %}
<ul class="dropdown-menu">
{% endif %}
{% for item in items %}
{%
set classes = [
menu_level ? 'dropdown-item' : 'nav-item',
item.is_expanded ? 'menu-item--expanded',
item.is_collapsed ? 'menu-item--collapsed',
item.in_active_trail ? 'active',
item.below ? 'dropdown',
]
%}
<li{{ item.attributes.addClass(classes) }}>
{%
set link_classes = [
not menu_level ? 'nav-link',
item.in_active_trail ? 'active',
]
%}
{% if item.below %}
{{ link(item.title, item.url, {'class': link_classes, 'data-target': '#', 'data-toggle': 'dropdown', 'aria-expanded': 'false'}) }}
{{ menus.menu_links(item.below, attributes, menu_level + 1) }}
{% else %}
{{ link(item.title, item.url, {'class': link_classes}) }}
{% endif %}
</li>
{% endfor %}
</ul>
{% endif %}
{% endmacro %}
Hello,
thank you for this module,
Sorry if i'm not understandable, this is my first issue report on GitHub!
When i enable your module, and then try to access a Views page (whose link is provided by Views) here is the message i get:
The website encountered an unexpected error. Please try again later.
Drupal\Component\Plugin\Exception\PluginNotFoundException: The "views_view" entity type does not exist. in Drupal\Core\Entity\EntityTypeManager->getDefinition() (line 130 of core/lib/Drupal/Core/Entity/EntityTypeManager.php).
Drupal\Core\Entity\EntityRepository->loadEntityByUuid('views_view', 'views.nous_sommes_cornoualia.page_1')
Drupal\Core\Entity\EntityManager->loadEntityByUuid('views_view', 'views.nous_sommes_cornoualia.page_1')
menu_link_attributes_get_attributes(Object)
menu_link_attributes_preprocess_menu(Array, 'menu__main', Array)
Drupal\Core\Theme\ThemeManager->render('menu__main', Array)
Drupal\Core\Render\Renderer->doRender(Array, )
Drupal\Core\Render\Renderer->render(Array)
Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1)
__TwigTemplate_0d55fa8dc9e0732b6e892d7962282aac2736f6a4c869c6ce6bc980b75bbb7d21->block_content(Array, Array)
Twig_Template->displayBlock('content', Array, Array)
__TwigTemplate_0d55fa8dc9e0732b6e892d7962282aac2736f6a4c869c6ce6bc980b75bbb7d21->doDisplay(Array, Array)
Twig_Template->displayWithErrorHandling(Array, Array)
Twig_Template->display(Array)
Twig_Template->render(Array)
twig_render_template('core/themes/stable/templates/block/block--system-menu-block.html.twig', >Array)
...
Furthermore, when i try to edit the menu link, no attribute is available...
Did i miss something ?
Thank you
Romain
Module is missing schema.yml for it's configuration object(s).
Hi,
Thanks for this module.
I got the following error when I enabled class and target attributes for one of my menus, which breaks all the site. I used Drupal 8.1.1 and 8.x-1.0-beta3
Notice: Trying to get property of non-object in menu_link_attributes_get_attributes() (line 161 of modules/contrib/menu_link_attributes/menu_link_attributes.module).
menu_link_attributes_get_attributes(Object) (Line: 117)
menu_link_attibutes_set_attributes(Array) (Line: 106)
menu_link_attributes_preprocess_menu(Array, 'menu', Array) (Line: 287)
Drupal\Core\Theme\ThemeManager->render('menu__social_icons', Array) (Line: 435)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 195)
Drupal\Core\Render\Renderer->render(Array) (Line: 462)
Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1) (Line: 90)
__TwigTemplate_da7679a3aa78f3b22675fb93b558684e79257bd67a145d756de1548ea8ecbebf->block_content(Array, Array) (Line: 167)
Twig_Template->displayBlock('content', Array, Array) (Line: 79)
__TwigTemplate_da7679a3aa78f3b22675fb93b558684e79257bd67a145d756de1548ea8ecbebf->doDisplay(Array, Array) (Line: 381)
Twig_Template->displayWithErrorHandling(Array, Array) (Line: 355)
Twig_Template->display(Array) (Line: 366)
Twig_Template->render(Array) (Line: 64)
twig_render_template('core/modules/system/templates/block--system-menu-block.html.twig', Array) (Line: 384)
Drupal\Core\Theme\ThemeManager->render('block', Array) (Line: 435)
Drupal\Core\Render\Renderer->doRender(Array) (Line: 448)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 195)
Drupal\Core\Render\Renderer->render(Array) (Line: 462)
Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1) (Line: 112)
__TwigTemplate_e7fe9250234a75e050125bb3e082af4ac4b40b98247e095b2cc864f7f598b98f->block_navbar(Array, Array) (Line: 167)
Twig_Template->displayBlock('navbar', Array, Array) (Line: 59)
__TwigTemplate_e7fe9250234a75e050125bb3e082af4ac4b40b98247e095b2cc864f7f598b98f->doDisplay(Array, Array) (Line: 381)
Twig_Template->displayWithErrorHandling(Array, Array) (Line: 355)
Twig_Template->display(Array) (Line: 366)
Twig_Template->render(Array) (Line: 64)
twig_render_template('sites/all/themes/ajmn/templates/page.html.twig', Array) (Line: 384)
Drupal\Core\Theme\ThemeManager->render('page', Array) (Line: 435)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 195)
Drupal\Core\Render\Renderer->render(Array) (Line: 462)
Drupal\Core\Template\TwigExtension->escapeFilter(Object, Array, 'html', NULL, 1) (Line: 90)
__TwigTemplate_304239c5cd33d7c4ffd842c59df32edc834eccc6934b356c6fce6bae668d789c->doDisplay(Array, Array) (Line: 381)
Twig_Template->displayWithErrorHandling(Array, Array) (Line: 355)
Twig_Template->display(Array) (Line: 366)
Twig_Template->render(Array) (Line: 64)
twig_render_template('sites/all/themes/bootstrap/templates/system/html.html.twig', Array) (Line: 384)
Drupal\Core\Theme\ThemeManager->render('html', Array) (Line: 435)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 195)
Drupal\Core\Render\Renderer->render(Array) (Line: 147)
Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 574)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 148)
Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object) (Line: 111)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.view', Object) (Line: 144)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 62)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 98)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 77)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
Drupal\services\StackMiddleware\FormatSetter->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 50)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 628)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
Hello,
I've added attributes through the Drupal UI but when I render that menu in a custom module, the attributes are not included:
$menu_tree = \Drupal::menuTree();
$menu_name = 'main';
// Build the typical default set of menu tree parameters.
$parameters = $menu_tree->getCurrentRouteMenuTreeParameters($menu_name);
// Load the tree based on this set of parameters.
$tree = $menu_tree->load($menu_name, $parameters);
// Transform the tree using the manipulators you want.
$manipulators = array(
// Only show links that are accessible for the current user.
array('callable' => 'menu.default_tree_manipulators:checkAccess'),
// Use the default sorting of menu links.
array('callable' => 'menu.default_tree_manipulators:generateIndexAndSort'),
);
$tree = $menu_tree->transform($tree, $manipulators);
// Finally, build a renderable array from the transformed tree.
$menu = $menu_tree->build($tree);
Do I need into include the preprocessor in this module in my custom module as well or is there a manipulator that I can include above?
An optional second pipe (|
) in the attribute lines would be great for setting a description text which can be very handy.
I have a scenario where menu attributes added to menu items do not render.
For example:
When I enable this module, and add an attribute to "Menu 2.1", and show it as expanded, the attribute does not display.
But, if I add an attribute to "Menu 3", the attribute displays properly.
It appears that this is a conflict between this module and Bootstrap...
-mike
Im trying to add a class to a new menu item but I believe I must not have the right syntax for the attributes option. All my other fields work below except the class attribute.
When creating/updating menu links programmatically how do you access the attributes properties?
$items = [
'link1' => [
'link' => 'internal:/page1',
'weight' => -6,
'expanded' => TRUE,
'status' => TRUE,
'attributes' => [
'class' => 'my-class'
],
],
];
foreach ($items as $name => $option) {
$menu_link = MenuLinkContent::create([
'title' => $name,
'link' => ['uri' => $option['link']],
'weight' => $option['weight'],
'menu_name' => 'my-menu',
'attributes' => [
'class' => $option['attributes']['class']
]
]);
$menu_link->save();
}
Hi
here the issue on drupal.org, let me know if it's not clear. https://www.drupal.org/project/default_content_deploy/issues/2926800
Hi,
I am using module Micon and Menu Link Attributes. On saving a menu link with a Micon icon and additional custom classes, the 'data-icon' is not Micon saved.
In menu_link_attributes_menu_link_content_form_submit
line 145 the $menu_link_options
and $menu_link_attributes
are merged.
Eg.
$menu_link_options = ['attributes' => ['data-icon' => 'icon']];
$menu_link_attributes = ['attributes' => ['class' => 'ico--d-none ico--d-lg ico--s-large ico-l--d-lg-none']];
array_merge()
of these two results in an array equal to $menu_link_attributes
I suggest replacing this with array_merge_recursive()
.
You can test the results at 3v4l.org: https://3v4l.org/ACQmG
Hello,
Thanks for your module.
I have noticed that below items are not affected by the module.
This could be fixed by calling a recursive function that sets attributes for one item inside the preprocess menu hook function inside the foreach loop.
Thank you.
I am trying to add the tabindex="0" attribute to a menu item.
While this works correctly for values other than 0 (e.g -1) if I enter 0 then the attribute is not added at all.
I have also tried to use 0 as a class and this also does not get added.
It seems that 0 is evaluated as null somewhere and therefore no attribute is added.
Had to restore a db backup.
The menu items still have their settings, but do not trigger new window UNLESS i open that menu link, do nothing, and save it.
I've cleared all caches, and even looked at the db.
I see the link attributes in the db table menu_link_content_data , and two links with identical link_options behave differently.
Link One : Opens in new tab (because I opened the link and saved)
Link 2: Does not open in new tab. (did not alter it, but you can see the _blank in the db)
The problem is in line #173 of menu_link_attributes.module:
// Remove empty attributes by checking their string length.
foreach ($attributes as &$attribute) {
if (!is_array($attribute) && Unicode::strlen($attribute) === 0) {
unset($attribute);
}
}
Unset on a reference doesnt work that way. Only the reference is "unreferenced" in this case. The original value stays the same. You need to unset the key:
// Remove empty attributes by checking their string length.
foreach ($attributes as $key=>$attribute) {
if (!is_array($attribute) && Unicode::strlen($attribute) === 0) {
unset($attributes[$key]);
}
}
The attributes will only be applied to the top-level menu links in a menu. It does not look to see if this top-level menu link has children. And if so, apply the same logic to them.
I made a patch that does just this.
index e6c2b3d..0e2b2f6 100644
--- a/menu_link_attributes.module
+++ b/menu_link_attributes.module
@@ -174,16 +174,34 @@ function menu_link_attributes_is_container_attribute($attribute_name) {
* Implements template_preprocess_menu().
*/
function menu_link_attributes_preprocess_menu(&$variables) {
- foreach ($variables['items'] as $item) {
+ menu_link_attributes_place_attributes($variables['items']);
+
+}
+
+/**
+ * The function that will assign the attributes to the correct menu link
+ *
+ * @param array $items
+ * The array of items assign the attributes to
+ *
+ */
+function menu_link_attributes_place_attributes($items) {
+ foreach ($items as $item) {
/** @var \Drupal\Core\Menu\MenuLinkDefault $menu_link */
$menu_link = $item['original_link'];
$options = $menu_link->getOptions();
+
// Apply container attributes on <li> element.
if ($options && isset($options['container_attributes'])) {
foreach ($options['container_attributes'] as $attribute => $value) {
$item['attributes']->setAttribute($attribute, $value);
}
}
+
+ // Rerun the script if the menu item has children
+ if ($item['below']) {
+ menu_link_attributes_place_attributes($item['below']);
+ }
}
}
Hi!
function menu_link_attributes_get_attributes produces mb_strlen warnings
mb_strlen() expects parameter 1 to be string, array given in
that's because the attributes['class'] will now, after your last change, always be an array.
The corrected version could be:
// Remove empty attributes by checking their string length.
foreach ($attributes as &$attribute) {
if (!is_array($attribute) && Unicode::strlen($attribute) === 0) {
unset($attribute);
}
}
Warning: explode() expects parameter 2 to be string, array given in menu_link_attributes_get_attributes() (line 149 of modules/menu_link_attributes/menu_link_attributes.module).
Solution:
Check if it was not array before continue, Here the diff line 148
`- if (isset($attributes['class'])) {
At the moment the attributes cannot be set for menu links configured from within the views interface
Hello, After install module I got this error
Fatal error: Call to a member function first() on null in /.../menu_link_attributes/menu_link_attributes.module on line 162
After debugging I notice something wrong in uuid "fded6395-d5f8-48d6-b708-cefebe737654:4",
I think the best and safe solution is add strstr to trim extra text if exist like
$uuid = strstr($uuid, ':', true);
I will create pull request fir this solution
Regards.
Currently we are using the beta3 version of the module, but we've updated it and some of menu items stopped working.
I've noticed this happened only for items that already had one class (that's no the one we've set on the menu link attribute).
But since it was working before the update, we compared the revisions and found out that on menu_link_attributes_preprocess_menu there wasn't any check for it anymore.
So, based on the beta3 revision, I've created a patch for it and now it's working properly.
menu_link_attributes_class_not_working.patch.txt
Hope it helps someone else.
When adding attributes to a child item - the item won't rendered with the attributes.
To overcome this, add an attribute to the parent item and it will work great.
Can you fix this in the next version?
The issue is in function menu_link_attributes_preprocess_menu(), the condition of count($menu_link_attributes) prevent processing child items of none-attributes parents.
We've noticed that there is no way to add an attribute to a link that a module has provided. For example, if you create a custom admin link in a module and add it to a user-facing menu where all other menu items have an icon, there's no way to add an icon to the link in the menu structure.
I can see why - the main form alter is on the base content link entity form, and links that are created in a module are not content link entites.
We tried to get around this quickly by adding an extra form alter to target the menu_link_edit form, but of course the code fails because there is no $form_state->getFormObject()->getEntity()
.
Then there would be no way to save that information because there is no menu link entity.
It would be good if there were a way to add attributes to module-provided links. My team's happy to help out with patches and testing where possible, after the Christmas break. Has there been any kind of initiative to work on this, or any half-worked code? Any help is appreciated.
Thank you!
Hi, I would very much like an option to add the class to the menu list item (<li>
), not the menu link (<a>
) itself.
In my case I have a nested menu where I would like to style the child li items based on the class of the parent, but this is not possible when the class is added to the link as the structure is:
In general adding it to the highest level makes styling a lot easier.
I realise this cannot be done for options like for instance target, and also that you can't break the current functionality, but maybe something like a checkbox "add to list item" for the class option could be feasible?
I want to have icons to my menu. After viewing this module I installed it and added a class to my link.
Now how can I place the attribute to my menu in the menu.html.twig page?
When one does not choose any attribute for the current menu item the default is an array containing the key 'attributes' and NULL as value. This breaks in LinkGenerator::generate().
In your example a select drop-down is provided for target
but if nothing is selected it prints the attribute anyways. Because some attributes are allowed exist without values maybe we can add a configurable option to the yaml config to not print the attribute when it doesn't have any values?
Is it possible to attributes via a custom menu links yml???
for example
test:
title: 'test'
url: 'internal:/test/'
menu_name: main
weight: 11
attributes:
target:
options:
_blank: 'New window (_blank)'
I'm using the D8 Menu Link Attributes module to add an attribute to a menu item using the yml configuration offered by the module ex:
attributes:
title:
label: 'Tooltip text'
type: file
The code Above works fine, but the file is always downloaded to a temporary location, I have tried to set upload_location key but it seems not working :
attributes:
image2:
label: Image2
type: managed_file
upload_location: 'public://images/'
I have a all list of error when I triy to give an attribut.
Notice: Array to string conversion in Drupal\bootstrap\Utility\Attributes->addClass() (line 29 of themes\contrib\bootstrap\src\Utility\Attributes.php).
Notice: Array to string conversion in Drupal\bootstrap\Utility\Attributes->getClasses() (line 59 of themes\contrib\bootstrap\src\Utility\Attributes.php).
Notice: Array to string conversion in Drupal\bootstrap\Utility\Attributes->addClass() (line 29 of themes\contrib\bootstrap\src\Utility\Attributes.php).
Warning: explode() expects parameter 2 to be string, array given in twig_split_filter() (line 770 of vendor\twig\twig\lib\Twig\Extension\Core.php).
Notice: Array to string conversion in Drupal\Core\Template\AttributeArray->__toString() (line 73 of core\lib\Drupal\Core\Template\AttributeArray.php).
Notice: Array to string conversion in Drupal\Core\Template\AttributeArray->__toString() (line 74 of core\lib\Drupal\Core\Template\AttributeArray.php).
I tried to apply your Patch #60 , no change.
If i leave the attribut blank, it's ok.
But I need a class on the LI element. I did exactly like your example,
attributes:
container_class:
label: 'Container class'
description: 'Enter a class for the wrapping li
element.'
The result is
<li class="Array class section_enfant expanded dropdown">
That Array at the start of the class is an error...
Do you have any idea?
Drupal 8.6.5 on XAMP, PHP7.2
Hello there,
Been testing your module working perfect for the menus i've created, would like to ask whether there is a way to add attributes for user_account_menu like my account or logout menus ?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.