DEVHIDE
  • Home (current)
  • About
  • Contact
  • Cookie
  • Home (current)
  • About
  • Contact
  • Cookie
  • Disclaimer
  • Privacy
  • TOS
Login Or Sign up

Drupal 8 remove extra divs and add custom classes in menu

1.5k Views Asked by Niladri Banerjee - Uttarpara At 13 February 2019 at 05:12 2025-12-17T14:19:31.884368

Currently the {{page.primary_menu}} created the extra divs and default d8 classes as below:

<div class="region region-primary-menu">
    <nav role="navigation" aria-labelledby="block-demo-main-menu-menu" id="block-demo-main-menu">
            <h2 class="sr-only" id="block-demo-main-menu-menu">Main navigation</h2>

      
      <ul class="menu menu--main nav navbar-nav">
                      <li class="first">
                                        <a href="#mu-slider" data-drupal-link-system-path="<front>" class="is-active">Home</a>
              </li>
                      <li>
                                        <a href="/drupal8/" data-drupal-link-system-path="<front>" class="is-active">Home</a>
              </li>
                      <li class="last">
                                        <a href="#mu-about-us" data-drupal-link-system-path="<front>" class="is-active">ABOUT US</a>
              </li>
        </ul>
  

  </nav>

  </div>

However, I want to generate the menu structure as like:

<ul id="top-menu" class="nav navbar-nav navbar-right mu-main-nav">
    <li><a href="#mu-slider">HOME</a></li>
    <li><a href="#mu-about-us">ABOUT US</a></li>                       
    <li><a href="#mu-restaurant-menu">MENU</a></li>                       
    <li><a href="#mu-reservation">RESERVATION</a></li>                   
    <li><a href="#mu-gallery">GALLERY</a></li>
    <li><a href="#mu-chef">OUR TEAM</a></li>
    <li><a href="#mu-latest-news">BLOG</a></li> 
    <li><a href="#mu-contact">CONTACT</a></li> 
 </ul>

I've created a file name demo.theme and pasted the code but it did not give me the expected result.

<?php 
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Template\Attribute;

/**
 * Implements hook_preprocess_HOOK() for HTML document templates.
 *
 * Adds body classes if certain regions have content.
 */
function demo_menu_tree(&$variables) {

  return '<ul id="top-menu" class="nav navbar-nav navbar-right mu-main-nav">' . $variables['tree'] . '</ul>';

}

Any suggestion?

drupal drupal-8 drupal-menu
Original Q&A
1

There are 1 best solutions below

0
Nate F. Nate F. On 20 February 2019 at 23:19
  1. Make sure you have twig debugging enabled, it will make your life a lot easier, by adding comments to your mark up (which you can see inline in the web inspector). Using those comments you can figure out what you should name your theme file.

  2. Create a new custom twig file in the /templates directory of your theme like so themes/[your-theme-name-here]/templates/menu.html.twig. As a starting point I'd suggest either using the default classy theme menu.html.twig template, or clone use the file referenced inline in the markup comments of your site when you have twig debugging enabled.

  3. Edit the menu.html.twig file to meet your needs, something like this:

{% import _self as menus %}

{#
  We call a macro which calls itself to render the full tree.
  @see http://twig.sensiolabs.org/doc/tags/macro.html
#}
{{ menus.menu_links(items, attributes, 0) }}

{% macro menu_links(items, attributes, menu_level) %}
  {% import _self as menus %}
  {% if items %}
    {% if menu_level == 0 %}
      <ul id="top-menu" {{ attributes.addClass('nav navbar-nav navbar-right mu-main-nav') }}>
    {% else %}
      <ul class="menu">
    {% endif %}
    {% for item in items %}
      {%
        set classes = [
          'menu-item',
          item.is_expanded ? 'menu-item--expanded',
          item.is_collapsed ? 'menu-item--collapsed',
          item.in_active_trail ? 'menu-item--active-trail',
        ]
      %}
      <li{{ item.attributes.addClass(classes) }}>
        {{ link(item.title, item.url) }}
        {% if item.below %}
          {{ menus.menu_links(item.below, attributes, menu_level + 1) }}
        {% endif %}
      </li>
    {% endfor %}
    </ul>
  {% endif %}
{% endmacro %}

Related Questions in DRUPAL

  • Drupal access control to custom user profile page
  • How to list all child pages of current parent page in drupal 7?
  • MariaDB/MySQL all of a sudden crashed and won't restart
  • Drupal location module shows only a portion of Gmap on node page
  • How to add a select collection type select list to field collection
  • Drupal7 modified user login no effect?
  • Drupal AngularJS ajax simple ng-repeat update
  • Drupal: Access A Field in hook_views_query_alter()
  • Drupal Webform : set SCORE on each SELECT Options?
  • Register new user as authenitcated user in drupal using xml-rpc
  • Drupal domain access auto create content
  • How to display sibling pages except current page in drupal 7?
  • how to merge entity reference relationship field value
  • Drupal print breaks when rendering a region
  • All files and folders are not being listed in the imce file browser in Drupal 7

Related Questions in DRUPAL-8

  • View theme override in drupal 8
  • drupal 8 error to render template twig with controller
  • Admin primary tabs completely disappeared
  • Setting the migration database in Drupal 8
  • How to select a form without submit button in drupal 8 using javascript
  • The tool "Handlers/emails" missing from YAML Forms
  • The installed schema version does not meet the minimum
  • .theme file not being reached by all hooks and preprocessing not implemented
  • Language Dependent Module Content in Template - Drupal 8
  • Is it possible to have multiple cache entries depending on the language in drupal 8?
  • Browsersync doesn't detect file changes with Drupal Basic Theme Grunt config
  • On using CSRF Token in Routing file of custom module throws error?
  • How to override template file item-list.html.twig for field_slider_images in Drupal 8?
  • Drupal 8 - custom Selection Handler for entity_autocomplete causes indefinite loading
  • Drupal 8 Errors in configuration

Related Questions in DRUPAL-MENU

  • Drupal7: Some of the main Menu links not showing if not logged in
  • Drupal - Set 'user/%/edit/uprofile' to default tab on 'user/%/edit'
  • Delete div from menu block drupal 7
  • Drupal 7 - Active menu items does not have active-trail class
  • How do I add a target="_blank" attribute to Footer Menu Block Link in Drupal 7
  • Drupal 8 remove extra divs and add custom classes in menu
  • Drupal 7: calling "menu block" into page.tpl.php

Trending Questions

  • UIImageView Frame Doesn't Reflect Constraints
  • Is it possible to use adb commands to click on a view by finding its ID?
  • How to create a new web character symbol recognizable by html/javascript?
  • Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
  • Heap Gives Page Fault
  • Connect ffmpeg to Visual Studio 2008
  • Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
  • How to avoid default initialization of objects in std::vector?
  • second argument of the command line arguments in a format other than char** argv or char* argv[]
  • How to improve efficiency of algorithm which generates next lexicographic permutation?
  • Navigating to the another actvity app getting crash in android
  • How to read the particular message format in android and store in sqlite database?
  • Resetting inventory status after order is cancelled
  • Efficiently compute powers of X in SSE/AVX
  • Insert into an external database using ajax and php : POST 500 (Internal Server Error)

Popular # Hahtags

javascript python java c# php android html jquery c++ css ios sql mysql r reactjs

Popular Questions

  • How do I undo the most recent local commits in Git?
  • How can I remove a specific item from an array in JavaScript?
  • How do I delete a Git branch locally and remotely?
  • Find all files containing a specific text (string) on Linux?
  • How do I revert a Git repository to a previous commit?
  • How do I create an HTML button that acts like a link?
  • How do I check out a remote Git branch?
  • How do I force "git pull" to overwrite local files?
  • How do I list all files of a directory?
  • How to check whether a string contains a substring in JavaScript?
  • How do I redirect to another webpage?
  • How can I iterate over rows in a Pandas DataFrame?
  • How do I convert a String to an int in Java?
  • Does Python have a string 'contains' substring method?
  • How do I check if a string contains a specific word?
.

Copyright © 2021 Jogjafile Inc.

  • Disclaimer
  • Privacy
  • TOS
  • Homegardensmart
  • Math
  • Aftereffectstemplates