DblogClearLogConfirmForm.php

Same filename in other branches
  1. 9 core/modules/dblog/src/Form/DblogClearLogConfirmForm.php
  2. 8.9.x core/modules/dblog/src/Form/DblogClearLogConfirmForm.php
  3. 11.x core/modules/dblog/src/Form/DblogClearLogConfirmForm.php

Namespace

Drupal\dblog\Form

File

core/modules/dblog/src/Form/DblogClearLogConfirmForm.php

View source
<?php

namespace Drupal\dblog\Form;

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\Core\Database\Connection;
use Drupal\Core\Form\ConfirmFormBase;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a confirmation form before clearing out the logs.
 *
 * @internal
 */
class DblogClearLogConfirmForm extends ConfirmFormBase {
    
    /**
     * The database connection.
     *
     * @var \Drupal\Core\Database\Connection
     */
    protected $connection;
    
    /**
     * Constructs a new DblogClearLogConfirmForm.
     *
     * @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_confirm';
    }
    
    /**
     * {@inheritdoc}
     */
    public function getQuestion() {
        return $this->t('Are you sure you want to delete the recent logs?');
    }
    
    /**
     * {@inheritdoc}
     */
    public function getCancelUrl() {
        return new Url('dblog.overview');
    }
    
    /**
     * {@inheritdoc}
     */
    public function submitForm(array &$form, FormStateInterface $form_state) {
        $this->getRequest()
            ->getSession()
            ->remove('dblog_overview_filter');
        $this->connection
            ->truncate('watchdog')
            ->execute();
        $this->messenger()
            ->addStatus($this->t('Database log cleared.'));
        $form_state->setRedirectUrl($this->getCancelUrl());
    }

}

Classes

Title Deprecated Summary
DblogClearLogConfirmForm Provides a confirmation form before clearing out the logs.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.