4c06e0b1de5ca8e5a9f8280b2bed573b64859481
[yaffs-website] / web / core / modules / statistics / src / StatisticsSettingsForm.php
1 <?php
2
3 namespace Drupal\statistics;
4
5 use Drupal\Core\Extension\ModuleHandlerInterface;
6 use Drupal\Core\Form\ConfigFormBase;
7 use Drupal\Core\Config\ConfigFactoryInterface;
8 use Drupal\Core\Form\FormStateInterface;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10
11 /**
12  * Configure statistics settings for this site.
13  */
14 class StatisticsSettingsForm extends ConfigFormBase {
15
16   /**
17    * The module handler.
18    *
19    * @var \Drupal\Core\Extension\ModuleHandlerInterface
20    */
21   protected $moduleHandler;
22
23   /**
24    * Constructs a \Drupal\statistics\StatisticsSettingsForm object.
25    *
26    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
27    *   The factory for configuration objects.
28    * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
29    *   The module handler.
30    */
31   public function __construct(ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler) {
32     parent::__construct($config_factory);
33
34     $this->moduleHandler = $module_handler;
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('module_handler')
44     );
45   }
46
47   /**
48    * {@inheritdoc}
49    */
50   public function getFormId() {
51     return 'statistics_settings_form';
52   }
53
54   /**
55    * {@inheritdoc}
56    */
57   protected function getEditableConfigNames() {
58     return ['statistics.settings'];
59   }
60
61   /**
62    * {@inheritdoc}
63    */
64   public function buildForm(array $form, FormStateInterface $form_state) {
65     $config = $this->config('statistics.settings');
66
67     // Content counter settings.
68     $form['content'] = [
69       '#type' => 'details',
70       '#title' => t('Content viewing counter settings'),
71       '#open' => TRUE,
72     ];
73     $form['content']['statistics_count_content_views'] = [
74       '#type' => 'checkbox',
75       '#title' => t('Count content views'),
76       '#default_value' => $config->get('count_content_views'),
77       '#description' => t('Increment a counter each time content is viewed.'),
78     ];
79
80     return parent::buildForm($form, $form_state);
81   }
82
83   /**
84    * {@inheritdoc}
85    */
86   public function submitForm(array &$form, FormStateInterface $form_state) {
87     $this->config('statistics.settings')
88       ->set('count_content_views', $form_state->getValue('statistics_count_content_views'))
89       ->save();
90
91     // The popular statistics block is dependent on these settings, so clear the
92     // block plugin definitions cache.
93     if ($this->moduleHandler->moduleExists('block')) {
94       \Drupal::service('plugin.manager.block')->clearCachedDefinitions();
95     }
96
97     parent::submitForm($form, $form_state);
98   }
99
100 }