ToolbarSettingsForm.php in Devel 8
File
src/Form/ToolbarSettingsForm.php
View source
<?php
namespace Drupal\devel\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Menu\MenuLinkTreeInterface;
use Drupal\Core\Menu\MenuTreeParameters;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ToolbarSettingsForm extends ConfigFormBase {
protected $menuLinkTree;
public function __construct(ConfigFactoryInterface $config_factory, MenuLinkTreeInterface $menu_link_tree) {
parent::__construct($config_factory);
$this->menuLinkTree = $menu_link_tree;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('menu.link_tree'));
}
public function getFormId() {
return 'devel_toolbar_settings_form';
}
protected function getEditableConfigNames() {
return [
'devel.toolbar.settings',
];
}
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('devel.toolbar.settings');
$form['toolbar_items'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Menu items always visible'),
'#options' => $this
->getLinkLabels(),
'#default_value' => $config
->get('toolbar_items') ?: [],
'#required' => TRUE,
'#description' => $this
->t('Select the menu items always visible in devel toolbar tray. All the items not selected in this list will be visible only when the toolbar orientation is vertical.'),
];
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$values = $form_state
->getValues();
$toolbar_items = array_keys(array_filter($values['toolbar_items']));
$this
->config('devel.toolbar.settings')
->set('toolbar_items', $toolbar_items)
->save();
parent::submitForm($form, $form_state);
}
protected function getLinkLabels() {
$options = [];
$parameters = new MenuTreeParameters();
$parameters
->onlyEnabledLinks()
->setTopLevelOnly();
$tree = $this->menuLinkTree
->load('devel', $parameters);
foreach ($tree as $element) {
$link = $element->link;
$options[$link
->getPluginId()] = $link
->getTitle();
}
asort($options);
return $options;
}
}