SimpleMenuBlock.php in Simple Mobile Menu 8
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
->_build_menu_style($tree, $block_config);
$library = [];
$library[] = 'simple_mobile_menu/simple_mobile_menu';
return [
'#theme' => 'simple_mobile_menu',
'#attached' => [
'library' => $library,
],
'#menu_output' => drupal_render($tree),
];
}
public function _build_sub_menu_menuparent($items) {
foreach ($items as $k => &$item) {
if ($item['below']) {
$item['attributes']
->addClass('has-child');
$item['below'] = $this
->_build_sub_menu_menuparent($item['below']);
}
}
return $items;
}
public function _build_menu_style($tree, $block_config) {
$tree['#attributes']['class'][] = 'mobile_menu';
$tree['#items'] = $this
->_build_sub_menu_menuparent($tree['#items']);
return $tree;
}
}