0

Requirements:

Change the Navigation block link "Add Listing" based on role. This is the built in Navigation block and I've renamed Add Content to Add Listing

enter image description here

I've tried all the solutions here. Though I admit I am probably doing it wrong.

Change menu link title dynamically? How do I modify a menu link title dynamically? How do I dynamically change the title of a menu link?

I've created a module with the following code:

function mymodule_menu_alter(&$items) {
  $item['options']['alter'] = TRUE;
  $items['node/add']['access callback'] = '_accesscheck';
  $items['node/add']['title callback'] = 'mymodule_title_callback';
    var_dump($items);
}

function mymodule_title_callback($arg1) {
    $title = 'New Title';
    return $title;
}

function _accesscheck(){
  global $user;
  return $user->uid;
}

This is a var_dump snippet of what I'm after:

["node/add"]=>
  array(5) {
    ["title"]=>
    string(11) "Add content"
    ["page callback"]=>
    string(13) "node_add_page"
    ["access callback"]=>
    string(16) "_node_add_access"
    ["file"]=>
    string(14) "node.pages.inc"
    ["module"]=>
    string(4) "node"
  }

Before I attempt to include roles I'm just trying to get the link to change.

  • Might not matter, but generally your access callback should return a TRUE or a FALSE, not a uid. – lukedekker Apr 8 '16 at 16:12
  • To see if I can get this work, I removed the function and the access call back. Still no joy. – Max Apr 8 '16 at 19:34
3

Just tried your code (trimmed down) and it worked without any problems. I simply overwrote the "title callback", like you did. I tried it with admin and anonymous account, see the screenshots for the end result below:

enter image description here

I also added, as an extra precaution, a module_implements_alter implementation. With that hook, you can push your menu_alter implementation to the end of the hook_menu_alter list. That way you can make sure it gets executed after every other menu_alter, in case there was some competing implementation there.

<?php

/**
 * Implements hook_menu_alter().
 */
function mymodule_menu_alter(&$items) {
  $items['node/add']['title callback'] = 'mymodule_title_callback';
}

function mymodule_title_callback($arg1) {
  global $user;
  $title = 'New Title';
  if( $user->uid == 0 ) {
    $title = 'New Title for anonymous';
  }
  return $title;
}

/**
 * Implements hook_module_implements_alter().
 */
function mymodule_module_implements_alter(&$implementations, $hook) {
  if ($hook != 'menu_alter') {
    return;
  }

  $module = 'mymodule';
  $group = array($module => $implementations[$module]);
  unset($implementations[$module]);
  $implementations = $implementations + $group;
}

Hope you can get this to work!

  • Perfect and thank you for the code cleanup. I was easily able to add checks for each role and display the proper link name. If anyone else runs into this ensure you keep the defaults link names in the Navigation block. It MUST BE 'Create content' as the link name or it won't work. – Max Apr 11 '16 at 14:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.