Programmatically add menu item/link to Drupal menu

2019-08-02 21:06发布

问题:

I have a Drupal Menu which I created through the interface. I wish to add an entry which says

Hi, John Doe

Where "John Doe" is a link to the User's profile page. I would like to do this programmatically or if it can be done through the interface then that would be great.

回答1:

You cannot add HTML-ified items to the menu. Menu-items are always wrapped in an anchor tag and will filter out HTML. Changing this behaviour is technically possible, but will cause a lot of unknown side-effects.

What you want, is either a simple theme override, or a custom block (potentially without a title and with some other bullets).

You can create a block with hook_block or by simply typing the HTML, with some PHP into a new block, using the PHP-input filter. This last option is quick, but is discouraged by many people for performance and "good practices" reasons: you should not store php in your database.

EDIT: After comment about "just the name":

To insert "just the name" you simply need to create module with a hook_menu.

global $user;
$items['path/%uid'] = array(
  'title'            => $user->name,
  'description'      => 'description',
  'page callback'    => 'drupal_get_form', //Fill in the callback here: function that renders the page content.
  'page arguments'   => array(''),
  'access callback'  => '',
  'access arguments' => array(''),
  'weight'           => 0,
  'menu_name'        => 'Navigation',
  'type'             => MENU_NORMAL_ITEM,
);

Is about what you want.



标签: drupal menu