You are here

AbjsConditionDeleteConfirmForm.php in A/B Test JS 2.0.x

Same filename and directory in other branches
  1. 8 src/Form/AbjsConditionDeleteConfirmForm.php

Namespace

Drupal\abjs\Form

File

src/Form/AbjsConditionDeleteConfirmForm.php
View source
<?php

namespace Drupal\abjs\Form;

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

/**
 * Class for confirm delete condition.
 */
class AbjsConditionDeleteConfirmForm extends ConfirmFormBase {

  /**
   * The ID of the item to delete.
   *
   * @var string
   */
  protected $id;

  /**
   * The database connection.
   *
   * @var Drupal\Core\Database\Connection
   */
  protected $database;

  /**
   * Class constructor.
   */
  public function __construct(Connection $database) {
    $this->database = $database;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('database'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'abjs_condition_delete_confirm';
  }

  /**
   * {@inheritdoc}
   */
  public function getQuestion() {
    return $this
      ->t('Do you want to delete condition %id?', [
      '%id' => $this->id,
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function getCancelUrl() {
    return new Url('abjs.condition_admin');
  }

  /**
   * {@inheritdoc}
   */
  public function getDescription() {
    return $this
      ->t('This action cannot be undone.');
  }

  /**
   * {@inheritdoc}
   */
  public function getConfirmText() {
    return $this
      ->t('Delete');
  }

  /**
   * {@inheritdoc}
   */
  public function getCancelText() {
    return $this
      ->t('Cancel');
  }

  /**
   * Build form.
   *
   * @param array $form
   *   The form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   Provides an object containing the current state of a form.
   * @param int $cid
   *   The ID of the item to be deleted.
   */
  public function buildForm(array $form, FormStateInterface $form_state, $cid = NULL) {
    $this->id = $cid;
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->database
      ->delete('abjs_condition')
      ->condition('cid', $this->id)
      ->execute();
    $this->database
      ->delete('abjs_test_condition')
      ->condition('cid', $this->id)
      ->execute();
    $this
      ->messenger()
      ->addStatus($this
      ->t('Condition %id has been deleted.', [
      '%id' => $this->id,
    ]));
    $form_state
      ->setRedirect('abjs.condition_admin');
  }

}

Classes

Namesort descending Description
AbjsConditionDeleteConfirmForm Class for confirm delete condition.