d0e09fc7ed320436c9b2c95b947561d0b2aabf34
[yaffs-website] / web / modules / contrib / devel / src / Form / ToolbarSettingsForm.php
1 <?php
2
3 namespace Drupal\devel\Form;
4
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\Form\ConfigFormBase;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\Menu\MenuLinkTreeInterface;
9 use Drupal\Core\Menu\MenuTreeParameters;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11
12 /**
13  * Configures devel toolbar settings.
14  */
15 class ToolbarSettingsForm extends ConfigFormBase {
16
17   /**
18    * The menu link tree service.
19    *
20    * @var \Drupal\Core\Menu\MenuLinkTree
21    */
22   protected $menuLinkTree;
23
24   /**
25    * ToolbarSettingsForm constructor.
26    *
27    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
28    *   The config factory.
29    * @param \Drupal\Core\Menu\MenuLinkTreeInterface $menu_link_tree
30    *   The menu link tree service.
31    */
32   public function __construct(ConfigFactoryInterface $config_factory, MenuLinkTreeInterface $menu_link_tree) {
33     parent::__construct($config_factory);
34     $this->menuLinkTree = $menu_link_tree;
35   }
36
37   /**
38    * {@inheritdoc}
39    */
40   public static function create(ContainerInterface $container) {
41     return new static(
42       $container->get('config.factory'),
43       $container->get('menu.link_tree')
44     );
45   }
46
47   /**
48    * {@inheritdoc}
49    */
50   public function getFormId() {
51     return 'devel_toolbar_settings_form';
52   }
53
54   /**
55    * {@inheritdoc}
56    */
57   protected function getEditableConfigNames() {
58     return [
59       'devel.toolbar.settings',
60     ];
61   }
62
63   /**
64    * {@inheritdoc}
65    */
66   public function buildForm(array $form, FormStateInterface $form_state) {
67     $config = $this->config('devel.toolbar.settings');
68
69     $form['toolbar_items'] = [
70       '#type' => 'checkboxes',
71       '#title' => $this->t('Menu items always visible'),
72       '#options' => $this->getLinkLabels(),
73       '#default_value' => $config->get('toolbar_items') ?: [],
74       '#required' => TRUE,
75       '#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.'),
76     ];
77
78     return parent::buildForm($form, $form_state);
79   }
80
81   /**
82    * {@inheritdoc}
83    */
84   public function submitForm(array &$form, FormStateInterface $form_state) {
85     $values = $form_state->getValues();
86     $toolbar_items = array_keys(array_filter($values['toolbar_items']));
87
88     $this->config('devel.toolbar.settings')
89       ->set('toolbar_items', $toolbar_items)
90       ->save();
91
92     parent::submitForm($form, $form_state);
93   }
94
95   /**
96    * Provides an array of available menu items.
97    *
98    * @return array
99    *   Associative array of devel menu item labels keyed by plugin ID.
100    */
101   protected function getLinkLabels() {
102     $options = [];
103
104     $parameters = new MenuTreeParameters();
105     $parameters->onlyEnabledLinks()->setTopLevelOnly();
106     $tree = $this->menuLinkTree->load('devel', $parameters);
107
108     foreach ($tree as $element) {
109       $link = $element->link;
110       $options[$link->getPluginId()] = $link->getTitle();
111     }
112
113     asort($options);
114
115     return $options;
116   }
117
118 }