Exercise: Override the Main Menu Template for Drupal 8, Drupal 9

Last updated January 8, 2021
Theming8.9.x/9.0.x

In this exercise, we'll continue our template overriding practice by overriding the main menu template. We'll override it and rename it with a file name suggestion so that it will only affect the main menu component of our Drupal site. We'll consult the Bootstrap documentation and add the classes from the base nav component into our overridden main menu template file. Along the way, we'll utilize a variety of methods for adding CSS classes to HTML selectors including using attributes.addClass() and set. You should work on the exercise steps below first, and you can refer to the video if you need some help.

If you want to try and accomplish this on your own first you'll need to:

  • Override the menu.html.twig template file, using a file name suggestion to target only the main menu.
  • Add CSS classes from the base nav component in Bootstrap.

Note: At the end of this exercise, you'll find a video walk-through of the solution.