Site Building
Topic

Menus for Drupal 7, 8, 9, and 10

Drupal's menu system allows users to create as many separate menus as they need throughout their site. Links can be created to both external sites and internal pieces of content. The site administration interface gives users (with sufficient permissions) the ability to organize and sort these links within their respective menus. The API provided by the menu system gives developers the ability to interact with and alter menu links. The API also includes a mechanism to provide menu links from within custom modules.

Example tasks

  • Create several menus for use (and display) in various locations throughout a site
  • Add a link to a menu from within a custom module
  • Understand how existing menu items can be altered

Confidence

The Drupal menu system is stable in core.

Drupalize.Me resources

Configuring menus

Drupal 8, 9, and 10
More information

Overview of the menu concept and menus automatically created with the core Standard installation profile.

More information

How to add a page to the navigation.

More information

How to reorder links in a menu.

Menus can be added as menu blocks using the Block display system

Blocks

Topic
Drupal 7, 8, 9, and 10
More information

A block is a reusable widget that is placed inside regions (layout containers) of your theme. Blocks can be used by site administrators on the Block layout admin page or provided by a module using the Plugin API.

For developers

Categories
Drupal 8, 9, and 10
More information

Drupal provides module developers several different methods for creating the different types of links we see on a typical page. In this tutorial we'll see how these different types of links relate to each other, and when you might want to make use of them.

Link types illustrated

In this tutorial we'll provide an overview of the following concepts:

  • Menu links
  • Action links
  • Local tasks
  • Contextual links
Categories
Drupal 8, 9, and 10
More information

In this tutorial we will learn how to add menu links using a custom Drupal module. We will also look at the options available for configuring our new menu link. Lastly, we'll learn about using hook_menu_links_discovered_alter() that can be used to add new menu links, or alter the behavior of existing ones.

Categories
Drupal 8, 9, and 10
More information

Modules can provide action links to make common operations more readily available within a particular context. For example, a site administrator visiting the content management page may be interested in adding content. If you've ever noticed the Add content button on this page, that's an example of an action link. In this tutorial we'll take a look at how custom modules can provide these action links to make it easier for users to interact with our site.

More information

Contextual links provide yet another method for Drupal module developers to add helpful links for site administrators to quickly navigate to commonly used tasks. In this tutorial we'll learn how to implement and render contextual links from a custom module. We will also look at a pair of alter hooks that can be used to tweak existing contextual links.

More information

Local task links are the tabs you see when logged in as an administrator viewing a node on a Drupal site. In this tutorial we'll take a look at how local tasks are added within a custom module. We'll also see how to alter local tasks provided by other modules via hook_menu_local_tasks_alter().

Categories
Drupal 8, 9, and 10
More information

Linking to things is probably one of the first things you learned how to do as a web developer. Anchor tags are the framework of how the world wide web works. So it's important to know how to create them in Drupal. Chances are you'll be doing a lot of it.

Creating links to things in Drupal, however, is a bit more complicated than just typing out an HTML anchor tag. It requires understanding how URLs are generated from routes, and how to define links as renderable arrays. It can also be tricky because of the multitude of deprecated, but still functioning, ways of creating links.

In this tutorial we'll:

  • Use the \Drupal\Core\Url class to generate URL objects from routes and external URIs.
  • Use the \Drupal\Core\Link class to create HTML links within a Drupal module.
  • Examine best practices for working with URLs and links in a Drupal module in order to ensure that your code continues to work into the future.

By the end of this tutorial you should be able to link to anything, either internal to your application or external, using Drupal best practices.

Drupal 7

More information

Provides a overview presentation for how Drupal's menu system takes care of incoming requests via the index.php. Then it walks through the process of implementing a simple module that hooks into Drupal's menu system at the path of /magic. It then executes a page callback function of menu_magic_basic(), which outputs some simple markup text.

Additional resources

hook_menu() documentation

This tutorial is part of our Module Developer in Drupal 7 series. Several of the tutorials that follow this one walk through some of the more advanced techniques for working with the menu system.

More information

In this video we take a tour of Drupal menus, and show you how you can modify existing menu items and create your own.

Guides

Not sure where to start? Our guides provide useful learning tracks for all skill levels.

Navigate guides

External resources

  • Accessible Navigation with Drupal Core's Menu System (lullabot.com)
    • New to Drupal 8.9 and 9.0 is the ability to create the HTML <button> element within a native Drupal menu that can be used to toggle secondary menus (such as drop-downs or mega-menus) in a usable and accessible way. Learn more in this article from Lullabot by front-end developer Mike Herschel.