0
votes

Is it possible to have a block/view render from within my Mega Menu twig template?

I've created a region, created a view/block and added that block to the region. But it's a matter of having that content from the view displayed in my menu.

I started off by considering I may be able to simply have a region specified from within my twig menu loop.

enter image description here

1

1 Answers

0
votes

If you want to add something to your template, you need to use preprocess function in your module/theme. For example, you can add view variable using views_embed_view function:

/**
 * Implements hook_preprocess_HOOK().
 */
function MYTHEME_preprocess_menu(&$variables) {
  switch ($variables['menu_name']) {
    case 'mega-menu':
      $variables['my_view'] = views_embed_view('my_view');
      break;
  }
}

After this, $my_view variable will be defined in your menu--mega-menu.html.twig template.

Adding block is a bit more difficult. Please take a look at this answer.