You are here

public function SimpleMegaMenuTwigExtension::viewMegaMenu in Simple Mega Menu 8

Same name and namespace in other branches
  1. 2.0.x src/TwigExtension/SimpleMegaMenuTwigExtension.php \Drupal\simple_megamenu\TwigExtension\SimpleMegaMenuTwigExtension::viewMegaMenu()

Render a mega menu in a view from an Url object.

Parameters

\Drupal\Core\Url|string $url: The URL object used for the link.

string $view_mode: The view mode to use for rendering the mega menu.

Return value

array A render array representing a the megamenu for the given view mode.

File

src/TwigExtension/SimpleMegaMenuTwigExtension.php, line 54

Class

SimpleMegaMenuTwigExtension
Class SimpleMegaMenuTwigExtension.

Namespace

Drupal\simple_megamenu\TwigExtension

Code

public function viewMegaMenu(Url $url, $view_mode = 'default') {
  if (!$url instanceof Url) {
    return [];
  }
  if (!$this
    ->hasMegaMenu($url)) {
    return [];
  }
  $build = [];
  $menu_attributes = $url
    ->getOption('attributes');
  $simple_mega_menu_id = $menu_attributes['data-simple-mega-menu'];

  /** @var \Drupal\simple_megamenu\Entity\SimpleMegaMenu $simple_mega_menu */
  $simple_mega_menu = $this->entityTypeManager
    ->getStorage('simple_mega_menu')
    ->load($simple_mega_menu_id);
  if ($simple_mega_menu instanceof SimpleMegaMenuInterface) {
    if (!$simple_mega_menu
      ->access('view')) {
      return $build;
    }
    $viewBuilder = $this->entityTypeManager
      ->getViewBuilder($simple_mega_menu
      ->getEntityTypeId());
    $build = $viewBuilder
      ->view($simple_mega_menu, $view_mode);
  }
  return $build;
}