3 namespace Drupal\webprofiler\Form;
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\Form\ConfigFormBase;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\webprofiler\Profiler\ProfilerStorageManager;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10 use Symfony\Component\HttpKernel\Profiler\Profiler;
15 class ConfigForm extends ConfigFormBase {
18 * @var \Symfony\Component\HttpKernel\Profiler\Profiler
28 * @var \Drupal\webprofiler\Profiler\ProfilerStorageManager
30 private $storageManager;
35 public static function create(ContainerInterface $container) {
37 $container->get('config.factory'),
38 $container->get('profiler'),
39 $container->get('profiler.storage_manager'),
40 $container->getParameter('data_collector.templates')
45 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
46 * @param Profiler $profiler
47 * @param \Drupal\webprofiler\Profiler\ProfilerStorageManager $storageManager
48 * @param array $templates
50 public function __construct(ConfigFactoryInterface $config_factory, Profiler $profiler, ProfilerStorageManager $storageManager, $templates) {
51 parent::__construct($config_factory);
53 $this->profiler = $profiler;
54 $this->templates = $templates;
55 $this->storageManager = $storageManager;
61 public function getFormId() {
62 return 'webprofiler_config';
68 public function buildForm(array $form, FormStateInterface $form_state) {
69 $config = $this->config('webprofiler.config');
71 $form['purge_on_cache_clear'] = [
72 '#type' => 'checkbox',
73 '#title' => $this->t('Purge on cache clear'),
74 '#description' => $this->t('Deletes all profiler files during cache clear.'),
75 '#default_value' => $config->get('purge_on_cache_clear'),
78 $storages = $this->storageManager->getStorages();
82 '#title' => $this->t('Storage backend'),
83 '#description' => $this->t('Choose were to store profiler data.'),
84 '#options' => $storages,
85 '#default_value' => $config->get('storage'),
89 '#type' => 'textarea',
90 '#title' => $this->t('Exclude'),
91 '#default_value' => $config->get('exclude'),
92 '#description' => $this->t('Paths to exclude for profiling. One path per line.'),
95 $form['active_toolbar_items'] = [
96 '#type' => 'checkboxes',
97 '#title' => $this->t('Active toolbar items'),
98 '#options' => $this->getCollectors(),
99 '#description' => $this->t('Choose which items to show into the toolbar.'),
100 '#default_value' => $config->get('active_toolbar_items'),
103 $form['ide_settings'] = [
104 '#type' => 'details',
105 '#title' => $this->t('IDE settings'),
109 $form['ide_settings']['ide_link'] = [
110 '#type' => 'textfield',
111 '#title' => $this->t('IDE link'),
112 '#description' => $this->t('IDE link for open files.'),
113 '#default_value' => $config->get('ide_link'),
116 $form['ide_settings']['ide_link_remote'] = [
117 '#type' => 'textfield',
118 '#title' => $this->t('IDE link remote path'),
119 '#description' => $this->t('The path of the remote docroot. Leave blank if the docroot is on the same machine of the IDE. No trailing slash.'),
120 '#default_value' => $config->get('ide_link_remote'),
123 $form['ide_settings']['ide_link_local'] = [
124 '#type' => 'textfield',
125 '#title' => $this->t('IDE link local path'),
126 '#description' => $this->t('The path of the local docroot. Leave blank if the docroot is on the same machine of IDE. No trailing slash.'),
127 '#default_value' => $config->get('ide_link_local'),
130 $form['database'] = [
131 '#type' => 'details',
132 '#title' => $this->t('Database settings'),
137 ':input[name="active_toolbar_items[database]' => array('checked' => TRUE),
143 $form['database']['query_sort'] = [
145 '#title' => $this->t('Sort query log'),
146 '#options' => ['source' => 'by source', 'duration' => 'by duration'],
147 '#description' => $this->t('The query table can be sorted in the order that the queries were executed or by descending duration.'),
148 '#default_value' => $config->get('query_sort'),
151 $form['database']['query_highlight'] = [
153 '#title' => $this->t('Slow query highlighting'),
154 '#description' => $this->t('Enter an integer in milliseconds. Any query which takes longer than this many milliseconds will be highlighted in the query log. This indicates a possibly inefficient query, or a candidate for caching.'),
155 '#default_value' => $config->get('query_highlight'),
159 $storageId = $this->config('webprofiler.config')->get('storage');
160 $storage = $this->storageManager->getStorage($storageId);
163 '#type' => 'details',
164 '#title' => $this->t('Purge profiles'),
168 $form['purge']['purge'] = [
170 '#value' => $this->t('Purge'),
171 '#submit' => [[$this, 'purge']],
174 $form['purge']['purge-help'] = [
175 '#type' => 'inline_template',
176 '#template' => '<div class="form-item">{{ message }}</div>',
178 'message' => $this->t('Purge %storage profiles.', ['%storage' => $storage['title']]),
182 return parent::buildForm($form, $form_state);
188 public function submitForm(array &$form, FormStateInterface $form_state) {
189 $this->config('webprofiler.config')
190 ->set('purge_on_cache_clear', $form_state->getValue('purge_on_cache_clear'))
191 ->set('storage', $form_state->getValue('storage'))
192 ->set('exclude', $form_state->getValue('exclude'))
193 ->set('active_toolbar_items', $form_state->getValue('active_toolbar_items'))
194 ->set('ide_link', $form_state->getValue('ide_link'))
195 ->set('ide_link_remote', $form_state->getValue('ide_link_remote'))
196 ->set('ide_link_local', $form_state->getValue('ide_link_local'))
197 ->set('query_sort', $form_state->getValue('query_sort'))
198 ->set('query_highlight', $form_state->getValue('query_highlight'))
201 parent::submitForm($form, $form_state);
207 public function purge(array &$form, FormStateInterface $form_state) {
208 $this->profiler->purge();
209 drupal_set_message($this->t('Profiles purged'));
215 private function getCollectors() {
217 foreach ($this->templates as $template) {
218 // drupal collector should not be disabled
219 if ($template[0] != 'drupal') {
220 $options[$template[0]] = $template[2];
232 public function getEditableConfigNames() {
234 'webprofiler.config',