You are here

public function HorizontalMenu::build in Responsive and off-canvas menu 4.3.x

Same name and namespace in other branches
  1. 8.3 src/Plugin/Block/HorizontalMenu.php \Drupal\responsive_menu\Plugin\Block\HorizontalMenu::build()
  2. 8.2 src/Plugin/Block/HorizontalMenu.php \Drupal\responsive_menu\Plugin\Block\HorizontalMenu::build()
  3. 4.4.x src/Plugin/Block/HorizontalMenu.php \Drupal\responsive_menu\Plugin\Block\HorizontalMenu::build()
  4. 4.0.x src/Plugin/Block/HorizontalMenu.php \Drupal\responsive_menu\Plugin\Block\HorizontalMenu::build()
  5. 4.1.x src/Plugin/Block/HorizontalMenu.php \Drupal\responsive_menu\Plugin\Block\HorizontalMenu::build()

Builds and returns the renderable array for this block plugin.

If a block should not be rendered because it has no content, then this method must also ensure to return no content: it must then only return an empty array, or an empty array with #cache set (with cacheability metadata indicating the circumstances for it being empty).

Return value

array A renderable array representing the content of the block.

Overrides BlockPluginInterface::build

See also

\Drupal\block\BlockViewBuilder

File

src/Plugin/Block/HorizontalMenu.php, line 89

Class

HorizontalMenu
Provides the HorizontalMenu block.

Namespace

Drupal\responsive_menu\Plugin\Block

Code

public function build() {
  $depth = $this->config
    ->get('horizontal_depth');
  $menu_name = $this->config
    ->get('horizontal_menu');

  // Allow other modules to modify the menu name.
  $this->moduleHandler
    ->alter('responsive_menu_horizontal_menu_name', $menu_name);
  $menu_tree = $this->menuTree;
  $parameters = $menu_tree
    ->getCurrentRouteMenuTreeParameters($menu_name);
  $parameters
    ->setMaxDepth($depth);

  // Force the entire tree to be build be setting expandParents to an
  // empty array.
  $parameters->expandedParents = [];
  $tree = $menu_tree
    ->load($menu_name, $parameters);
  $manipulators = [
    // Show links to nodes that are accessible for the current user.
    [
      'callable' => 'menu.default_tree_manipulators:checkNodeAccess',
    ],
    // Only show links that are accessible for the current user.
    [
      'callable' => 'menu.default_tree_manipulators:checkAccess',
    ],
    // Use the default sorting of menu links.
    [
      'callable' => 'menu.default_tree_manipulators:generateIndexAndSort',
    ],
  ];

  // Allow other modules to alter manipulators before transforming menu tree.
  $this->moduleHandler
    ->alter('responsive_menu_horizontal_manipulators', $manipulators);
  $tree = $menu_tree
    ->transform($tree, $manipulators);
  $menu = $menu_tree
    ->build($tree);

  // Allow other modules to manipulate the built tree data.
  $this->moduleHandler
    ->alter('responsive_menu_horizontal_tree', $menu);
  $menu['#theme'] = 'responsive_menu_horizontal';
  $output = [
    '#theme' => 'responsive_menu_block_wrapper',
    '#element_type' => $this->config
      ->get('horizontal_wrapping_element'),
    '#content' => $menu,
  ];

  // Add the superfish library if the user has requested it.
  $superfish_setting = $this->config
    ->get('horizontal_superfish');
  if ($superfish_setting) {
    $output['#attached']['library'][] = 'responsive_menu/responsive_menu.superfish';

    // Add some of the config as javascript settings.
    $output['#attached']['drupalSettings']['responsive_menu']['superfish'] = [
      'active' => $this->config
        ->get('horizontal_superfish'),
      'delay' => $this->config
        ->get('horizontal_superfish_delay'),
      'speed' => $this->config
        ->get('horizontal_superfish_speed'),
      'speedOut' => $this->config
        ->get('horizontal_superfish_speed_out'),
    ];
  }

  // Add superfish's hoverIntent library if the user has requested it.
  if ($superfish_setting && $this->config
    ->get('horizontal_superfish_hoverintent')) {
    $output['#attached']['library'][] = 'responsive_menu/responsive_menu.superfish_hoverintent';
  }
  $media_query = $this->config
    ->get('horizontal_media_query');

  // Attempt to clean up a media query in case it isn't properly enclosed in
  // brackets.
  $media_query = preg_replace('/^(min|max)(.+?)$/', '($1$2)', $media_query);
  $output['#attached']['drupalSettings']['responsive_menu']['mediaQuery'] = $media_query;

  // Add a contextual link to edit the menu.
  $output['#contextual_links']['menu'] = [
    'route_parameters' => [
      'menu' => $menu_name,
    ],
  ];
  return $output;
}