3 namespace Drupal\statistics;
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;
12 * Configure statistics settings for this site.
16 class StatisticsSettingsForm extends ConfigFormBase {
21 * @var \Drupal\Core\Extension\ModuleHandlerInterface
23 protected $moduleHandler;
26 * Constructs a \Drupal\statistics\StatisticsSettingsForm object.
28 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
29 * The factory for configuration objects.
30 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
33 public function __construct(ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler) {
34 parent::__construct($config_factory);
36 $this->moduleHandler = $module_handler;
42 public static function create(ContainerInterface $container) {
44 $container->get('config.factory'),
45 $container->get('module_handler')
52 public function getFormId() {
53 return 'statistics_settings_form';
59 protected function getEditableConfigNames() {
60 return ['statistics.settings'];
66 public function buildForm(array $form, FormStateInterface $form_state) {
67 $config = $this->config('statistics.settings');
69 // Content counter settings.
72 '#title' => t('Content viewing counter settings'),
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.'),
82 return parent::buildForm($form, $form_state);
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'))
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();
99 parent::submitForm($form, $form_state);