• Development

    Adding Level Number Class to Menu Items in Drupal 8

    This is a quick post showing how to add level classes to menu items in Drupal 8.

    Here’s the result, showing the additional menu--level-N  and menu-item--level-N  classes:


    Step 1: Create a New Twig template File

    Determine which Twig template you need to override. I recommend reading the official docs on this. In my case, for a menu called infofor, I copied docroot/core/themes/classy/templates/navigation/menu.html.twig to docroot/themes/custom/mytheme/templates/navigation/menu--infofor.html.twig .

    Step 2: Update the Twig Code

  • Development

    Rebuilding the Drupal Navigation Menu (Automatically)

    The following steps will get you a completely rebuilt Navigation menu.

    1. Clear your cache
    2. Backup your database
    3. In the database, remove any records in {menu_links} where menu_name = navigation
    4. Create a file somewhere in your site’s directory structure
    5. Add this code to that file and then save it
    6. Tell Drupal to ignore this file using this method.
    7. Browse to the file in your web browser (it will run quickly, with no indication of completion). Wait for the page to stop loading, then check your navigation menu settings in the admin.
    8. When finished, delete or make-inaccessible the PHP file you just made!