<?php //if you want show in every li of menu diferent onformation of node sigue instruction(2) // in theme function themName_preprocess_menu(&$variables){ $node = Node::load(2311); $view_builder = \Drupal::entityTypeManager()->getViewBuilder('node'); $teaser_view = $view_builder->view($node, 'teaser'); $variables['teaser'] = \Drupal::service('renderer')->renderRoot($teaser_view); //instruction(2) $ar = []; $i = 0 ; $ids = \Drupal::entityQuery('node') ->condition('status', 1) ->condition('type', 'blog') ->range(0, 30) ->execute(); foreach ($ids as $key => $value) { $ar[] = $value; } $menuItems = &$variables['items']; foreach ($menuItems as $idx => &$item) { $node = Node::load($ar[rand(0,30)]); if(empty($node)){ continue; } $view_builder = \Drupal::entityTypeManager()->getViewBuilder('node'); $teaser_view = $view_builder->view($node, 'teaser'); $item['#node']= \Drupal::service('renderer')->renderRoot($teaser_view); // in twig corespon print foreach($item in $items){ <li> {{ $item['#node }} </li> } } } // in twig menu.html.twig //{{ teaser }}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter