Updated to Drupal 8.5. Core Media not yet in use.
[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  * @internal
15  */
16 class StatisticsSettingsForm extends ConfigFormBase {
17
18   /**
19    * The module handler.
20    *
21    * @var \Drupal\Core\Extension\ModuleHandlerInterface
22    */
23   protected $moduleHandler;
24
25   /**
26    * Constructs a \Drupal\statistics\StatisticsSettingsForm object.
27    *
28    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
29    *   The factory for configuration objects.
30    * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
31    *   The module handler.
32    */
33   public function __construct(ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler) {
34     parent::__construct($config_factory);
35
36     $this->moduleHandler = $module_handler;
37   }
38
39   /**
40    * {@inheritdoc}
41    */
42   public static function create(ContainerInterface $container) {
43     return new static(
44       $container->get('config.factory'),
45       $container->get('module_handler')
46     );
47   }
48
49   /**
50    * {@inheritdoc}
51    */
52   public function getFormId() {
53     return 'statistics_settings_form';
54   }
55
56   /**
57    * {@inheritdoc}
58    */
59   protected function getEditableConfigNames() {
60     return ['statistics.settings'];
61   }
62
63   /**
64    * {@inheritdoc}
65    */
66   public function buildForm(array $form, FormStateInterface $form_state) {
67     $config = $this->config('statistics.settings');
68
69     // Content counter settings.
70     $form['content'] = [
71       '#type' => 'details',
72       '#title' => t('Content viewing counter settings'),
73       '#open' => TRUE,
74     ];
75     $form['content']['statistics_count_content_views'] = [
76       '#type' => 'checkbox',
77       '#title' => t('Count content views'),
78       '#default_value' => $config->get('count_content_views'),
79       '#description' => t('Increment a counter each time content is viewed.'),
80     ];
81
82     return parent::buildForm($form, $form_state);
83   }
84
85   /**
86    * {@inheritdoc}
87    */
88   public function submitForm(array &$form, FormStateInterface $form_state) {
89     $this->config('statistics.settings')
90       ->set('count_content_views', $form_state->getValue('statistics_count_content_views'))
91       ->save();
92
93     // The popular statistics block is dependent on these settings, so clear the
94     // block plugin definitions cache.
95     if ($this->moduleHandler->moduleExists('block')) {
96       \Drupal::service('plugin.manager.block')->clearCachedDefinitions();
97     }
98
99     parent::submitForm($form, $form_state);
100   }
101
102 }