The reason you can’t do this with hook_menu_alter is that this method does not create the menu on the fly; as the menu is built and stored in cache.
Here in my code I’ve got a calculation that returns a NID that I want to replace with the default one that I have in the menu item.
So I’ve added this to the body class list and retrieve it in the Javascript.
/*
* Implements hook_preprocess_html()
*/
function MODULE_preprocess_html(&$variables){
global $user;
drupal_add_js(drupal_get_path('module', 'MODULE') . '/js/MODULE.menu.overrides.js');
// the method here calculates what Nid I need to direct what user too.
$getNid = SupplierFormHelpers::getNid($user->uid);
$variables['classes_array'][] = 'ROLE-logged-in';
$variables['classes_array'][] = $getNid;
}
}
and here's the Javascript file
/**
* @file
* Javascript to replace NID with the one for the current User
*/
(function ($) {
/**
* Process controls.
*/
Drupal.behaviors.moduleMenuOverrides = {
attach: function (context) {
// first of all lets place the number somewhere ( body class !! ) and retrieve that .
if ($("body").hasClass("ROLE-logged-in")){
var pageNid = 1;
/* get the body class array and retrieve the next number along */
var classList = $("body").attr('class').split(/\s+/);
for (var i = 0; i < classList.length; i++) {
if (classList[i] === 'ROLE-logged-in') {
var i2 = i + 1;
pageNid = classList[i2];
}
}
/* i now have the number I want to replace with - so let's do a swap */
var editLink = '/node/' + pageNid + '/edit';
var navigation = $("#block-system-navigation");
$("#block-system-navigation").each(function(){
var html = $(this).html();
html = html.replace("/node/1/edit", roleLink);
$(this).html(html);
})
}
}
}
})(jQuery);
No comments:
Post a Comment