0

Well i've tried the following code to insert an attribute to the user-account link on installation profile.It works for all other menu items (Home[]), except the user-account link, probably due to the reason it is rebuilt again.

<?php
   $path = 'user';
   // Check for user (My account) menu item.
   $existing_item = db_select('menu_links')
    ->fields('menu_links')
    ->condition('link_path', $path)
    //->condition('module', 'system')
    ->execute()->fetchAssoc();

  $item = array(
   'mlid' => $existing['mlid'],
   'link_path' => $existing['link_path'],
   'link_title' => $existing['link_title'],
 //'menu_name' => $existing['menu_name'],
   'router_path' => $existing['router_path'],
 //'module' => $existing['module'],
 //'plid' => $existing['plid'],
   'options' => array(
    'attributes' => array(
    'target' => '_blank',
    ),
   ),
 );
 // $item['options'] = serialize($item['options']);
 // $item = _menu_link_build($item);

 // $link['options'] = serialize($link['options']);
 if (menu_link_save($item, $existing) == $existing['mlid']) {

   drupal_set_message(t('User menu target attribute updated'));
 }

The above code :

  1. selects form the database the {menu_links} record for 'user' menu item.
  2. Alter the options in the $item['options'] array
  3. Performs menu_link_save() on the existing item since we have given an existing $mlid

Well, i've tried menu_cache_clear, rebuilds, straight altering the database values.. everything! but the "My account" link is rebuilded after my profile installation routines, even though is the last one.

How this will work? How can i achieve this kind of alteration in installation profile?

In general, is it possible to override module-defined menu items options/properties in profile, or hook_menu() will run always after my code and revoke all my changes?

0

Perhaps you could use:

hook_menu_alter

Not sure if it'll work with option attributes, but worth a try.

e.g

/**
 * Implements hook_menu_alter
 */
PROFILE_menu_alter(&$items){
  $items['user']['options'] = array('attributes' => array('target' => '_blank'));
}

Alternatively, it would be easy enough to add the _blank attr with jQuery.

e.g:

$('body.some-class #user-menu-link a').attr('target', '_blank');

Update: added example PROFILE_ option for menu_alter hook

  • Yeah , i could find a million ways to just add the attribute.That could be implemented in a module, but not in a profile installation...thnx – Pan Chrono Jul 24 '13 at 11:49
  • 1
    You can implement PROFILENAME_menu_alter(). The profile will be treated as a module. So if your profile is called myprofile you should put your myprofile_menu_alter() in myprofile.profile – Pontus Nilsson Jul 29 '13 at 7:42
0

The solution?

variable_set('user_target_attr', '_blank');

in the profile installation and

hook_menu_link_alter()

normally afterwards in the module like :

/**
 * Implements hook_menu_link_alter().
 */
function module_menu_link_alter(&$item) {
  $destination = drupal_get_destination();
    if ($item['link_path'] == 'user') {
    // Allow to be overriden!!
    $default_target = variable_get('user_target_attr', '_blank');
     if ($default_target != $item['options']['attributes']['target']) {
        variable_set('user_target_attr',
          $item['options']['attributes']['target']);
     }
    // Alter all the links to user page to open via the configured or default attribute.
    $item['options']['attributes']['target'] = variable_get('user_target_attr', '_blank');
    }
}

If we don't make the check for the default target , it will always get the default one and we could never update the defaults from amdin UI

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.