Version 1
[yaffs-website] / web / modules / contrib / devel / src / Form / SettingsForm.php
1 <?php
2
3 namespace Drupal\devel\Form;
4
5 use Drupal\Core\Form\ConfigFormBase;
6 use Drupal\devel\DevelDumperPluginManagerInterface;
7 use Symfony\Component\DependencyInjection\ContainerInterface;
8 use Symfony\Component\HttpFoundation\Request;
9 use Drupal\Core\Form\FormStateInterface;
10 use Drupal\Core\Url;
11
12 /**
13  * Defines a form that configures devel settings.
14  */
15 class SettingsForm extends ConfigFormBase {
16
17   /**
18    * Devel Dumper Plugin Manager.
19    *
20    * @var \Drupal\devel\DevelDumperPluginManager
21    */
22   protected $dumperManager;
23
24   /**
25    * Constructs a new SettingsForm object.
26    *
27    * @param \Drupal\devel\DevelDumperPluginManagerInterface $devel_dumper_manager
28    *   Devel Dumper Plugin Manager.
29    */
30   public function __construct(DevelDumperPluginManagerInterface $devel_dumper_manager) {
31     $this->dumperManager = $devel_dumper_manager;
32   }
33
34   /**
35    * {@inheritdoc}
36    */
37   public static function create(ContainerInterface $container) {
38     return new static(
39       $container->get('plugin.manager.devel_dumper')
40     );
41   }
42
43   /**
44    * {@inheritdoc}
45    */
46   public function getFormId() {
47     return 'devel_admin_settings_form';
48   }
49
50   /**
51    * {@inheritdoc}
52    */
53   protected function getEditableConfigNames() {
54     return [
55       'devel.settings',
56     ];
57   }
58
59   /**
60    * {@inheritdoc}
61    */
62   public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL) {
63     $current_url = Url::createFromRequest($request);
64     $devel_config = $this->config('devel.settings');
65
66     $form['page_alter'] = array('#type' => 'checkbox',
67       '#title' => t('Display $page array'),
68       '#default_value' => $devel_config->get('page_alter'),
69       '#description' => t('Display $page array from <a href="http://api.drupal.org/api/function/hook_page_alter/7">hook_page_alter()</a> in the messages area of each page.'),
70     );
71     $form['raw_names'] = array('#type' => 'checkbox',
72       '#title' => t('Display machine names of permissions and modules'),
73       '#default_value' => $devel_config->get('raw_names'),
74       '#description' => t('Display the language-independent machine names of the permissions in mouse-over hints on the <a href=":permissions_url">Permissions</a> page and the module base file names on the Permissions and <a href=":modules_url">Modules</a> pages.', array(':permissions_url' => Url::fromRoute('user.admin_permissions')->toString(), ':modules_url' => Url::fromRoute('system.modules_list')->toString())),
75     );
76     $form['rebuild_theme'] = array(
77       '#type' => 'checkbox',
78       '#title' => $this->t('Rebuild the theme registry on every page load'),
79       '#description' => $this->t('New templates, theme overrides, and changes to the theme.info.yml need the theme registry to be rebuilt in order to appear on the site.'),
80       '#default_value' => $devel_config->get('rebuild_theme'),
81     );
82
83     $error_handlers = devel_get_handlers();
84     $form['error_handlers'] = array(
85       '#type' => 'select',
86       '#title' => t('Error handlers'),
87       '#options' => array(
88         DEVEL_ERROR_HANDLER_NONE => t('None'),
89         DEVEL_ERROR_HANDLER_STANDARD => t('Standard Drupal'),
90         DEVEL_ERROR_HANDLER_BACKTRACE_DPM => t('Kint backtrace in the message area'),
91         DEVEL_ERROR_HANDLER_BACKTRACE_KINT => t('Kint backtrace above the rendered page'),
92       ),
93       '#multiple' => TRUE,
94       '#default_value' => empty($error_handlers) ? DEVEL_ERROR_HANDLER_NONE : $error_handlers,
95       '#description' => [
96         [
97           '#markup' => $this->t('Select the error handler(s) to use, in case you <a href=":choose">choose to show errors on screen</a>.', [':choose' => $this->url('system.logging_settings')])
98         ],
99         [
100           '#theme' => 'item_list',
101           '#items' => [
102             $this->t('<em>None</em> is a good option when stepping through the site in your debugger.'),
103             $this->t('<em>Standard Drupal</em> does not display all the information that is often needed to resolve an issue.'),
104             $this->t('<em>Kint backtrace</em> displays nice debug information when any type of error is noticed, but only to users with the %perm permission.', ['%perm' => t('Access developer information')]),
105           ],
106         ],
107         [
108           '#markup' => $this->t('Depending on the situation, the theme, the size of the call stack and the arguments, etc., some handlers may not display their messages, or display them on the subsequent page. Select <em>Standard Drupal</em> <strong>and</strong> <em>Kint backtrace above the rendered page</em> to maximize your chances of not missing any messages.') . '<br />' .
109             $this->t('Demonstrate the current error handler(s):') . ' ' .
110             $this->l('notice', $current_url->setOption('query', ['demo' => 'notice'])) . ', ' .
111             $this->l('notice+warning', $current_url->setOption('query', ['demo' => 'warning'])). ', ' .
112             $this->l('notice+warning+error', $current_url->setOption('query', ['demo' => 'error'])) . ' (' .
113             $this->t('The presentation of the @error is determined by PHP.', ['@error' => 'error']) . ')'
114         ],
115       ],
116     );
117     $form['error_handlers']['#size'] = count($form['error_handlers']['#options']);
118     if ($request->query->has('demo')) {
119       if ($request->getMethod() == 'GET') {
120         $this->demonstrateErrorHandlers($request->query->get('demo'));
121       }
122       $request->query->remove('demo');
123     }
124
125     $dumper = $devel_config->get('devel_dumper');
126     $default = $this->dumperManager->isPluginSupported($dumper) ? $dumper : $this->dumperManager->getFallbackPluginId(NULL);
127
128     $form['dumper'] = array(
129       '#type' => 'radios',
130       '#title' => $this->t('Variables Dumper'),
131       '#options' => [],
132       '#default_value' => $default,
133       '#description' => $this->t('Select the debugging tool used for formatting and displaying the variables inspected through the debug functions of Devel. You can enable the <a href=":kint_install">Kint module</a> (shipped with Devel) and select the Kint debugging tool for an improved debugging experience. <strong>NOTE</strong>: Some of these plugins require external libraries for to be enabled. Learn how install external libraries with <a href=":url">Composer</a>.', [':url' => 'https://www.drupal.org/node/2404989', ':kint_install' => Url::fromRoute('system.modules_list')->toString()]),
134     );
135
136     foreach ($this->dumperManager->getDefinitions() as $id => $definition) {
137       $form['dumper']['#options'][$id] = $definition['label'];
138
139       $supported = $this->dumperManager->isPluginSupported($id);
140       $form['dumper'][$id]['#disabled'] = !$supported;
141
142       $form['dumper'][$id]['#description'] = [
143         '#type' => 'inline_template',
144         '#template' => '{{ description }}{% if not supported %}<div><small>{% trans %}<strong>Not available</strong>. You may need to install external dependencies for use this plugin.{% endtrans %}</small></div>{% endif %}',
145         '#context' => [
146           'description' => $definition['description'],
147           'supported' => $supported,
148         ]
149       ];
150     }
151
152     return parent::buildForm($form, $form_state);
153   }
154
155   /**
156    * {@inheritdoc}
157    */
158   public function submitForm(array &$form, FormStateInterface $form_state) {
159     $values = $form_state->getValues();
160     $this->config('devel.settings')
161       ->set('page_alter', $values['page_alter'])
162       ->set('raw_names', $values['raw_names'])
163       ->set('error_handlers', $values['error_handlers'])
164       ->set('rebuild_theme', $values['rebuild_theme'])
165       ->set('devel_dumper', $values['dumper'])
166       ->save();
167
168     parent::submitForm($form, $form_state);
169   }
170
171   /**
172    * @param string $severity
173    */
174   protected function demonstrateErrorHandlers($severity) {
175     switch ($severity) {
176       case 'notice':
177         $undefined = $undefined;
178         break;
179       case 'warning':
180         $undefined = $undefined;
181         1/0;
182         break;
183       case 'error':
184         $undefined = $undefined;
185         1/0;
186         devel_undefined_function();
187         break;
188     }
189   }
190
191 }