Commit 35551377 authored by webchick's avatar webchick

Issue #2112675 by tim.plunkett: Convert dblog_filter_form and...

Issue #2112675 by tim.plunkett: Convert dblog_filter_form and dblog_clear_log_form to FormInterface.
parent 8e5c3958
......@@ -108,114 +108,3 @@ function dblog_filters() {
return $filters;
}
/**
* Form constructor for the database logging filter form.
*
* @see dblog_filter_form_validate()
* @see dblog_filter_form_submit()
* @see dblog_overview()
*
* @ingroup forms
*/
function dblog_filter_form($form) {
$filters = dblog_filters();
$form['filters'] = array(
'#type' => 'details',
'#title' => t('Filter log messages'),
'#collapsed' => empty($_SESSION['dblog_overview_filter']),
);
foreach ($filters as $key => $filter) {
$form['filters']['status'][$key] = array(
'#title' => $filter['title'],
'#type' => 'select',
'#multiple' => TRUE,
'#size' => 8,
'#options' => $filter['options'],
);
if (!empty($_SESSION['dblog_overview_filter'][$key])) {
$form['filters']['status'][$key]['#default_value'] = $_SESSION['dblog_overview_filter'][$key];
}
}
$form['filters']['actions'] = array(
'#type' => 'actions',
'#attributes' => array('class' => array('container-inline')),
);
$form['filters']['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Filter'),
);
if (!empty($_SESSION['dblog_overview_filter'])) {
$form['filters']['actions']['reset'] = array(
'#type' => 'submit',
'#value' => t('Reset')
);
}
return $form;
}
/**
* Form validation handler for dblog_filter_form().
*
* @see dblog_filter_form_submit()
*/
function dblog_filter_form_validate($form, &$form_state) {
if ($form_state['values']['op'] == t('Filter') && empty($form_state['values']['type']) && empty($form_state['values']['severity'])) {
form_set_error('type', $form_state, t('You must select something to filter by.'));
}
}
/**
* Form submission handler for dblog_filter_form().
*
* @see dblog_filter_form_validate()
*/
function dblog_filter_form_submit($form, &$form_state) {
$op = $form_state['values']['op'];
$filters = dblog_filters();
switch ($op) {
case t('Filter'):
foreach ($filters as $name => $filter) {
if (isset($form_state['values'][$name])) {
$_SESSION['dblog_overview_filter'][$name] = $form_state['values'][$name];
}
}
break;
case t('Reset'):
$_SESSION['dblog_overview_filter'] = array();
break;
}
return 'admin/reports/dblog';
}
/**
* Form constructor for the form that clears out the log.
*
* @see dblog_clear_log_submit()
* @ingroup forms
*/
function dblog_clear_log_form($form) {
$form['dblog_clear'] = array(
'#type' => 'details',
'#title' => t('Clear log messages'),
'#description' => t('This will permanently remove the log messages from the database.'),
'#collapsed' => TRUE,
);
$form['dblog_clear']['clear'] = array(
'#type' => 'submit',
'#value' => t('Clear log messages'),
'#submit' => array('dblog_clear_log_submit'),
);
return $form;
}
/**
* Form submission handler for dblog_clear_log_form().
*/
function dblog_clear_log_submit() {
$_SESSION['dblog_overview_filter'] = array();
db_delete('watchdog')->execute();
drupal_set_message(t('Database log cleared.'));
}
......@@ -15,7 +15,7 @@
use Drupal\Core\Datetime\Date;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\user\UserStorageControllerInterface;
use Drupal\Core\Form\FormBuilderInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
......@@ -43,10 +43,13 @@ class DbLogController extends ControllerBase implements ContainerInjectionInterf
* @var \Drupal\Core\Datetime\Date
*/
protected $date;
/**
* @var \Drupal\user\UserStorageControllerInterface
* The form builder service.
*
* @var \Drupal\Core\Form\FormBuilderInterface
*/
protected $userStorage;
protected $formBuilder;
/**
* {@inheritdoc}
......@@ -56,23 +59,27 @@ public static function create(ContainerInterface $container) {
$container->get('database'),
$container->get('module_handler'),
$container->get('date'),
$container->get('entity.manager')->getStorageController('user')
$container->get('form_builder')
);
}
/**
* Constructs a DbLogController object.
*
* @param Connection $database
* @param \Drupal\Core\Database\Connection $database
* A database connection.
* @param ModuleHandlerInterface $module_handler
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* A module handler.
* @param \Drupal\Core\Datetime\Date $date
* The date service.
* @param \Drupal\Core\Form\FormBuilderInterface $form_builder
* The form builder service.
*/
public function __construct(Connection $database, ModuleHandlerInterface $module_handler, Date $date, UserStorageControllerInterface $user_storage) {
public function __construct(Connection $database, ModuleHandlerInterface $module_handler, Date $date, FormBuilderInterface $form_builder) {
$this->database = $database;
$this->moduleHandler = $module_handler;
$this->date = $date;
$this->userStorage = $user_storage;
$this->formBuilder = $form_builder;
}
/**
......@@ -105,7 +112,6 @@ public static function getLogLevelClassMap() {
*
* @see dblog_clear_log_form()
* @see dblog_event()
* @see dblog_filter_form()
*/
public function overview() {
......@@ -116,8 +122,8 @@ public function overview() {
$this->moduleHandler->loadInclude('dblog', 'admin.inc');
$build['dblog_filter_form'] = drupal_get_form('dblog_filter_form');
$build['dblog_clear_log_form'] = drupal_get_form('dblog_clear_log_form');
$build['dblog_filter_form'] = $this->formBuilder->getForm('Drupal\dblog\Form\DblogFilterForm');
$build['dblog_clear_log_form'] = $this->formBuilder->getForm('Drupal\dblog\Form\DblogClearLogForm');
$header = array(
// Icon column.
......
<?php
/**
* @file
* Contains \Drupal\dblog\Form\DblogClearLogForm.
*/
namespace Drupal\dblog\Form;
use Drupal\Core\Database\Connection;
use Drupal\Core\Form\FormBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides the form that clears out the log.
*/
class DblogClearLogForm extends FormBase {
/**
* The database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $connection;
/**
* Constructs a new DblogClearLogForm.
*
* @param \Drupal\Core\Database\Connection $connection
* The database connection.
*/
public function __construct(Connection $connection) {
$this->connection = $connection;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('database')
);
}
/**
* {@inheritdoc}
*/
public function getFormID() {
return 'dblog_clear_log_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state) {
$form['dblog_clear'] = array(
'#type' => 'details',
'#title' => $this->t('Clear log messages'),
'#description' => $this->t('This will permanently remove the log messages from the database.'),
'#collapsed' => TRUE,
);
$form['dblog_clear']['clear'] = array(
'#type' => 'submit',
'#value' => $this->t('Clear log messages'),
);
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, array &$form_state) {
$_SESSION['dblog_overview_filter'] = array();
$this->connection->delete('watchdog')->execute();
drupal_set_message($this->t('Database log cleared.'));
}
}
<?php
/**
* @file
* Contains \Drupal\dblog\Form\DblogFilterForm.
*/
namespace Drupal\dblog\Form;
use Drupal\Core\Form\FormBase;
/**
* Provides the database logging filter form.
*/
class DblogFilterForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormID() {
return 'dblog_filter_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state) {
$filters = dblog_filters();
$form['filters'] = array(
'#type' => 'details',
'#title' => $this->t('Filter log messages'),
'#collapsed' => empty($_SESSION['dblog_overview_filter']),
);
foreach ($filters as $key => $filter) {
$form['filters']['status'][$key] = array(
'#title' => $filter['title'],
'#type' => 'select',
'#multiple' => TRUE,
'#size' => 8,
'#options' => $filter['options'],
);
if (!empty($_SESSION['dblog_overview_filter'][$key])) {
$form['filters']['status'][$key]['#default_value'] = $_SESSION['dblog_overview_filter'][$key];
}
}
$form['filters']['actions'] = array(
'#type' => 'actions',
'#attributes' => array('class' => array('container-inline')),
);
$form['filters']['actions']['submit'] = array(
'#type' => 'submit',
'#value' => $this->t('Filter'),
);
if (!empty($_SESSION['dblog_overview_filter'])) {
$form['filters']['actions']['reset'] = array(
'#type' => 'submit',
'#value' => $this->t('Reset'),
'#limit_validation_errors' => array(),
'#submit' => array(array($this, 'resetForm')),
);
}
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, array &$form_state) {
if (empty($form_state['values']['type']) && empty($form_state['values']['severity'])) {
$this->setFormError('type', $form_state, $this->t('You must select something to filter by.'));
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, array &$form_state) {
$filters = dblog_filters();
foreach ($filters as $name => $filter) {
if (isset($form_state['values'][$name])) {
$_SESSION['dblog_overview_filter'][$name] = $form_state['values'][$name];
}
}
}
/**
* Resets the filter form.
*/
public function resetForm(array &$form, array &$form_state) {
$_SESSION['dblog_overview_filter'] = array();
}
}
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