View source
<?php
namespace Drupal\smartmenus\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Menu\MenuLinkTreeInterface;
use Drupal\Core\Render\Renderer;
use Drupal\Component\Utility\Html;
use Drupal\smartmenus\SmartmenusUtil;
class SmartMenusBlock extends BlockBase implements ContainerFactoryPluginInterface {
protected $menuLinkTree;
protected $renderer;
protected $htmlUtil;
protected $smartmenusUtil;
public function __construct(array $configuration, $plugin_id, $plugin_definition, MenuLinkTreeInterface $menu_link_tree, Renderer $renderer, SmartmenusUtil $smartmenusUtil) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->menuLinkTree = $menu_link_tree;
$this->renderer = $renderer;
$this->smartmenusUtil = $smartmenusUtil;
$this->htmlUtil = new Html();
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('menu.link_tree'), $container
->get('renderer'), $container
->get('smartmenus.util'));
}
public function defaultConfiguration() {
return [] + parent::defaultConfiguration();
}
public function blockForm($form, FormStateInterface $form_state) {
$form = parent::blockForm($form, $form_state);
$config = $this
->getConfiguration();
$form['smartmenus'] = array(
'#type' => 'fieldset',
'#title' => $this
->t('Smartmenus Settings'),
);
$form['smartmenus']['smartmenus_menu'] = array(
'#type' => 'select',
'#title' => $this
->t('Menu'),
'#options' => menu_ui_get_menus(),
'#description' => t('The desired menu to render as a Smartmenu.'),
'#default_value' => $config['smartmenus_menu'] ? $config['smartmenus_menu'] : '',
'#required' => TRUE,
);
$form['smartmenus']['smartmenus_toggle'] = array(
'#type' => 'checkbox',
'#title' => $this
->t('Display a menu toggle button on small screens'),
'#default_value' => $config['smartmenus_toggle'] ? $config['smartmenus_toggle'] : '',
);
$form['smartmenus']['smartmenus_orient'] = array(
'#type' => 'radios',
'#title' => $this
->t('Orientation'),
'#options' => array(
'vertical' => $this
->t('Vertical'),
'horizontal' => $this
->t('Horizontal'),
),
'#default_value' => $config['smartmenus_orient'] ? $config['smartmenus_orient'] : 'horizontal',
);
$form['smartmenus']['smartmenus_theme'] = array(
'#type' => 'select',
'#title' => $this
->t('Smart menus theme'),
'#options' => $this
->getThemeOptions(),
'#default_value' => $config['smartmenus_theme'] ? $config['smartmenus_theme'] : $this
->getDefaultTheme(),
'#required' => true,
);
return $form;
}
public function blockSubmit($form, FormStateInterface $form_state) {
$values = $form_state
->getValues();
$this->configuration['smartmenus_menu'] = $values['smartmenus']['smartmenus_menu'];
$this->configuration['smartmenus_toggle'] = $values['smartmenus']['smartmenus_toggle'];
$this->configuration['smartmenus_orient'] = $values['smartmenus']['smartmenus_orient'];
$this->configuration['smartmenus_theme'] = $values['smartmenus']['smartmenus_theme'];
}
public function getThemeOptions() {
return $this->smartmenusUtil
->getAvailableMenuThemesList();
}
public function getSelectedTheme() {
return $this
->getConfiguration()['smartmenus_theme'];
}
public function getSelectedMenu() {
return $this
->getConfiguration()['smartmenus_menu'];
}
public function getSelectedOrientation() {
return $this
->getConfiguration()['smartmenus_orient'];
}
public function isToggleEnabled() {
return $this
->getConfiguration()['smartmenus_toggle'];
}
public function getDefaultTheme() {
$config = \Drupal::config($this->smartmenusUtil
->getConfigFormSettingsName());
$theme = $config
->get('smartmenus_theme');
return isset($theme) ? $theme : '';
}
public function build() {
$menu_name = $this
->getSelectedMenu();
$parameters = $this->menuLinkTree
->getCurrentRouteMenuTreeParameters($menu_name);
$parameters->expandedParents = [];
$tree = $this->menuLinkTree
->load($menu_name, $parameters);
$manipulators = [
[
'callable' => 'menu.default_tree_manipulators:checkAccess',
],
[
'callable' => 'menu.default_tree_manipulators:generateIndexAndSort',
],
];
$tree = $this->menuLinkTree
->transform($tree, $manipulators);
$tree = $this->menuLinkTree
->build($tree);
$build['#theme'] = 'smartmenus_block';
$build['#attached']['library'] = [
'smartmenus/smartmenus-custom',
];
$menuOrientation = 'sm-' . $this
->getSelectedOrientation();
$toggle = null;
if ($this
->isToggleEnabled()) {
$render_toggle = [
'#theme' => 'smartmenus_toggle',
];
$toggle = $this->renderer
->renderPlain($render_toggle);
}
$rendered_menu = [
'#theme' => 'smartmenus_menu',
'#items' => $tree['#items'],
'#attributes' => [
'class' => [
$this
->getSelectedTheme(),
$menuOrientation,
],
],
'#menu_name' => $menu_name,
'#toggle' => $toggle,
'#cache' => [
'contexts' => [
'url.path',
],
],
];
$build['#menu_tree'] = $this->renderer
->renderPlain($rendered_menu);
return $build;
}
}