In drupal 7, there are default blocks such as user menu block and login block, the user menu block are especially working for the logged in user while login block are working for non logged in user.

the user menu block contains the static user menu with account, profile, logout menu item, however what I want to do is when the user logged in, the user menu can have additional menu item showing the user pic and name.

I know this could be done by making a new block with php code, but I just want to know if I want to use the existing user menu block, what and how I should change it?

  • Even if you don't want to write a block module from scratch with the Drupal API, you will still have to write code to alter the block via a hook. My suggestion is to build a block from scratch. Commented May 27, 2012 at 6:57

2 Answers 2


In Drupal 7, you can implement hook_block_view_alter() to alter the content of a block defined from another module. For example, menu_block_view_alter() is an implementation of that hook done by the Menu module.

function menu_block_view_alter(&$data, $block) {
  // Add contextual links for system menu blocks.
  if ($block->module == 'system' && !empty($data['content'])) {
    $system_menus = menu_list_system_menus();
    if (isset($system_menus[$block->delta])) {
      $data['content']['#contextual_links']['menu'] = array('admin/structure/menu/manage', array($block->delta));

$data is the array defined from the implementation of hook_block_view(), and it contains two array indexes:

  • "subject": the default localized title of the block
  • "content": the content of the block's body

In Drupal 7, $data['content'] could be either a string containing HTML code, or an array as accepted from drupal_render().

$block is an object containing all the data about the block loaded from the database. You can at least count on two elements:

  • "module": the name of the module implementing the block
  • "delta": the identifier for the block used from the module implementing it

If you just need to alter one single block, you can consider implementing hook_block_view_MODULE_DELTA_alter(), which is the equivalent of hook_form_FORM_ID_alter() of hook_form_alter(). Replace MODULE with the module name, and DELTA with the identifier for the block.
The hook gets the same parameters as hook_block_view_alter().


You can add any tokens to the menu item if you use the menu_token + menu_html modules:

  1. Go to the user account settings at example.com/admin/config/people/accounts
  2. Select the user's image style to an image style with the desired dimensions.
  3. Set the default avatr URL to an image with the same dimensions as your selected image style.
  4. Install the menu_token module. This will allow you to add tokens like [current-user:picture] to the menu item title.
  5. Install the menu_html module. This will tell drupal to keep the html in the token.
  6. Edit the menu item, enable tokens and HTML, and set the menu item title to someting like [current-user:name] [current-user:picture].
  7. Optionally, add tokens in your module (with hook_token_info + hook_tokens) to allow to put non-default image styles onto your menu item.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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