1

In D7 I could place and style a menu with a few lines in a mytheme_preprocess_HOOK function. for example:

$variables['page']['navigation'] = array(
  'section' => array(
    '#type' => 'html_tag',
    '#tag' => 'nav',
    '#attributes' => array(
      'id' => drupal_html_id('navigation'),
      'class' => array(
        'clearfix',
      ),
      'role' => 'navigation',
      'aria-labelledby' => 'main-menu-label',
    ),
    'menu' => array(
      '#theme' => 'links__system_main_menu',
      '#attributes' => array(
        'id' => drupal_html_id('main-menu'),
        'class' => array(
          'links',
          'inline',
          'clearfix',
        ),
      ),
      '#links' => menu_navigation_links('main-menu'),
    ),
  ),
);

As you can see this gave great control over markup and attributes for function and styling. The only way I can find to add a menu in D8 is via a block and to style via a TWIG template.

How can I add the menu in a preprocess?

2
  • I guess I would ask, why wouldn't you do it with a Twig template in D8? Commented Aug 16, 2016 at 0:16
  • There is less control or I should say defining text for something like the title H2 in a TWIG file would drastically limit how other functions could interact with the content. As well hook_theme_suggestions_HOOK_alter and a number of TWIG files means more to manage. Commented Aug 16, 2016 at 13:26

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.