diff --git a/core/modules/statistics/src/StatisticsSettingsForm.php b/core/modules/statistics/src/StatisticsSettingsForm.php index 85c455bfae..91260ec10c 100644 --- a/core/modules/statistics/src/StatisticsSettingsForm.php +++ b/core/modules/statistics/src/StatisticsSettingsForm.php @@ -2,6 +2,7 @@ namespace Drupal\statistics; +use Drupal\Core\Database\Connection; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Form\ConfigFormBase; use Drupal\Core\Config\ConfigFactoryInterface; @@ -22,6 +23,13 @@ class StatisticsSettingsForm extends ConfigFormBase { */ protected $moduleHandler; + /** + * The database service. + * + * @var \Drupal\Core\Database\Connection + */ + protected $connection; + /** * Constructs a \Drupal\statistics\StatisticsSettingsForm object. * @@ -29,11 +37,14 @@ class StatisticsSettingsForm extends ConfigFormBase { * The factory for configuration objects. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler * The module handler. + * @param \Drupal\Core\Database\Connection $connection + * The database connection. */ - public function __construct(ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler) { + public function __construct(ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler, Connection $connection) { parent::__construct($config_factory); $this->moduleHandler = $module_handler; + $this->connection = $connection; } /** @@ -42,7 +53,8 @@ public function __construct(ConfigFactoryInterface $config_factory, ModuleHandle public static function create(ContainerInterface $container) { return new static( $container->get('config.factory'), - $container->get('module_handler') + $container->get('module_handler'), + $container->get('database') ); } @@ -79,9 +91,29 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#description' => t('Increment a counter each time content is viewed.'), ]; + $form['reset_statistics'] = [ + '#type' => 'details', + '#title' => $this->t('Reset site statistics'), + '#open' => TRUE, + ]; + + $form['reset_statistics']['reset'] = [ + '#type' => 'submit', + '#value' => $this->t('Reset'), + '#submit' => ['::statisticsResetSubmit'], + ]; + return parent::buildForm($form, $form_state); } + /** + * Submit callback to clear statistics data. + */ + public function statisticsResetSubmit(array &$form, FormStateInterface $form_state) { + $this->connection->truncate('node_counter')->execute(); + $this->messenger()->addStatus($this->t('Cleared statistics data.')); + } + /** * {@inheritdoc} */ diff --git a/core/modules/statistics/tests/src/Functional/StatisticsAdminTest.php b/core/modules/statistics/tests/src/Functional/StatisticsAdminTest.php index 33ddb136c9..cd5ab2915a 100644 --- a/core/modules/statistics/tests/src/Functional/StatisticsAdminTest.php +++ b/core/modules/statistics/tests/src/Functional/StatisticsAdminTest.php @@ -177,4 +177,23 @@ public function testExpiredLogs() { $this->assertEmpty($result, 'Daycounter is zero.'); } + /** + * Log in as admin user and clear the statistics tables. + */ + public function testStatisticsClear() { + // Enable access logging. + \Drupal::configFactory()->getEditable('statistics.settings')->set('count_content_views', '1')->save(); + + // Log in the admin user. + $this->drupalLogin($this->privilegedUser); + + // Make post to clear the tables. + $this->drupalPost('admin/config/system/statistics', [], 'Reset'); + + // Test node_counter table. + $connection = \Drupal::database(); + $count = $connection->select('node_counter')->countQuery()->execute()->fetchField(); + $this->assertEqual($count, 0, 'The node_counter table contains 0 records after a clear.'); + } + }