Commit 96903932 authored by webchick's avatar webchick

Issue #1937968 by ACF: Convert statistics's system_config_form() to SystemConfigFormBase.

parent 232cfc74
<?php
/**
* @file
* Contains \Drupal\statistics\StatisticsSettingsForm.
*/
namespace Drupal\statistics;
use Drupal\system\SystemConfigFormBase;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Extension\ModuleHandler;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Configure statistics settings for this site.
*/
class StatisticsSettingsForm extends SystemConfigFormBase {
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandler
*/
protected $moduleHandler;
/**
* Constructs a \Drupal\user\StatisticsSettingsForm object.
*
* @param \Drupal\Core\Config\ConfigFactory $config_factory
* The factory for configuration objects.
* @param \Drupal\Core\Extension\ModuleHandler $module_handler
* The module handler.
*/
public function __construct(ConfigFactory $config_factory, ModuleHandler $module_handler) {
$this->configFactory = $config_factory;
$this->moduleHandler = $module_handler;
}
/**
* Implements \Drupal\Core\ControllerInterface::create().
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory'),
$container->get('module_handler')
);
}
/**
* Implements \Drupal\Core\Form\FormInterface::getFormID().
*/
public function getFormID() {
return 'statistics_settings_form';
}
/**
* Implements \Drupal\Core\Form\FormInterface::buildForm().
*/
public function buildForm(array $form, array &$form_state) {
$config = $this->configFactory->get('statistics.settings');
// Content counter settings.
$form['content'] = array(
'#type' => 'details',
'#title' => t('Content viewing counter settings'),
);
$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) {
$this->configFactory->get('statistics.settings')
->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')) {
drupal_container()->get('plugin.manager.block')->clearCachedDefinitions();
}
parent::submitForm($form, $form_state);
}
}
<?php
/**
* @file
* Admin page callbacks for the Statistics module.
*/
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* Form constructor for the statistics administration form.
*
* @ingroup forms
* @see statistics_settings_form_submit().
*/
function statistics_settings_form($form, &$form_state) {
$config = config('statistics.settings');
// Content counter settings.
$form['content'] = array(
'#type' => 'details',
'#title' => t('Content viewing counter settings'),
);
$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 system_config_form($form, $form_state);
}
/**
* Form submission handler for statistics_settings_form().
*/
function statistics_settings_form_submit($form, &$form_state) {
config('statistics.settings')
->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 (module_exists('block')) {
drupal_container()->get('plugin.manager.block')->clearCachedDefinitions();
}
}
......@@ -79,10 +79,8 @@ function statistics_menu() {
$items['admin/config/system/statistics'] = array(
'title' => 'Statistics',
'description' => 'Control details about what and how your site logs content statistics.',
'page callback' => 'drupal_get_form',
'page arguments' => array('statistics_settings_form'),
'route_name' => 'statistics_settings',
'access arguments' => array('administer statistics'),
'file' => 'statistics.admin.inc',
'weight' => -15,
);
return $items;
......
statistics_settings:
pattern: '/admin/config/system/statistics'
defaults:
_form: 'Drupal\statistics\StatisticsSettingsForm'
requirements:
_permission: 'administer statistics'
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment