Menus

Elgg contains helper code to build menus throughout the site. In Elgg 1.8, this code has been rewritten and is more flexible than previous versions of Elgg.

Contents

Elgg 1.8

The menu functions in Elgg 1.8 use two classes: ElggMenuItem and ElggMenuBuilder.

ElggMenuItem

This class is used to create and register menu items. Many of its methods are exposed through procedural code.

ElggMenuBuilder

This class is used to build the final menu. Like ElggMenuItem, many of its methods are exposed through procedural code.

Introduction

In Elgg 1.8, every single menu requires a name, as does every single menu item. These are required in order to allow easy overriding and manipulation, as well as to provide hooks for theming. To add an item to a menu, use the elgg_register_menu_item() function. To remove an item from a menu, use the elgg_unregister_menu_item() function. To customize an existing menu item, use elgg_register_menu_item() and pass in the name of that item.

Examples

Using the options array method

elgg_register_menu_item('foo', array(
    'name' => 'itemname',
    'text' => 'This is an item\'s text',
    'href' => '/item/url',
));

Using the ElggMenuItem method

elgg_register_menu_item('foo', new ElggMenuItem('itemname', 'Item Text', '/href'));

Remove the "Elgg" logo from the topbar menu.

elgg_unregister_menu_item('topbar', 'elgg_logo');

Theming

The menu name, section names, and item names are all embedded into the HTML as CSS classes (normalized to contain only hyphens, rather that underscores or colons). This increases the size of the markup slightly but provides themers with a high degree of control and flexibility when styling the site. For example, the following would be the output of the "foo" menu with sections "alt" and "default" containing items "baz" and "bar" respectively.

<ul class="elgg-menu elgg-menu-foo elgg-menu-foo-alt">
  <li class="elgg-menu-item elgg-menu-item-baz"></li>
</ul>
<ul class="elgg-menu elgg-menu-foo elgg-menu-foo-default">
  <li class="elgg-menu-item elgg-menu-item-bar"></li>
</ul>

Elgg 1.7 and below

Elgg contains a menu register. Of course, plugins and views don't need to use it, but it's there. For example, the following piece of code is defined in the blog plugin to create a menu for logged in users:

if (isloggedin())
add_menu(elgg_echo('blog'), $CONFIG->wwwroot . "mod/blog/",array(
	menu_item(elgg_echo('blog:read'),$CONFIG->wwwroot."mod/blog/?username=" . $_SESSION['user']->username),
	menu_item(elgg_echo('blog:addpost'),$CONFIG->wwwroot."mod/blog/add.php"),
	menu_item(elgg_echo('blog:everyone'),$CONFIG->wwwroot."mod/blog/everyone.php"),
	));

add_menu() takes as its parameters the title of the menu, its default location, and then an array of menu items (defined using menu_item($title, $url)). Menu items actually are a stdClass with the following properties:

  • Name (string)
  • Value (the URL, string)
  • Children (top level elements only); an optional array of stdClass objects with name and value properties

The array of menu items can be retrieved simply by calling

$menuitems = get_register('menu');

Search docs