You are here

public function CheeseburgerMenu::build in Cheeseburger Menu 8.2

Same name and namespace in other branches
  1. 8.4 src/Plugin/Block/CheeseburgerMenu.php \Drupal\cheeseburger_menu\Plugin\Block\CheeseburgerMenu::build()
  2. 8 src/Plugin/Block/CheeseburgerMenu.php \Drupal\cheeseburger_menu\Plugin\Block\CheeseburgerMenu::build()
  3. 8.3 src/Plugin/Block/CheeseburgerMenu.php \Drupal\cheeseburger_menu\Plugin\Block\CheeseburgerMenu::build()

Building block.

Overrides BlockPluginInterface::build

File

src/Plugin/Block/CheeseburgerMenu.php, line 628
Cheeseburger class extends BlockBase.

Class

CheeseburgerMenu
Block info.

Namespace

Drupal\cheeseburger_menu\Plugin\Block

Code

public function build() {
  $config = $this
    ->getConfiguration();
  $headerHeight = isset($config['headerHeight']) ? $config['headerHeight'] : 0;
  $headerPadding = isset($config['headerPadding']) ? $config['headerPadding'] : 0;
  $showOnAll = isset($config['breakpoint_all']) ? $config['breakpoint_all'] : TRUE;
  $page_type = 'unknown';
  if ($route_id = $this->routeMatch
    ->getParameters()
    ->get('taxonomy_term') != NULL) {
    $route_id = $this->routeMatch
      ->getParameters()
      ->get('taxonomy_term')
      ->id();
    $page_type = 'taxonomy_term';
  }
  elseif ($this->routeMatch
    ->getParameters()
    ->get('node') != NULL) {
    $route_id = $this->routeMatch
      ->getParameters()
      ->get('node')
      ->id();
    $page_type = 'node';
  }
  elseif ($this->routeMatch
    ->getParameters()
    ->get('view_id') != NULL) {
    $route_id = 'view.' . $this->routeMatch
      ->getParameters()
      ->get('view_id');
    $page_type = 'view';
  }
  elseif ($this->routeMatch
    ->getParameters()
    ->get('user') != NULL) {
    $route_id = $this->routeMatch
      ->getParameters()
      ->get('user')
      ->id();
    $page_type = 'user';
  }
  elseif ($this->routeMatch
    ->getRouteName() != NULL) {
    $route_id = $this->routeMatch
      ->getRouteName();
    if (strpos($route_id, 'commerce_cart') !== FALSE) {
      $page_type = 'cart';
    }
  }
  else {
    $route_id = 0;
    $page_type = 'unknown';
  }
  $current_route = Url::fromRoute('<current>');
  $current_route = $current_route
    ->toString();
  if ($showOnAll) {
    $controller = new RenderCheeseburgerMenuBlock($this->renderer, $this->menuTree, $this->themeHandler, $this->breakPointManager);
    $tree = $controller
      ->renderTree($route_id, $page_type, $current_route, $config);
    array_unshift($tree, [
      '#markup' => '<div class="cheeseburger-div-wrapper">',
    ]);
    $tree[] = [
      '#markup' => '</div>',
    ];
    $tree['#attached']['drupalSettings'] = [
      'collapsibleMenu' => 1,
      'interactiveParentMenu' => 0,
      'headerHeight' => $headerHeight,
      'headerPadding' => $headerPadding,
      'instant_show' => TRUE,
    ];
    if ($config['css_default']) {
      $tree['#attached']['library'][] = 'cheeseburger_menu/cheeseburger_menu.css';
    }
    return $tree;
  }
  else {
    $tree = [];
    $tree[] = [
      '#markup' => '<div class="cheeseburger-div-wrapper">',
    ];
    if ($config['css_default']) {
      $tree['#attached']['library'][] = 'cheeseburger_menu/cheeseburger_menu.css';
    }
    $breakpoints = $config['breakpoints'];
    foreach ($breakpoints as $key => $breakpoint) {
      $breakpoints[$key]['mediaQuery'] = str_replace('min', 'max', $breakpoints[$key]['mediaQuery']);
    }
    $tree['#attached'] = [
      'drupalSettings' => [
        'collapsibleMenu' => 1,
        'interactiveParentMenu' => 0,
        'headerHeight' => $headerHeight,
        'headerPadding' => $headerPadding,
        'route_id' => $route_id,
        'page_type' => $page_type,
        'current_route' => $current_route,
        'breakpoints' => $breakpoints,
        'block_id' => str_replace('_', '', $config['provider']),
        'instant_show' => FALSE,
      ],
    ];
    $tree[] = [
      '#markup' => '</div>',
    ];
    return $tree;
  }
}