I'm need to modify some links as they are added to the menu_links table, but the menu_link_alter hook does not seem to be running. Maybe I'm not understanding when the hook is called or I need to enable it somehow?

I've tried echoing and logging with no results. Also having the same problem with menu_alter.


function mymodule_menu_link_alter(&$item,$menu) {
	echo "link alter";
        $fh= fopen('/mylogs/log.txt','w');
	fwrite($fh,"link alter");
	fclose($fh);
}


Comments

bkelly9376’s picture

Well... it just started working. Not sure what was going on there. Do I need to clear cache after modifying a modules code?

jaypan’s picture

Menus are cached, so any time you make changes to either hook_menu or hook_menu_alter, you need to clear the cache before those changes will be seen.

Contact me to contract me for D7 -> D10/11 migrations.

zebadix’s picture

Can anyone explain this to me? As I cannot get this to work?

function MYMODULENAME_menu_link_alter(&$item, $menu){
if($item['link_path'] == 'user/%/profile/uprofile'){
$item['weight'] = 3;
}
}

I have rebuilt the menus and flushed the cache

I have even tried placing firep('hello from the menu_link_alter function'); in the function but this is not showing up?

I basically don't understand what is going on? I have the following in the same module and it works.

function MYMODULENAME_menu() {

$items['node/%node_gallery_gallery/delete'] = array(
'title' => 'Delete',
...
);

So what's the difference? Please help!

Thanks

bba99’s picture

Any luck with this? I'm having a similar issue.

coolboygfx’s picture

If the menu was customized by the administrator at ...admin/build/menu-customize/navigation
then in drupal db at table: menu_links -> column: customized, the coresponding link takes value of 1. Thus you cannot alter menu link via hook_menu_link_alter().
In order to use this hook that link must be reseted by clicking the link 'reset' or never being touched by admin.

What is done by administrator in administration pages gain priority over hooks(that's my conclusion).

Also read this

P.S. Anyway, that function does not want to echo anything to prove it's working.