Hello
For an effect on menu, I need to insert an attribute dynamically to all my menu links like this
going from :

<a href="xx" >Home</a>

to

<a href="xx" data-hover="Home">Home</a>

applying this pattern to each item.
Where could this be done ? (in php... On the server side without using js.)
I've tried different things...
- In page.php.tpl there is only print $content...
- MODULE_menu_link isn't the right place as there is still no html there, only node/5 for the path for instance...
where the html markup for links is added ?

Comments

vijaythummar’s picture

You can write something like below


function THEMENAME_links__system_main_menu($variables) {
  
  $output = '';
  foreach ($variables['links'] as $link) {
    $link['attributes']['data-hover'] = 'test';
    $output .= l($link['title'], $link['href'], $link);
  }
  return $output;
}

May this will help you !

Thanks,
Vijay Thummar

artatum’s picture

You saved my whole life !
Thx a lot, I'll check this out !
:-)

artatum’s picture

Is there a way to do this into a custom module ?

Jaypan’s picture

This is a theme override, and as such should be done in the theme, not in a module.

Edit: by the way, two of the links in your signature are dead. They lead to 403 pages.

artatum’s picture

(thx for the warning, i'll check it out)
About my question, I found an idea here, by registering a theme function in the module :
hook_menus_theme($existing, $type, $theme, $path)
But I dont find this hook on drupal.org. And I dont know where I could find my links into those variables...

othermachines’s picture

The hook referred to in that post is hook_theme(). 'menus' is part of the module name.

https://api.drupal.org/api/drupal/modules%21system%21system.api.php/func...

artatum’s picture

shame on me:O
thx:)

artatum’s picture

I cant believe we cant edit a menu link from a module !
Though I'm still rapping my head on it :/
Do you confirm othermachines ?

I 'm trying this way now:

function frontUX_theme($existing, $type, $theme, $path) {
  return array(
    'links__system_main_menu' => array(
      'variables' => array('inline' => FALSE),
                'function' => 'frontUX_main_menu',
    ),

  );
}
function frontUX_main_menu($args) {
...
vijaythummar’s picture

I know this may be dirty solution but will help you:

function MODULE_theme_registry_alter(&$theme_registry) {
	if (!empty($theme_registry['links']['function'])) {
		$theme_registry['links']['function'] = 'MODULE_theme_links';
	}
}

function MODULE_theme_links($variables) {
      $output = '';
      if ($variables['attributes']['id'] == "main-menu-links") {
          foreach ($variables['links'] as $link) {
            $link['attributes']['data-hover'] = 'test123132';
            $output .= l($link['title'], $link['href'], $link);
          }
      }
      return $output;
}

Thanks,
Vijay Thummar

artatum’s picture

there is no ['links'] into $variable. And not id in attributes.
only $variable['attributes']['class'][0]
if ($variables['attributes']['id'] == "main-menu-links")
raise an error.

othermachines’s picture

@artatum - There are really no "can'ts" when it comes to customizing with Drupal, but some things are trickier than others depending on your experience/skill level. I recommend taking some time to read up on and understand Drupal hooks - theme hooks in particular. It does take a bit of effort to wrap your head around it, but once you do it makes everything so much easier. Good luck!

artatum’s picture

it's been years I read. I bought every book, read the full web, subscribed at buildamodule and drupalize.
But I'm on this problem since days...
Sometimes you need help. A direction at least...
Everything I found speak about theme, templates, preprocessing : but I'm making a module to enhance links with graphic transitions. And I found very few things about theme ('links', ...

Jaypan’s picture

It's simply an override of the theme_links() function, and follows the same principles of all theme overrides.

What are you stuck on? I'm not clear on that.

artatum’s picture

@jaypan : I need to build a module : theme_links() is to be used in template.php ...
At this time, my module only contains the above code suggested by Vijay Thummar.

At the first call, in my debbugger $variables is like this

NAME-----------VALUE                     
$variables
  ["links"]                           size = 4
  ["attributes"]                    size = 0
    ["class"]                        size = 1
         ["0"]                        "menu"
         ["1"]                 "primary-menu"
         ["2"]                        "clearfix"
  ["headings"]                    size = 3
    ["text"]                    "Menu principal"
    ["level"]                          "h2"
    ["class"]                        size = 1
         ["0"]                   "element-invisible"
  ["theme_hook_suggestions"]  size = 0
       

at the second call,
$variables['attributes']['class"]["1"] becomes "secondary-menu"

the third time it's equal to "inline"
-----------------------
1 - hence the assertion
($variables['attributes']['id']== "main-menu-links")
makes no sense...

2 - It's called 3 times as you see, and each time passing a menu and not links from these menus...

3 - I dont understand the naming of the function :/

vijaythummar’s picture

It gives empty because It depends on how you call your theme function. You may not passing proper attributes in theme function.

Your function should like below

        print theme('links__system_main_menu', array(
          'links' => $main_menu,
          'attributes' => array(
            'id' => 'main-menu-links',
            'class' => array('links', 'clearfix'),
          ),
          'heading' => array(
            'text' => t('Main menu'),
            'level' => 'h2',
            'class' => array('element-invisible'),
          ),
        ));

Thanks,
Vijay Thummar

artatum’s picture

Sorry I dont see where your snippet goes ?
I'm using your 2 functions now ...

artatum’s picture

Well, it looks like AdaptiveTheme-Codex is faulty : as soon as I get back to Bartik, the $variables['links'] comes filled up with ... links.