StatisticsSettingsForm.php 2.58 KB
Newer Older
1 2 3 4 5 6 7 8
<?php
/**
 * @file
 * Contains \Drupal\statistics\StatisticsSettingsForm.
 */

namespace Drupal\statistics;

9
use Drupal\Core\Extension\ModuleHandlerInterface;
10
use Drupal\Core\Form\ConfigFormBase;
11 12 13 14 15 16
use Drupal\Core\Config\ConfigFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Configure statistics settings for this site.
 */
17
class StatisticsSettingsForm extends ConfigFormBase {
18 19 20 21

  /**
   * The module handler.
   *
22
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
23 24 25 26 27 28 29 30
   */
  protected $moduleHandler;

  /**
   * Constructs a \Drupal\user\StatisticsSettingsForm object.
   *
   * @param \Drupal\Core\Config\ConfigFactory $config_factory
   *   The factory for configuration objects.
31
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
32 33
   *   The module handler.
   */
34 35
  public function __construct(ConfigFactory $config_factory, ModuleHandlerInterface $module_handler) {
    parent::__construct($config_factory);
36

37 38 39 40
    $this->moduleHandler = $module_handler;
  }

  /**
41
   * {@inheritdoc}
42 43 44 45 46 47 48 49 50
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('config.factory'),
      $container->get('module_handler')
    );
  }

  /**
51
   * {@inheritdoc}
52
   */
53
  public function getFormId() {
54 55 56 57 58 59 60
    return 'statistics_settings_form';
  }

  /**
   * Implements \Drupal\Core\Form\FormInterface::buildForm().
   */
  public function buildForm(array $form, array &$form_state) {
61
    $config = $this->config('statistics.settings');
62 63 64 65 66

    // Content counter settings.
    $form['content'] = array(
      '#type' => 'details',
      '#title' => t('Content viewing counter settings'),
67
      '#open' => TRUE,
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    );
    $form['content']['statistics_count_content_views'] = array(
      '#type' => 'checkbox',
      '#title' => t('Count content views'),
      '#default_value' => $config->get('count_content_views'),
      '#description' => t('Increment a counter each time content is viewed.'),
    );

    return parent::buildForm($form, $form_state);
  }

  /**
   * Implements \Drupal\Core\Form\FormInterface::submitForm().
   */
  public function submitForm(array &$form, array &$form_state) {
83
    $this->config('statistics.settings')
84 85 86 87 88 89
      ->set('count_content_views', $form_state['values']['statistics_count_content_views'])
      ->save();

    // The popular statistics block is dependent on these settings, so clear the
    // block plugin definitions cache.
    if ($this->moduleHandler->moduleExists('block')) {
90
      \Drupal::service('plugin.manager.block')->clearCachedDefinitions();
91 92 93 94 95
    }

    parent::submitForm($form, $form_state);
  }
}
96