SimpleMenuBlock.php in Simple Mobile Menu 8.2
File
src/Plugin/Block/SimpleMenuBlock.php
View source
<?php
namespace Drupal\simple_mobile_menu\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Block\BlockPluginInterface;
class SimpleMenuBlock extends BlockBase implements BlockPluginInterface {
public function build() {
$simple_mobile_menu_config = \Drupal::config('simple_mobile_menu.settings')
->get('menu_name');
$block_config = [];
$block_config['menu_name'][] = $simple_mobile_menu_config;
$block_config = [
'menu_name',
$simple_mobile_menu_config,
];
$tree = simple_mobile_menu_build_tree($block_config);
$tree = $this
->buildMenuStyle($tree, $block_config);
$library = [];
$library[] = 'simple_mobile_menu/simple_mobile_menu';
return [
'#theme' => 'simple_mobile_menu',
'#attached' => [
'library' => $library,
],
'#menu_output' => \Drupal::service('renderer')
->render($tree),
];
}
public function buildSubMenuMenuparent($items) {
foreach ($items as $k => &$item) {
if ($item['below']) {
$item['attributes']
->addClass('has-child');
$item['below'] = $this
->buildSubMenuMenuparent($item['below']);
}
}
return $items;
}
public function buildMenuStyle($tree, $block_config) {
$tree['#attributes']['class'][] = 'mobile_menu';
$tree['#items'] = $this
->buildSubMenuMenuparent($tree['#items']);
return $tree;
}
}