You are here

AbjsExperienceDeleteConfirmForm.php in A/B Test JS 8

Same filename and directory in other branches
  1. 2.0.x src/Form/AbjsExperienceDeleteConfirmForm.php

Namespace

Drupal\abjs\Form

File

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

namespace Drupal\abjs\Form;

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

/**
 * Class for confirm delete experience.
 */
class AbjsExperienceDeleteConfirmForm extends ConfirmFormBase {
  use StringTranslationTrait;

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

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

  /**
   * Construct method.
   *
   * @param \Drupal\Core\Database\Connection $database
   *   Provides database connection service.
   */
  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_experience_delete_confirm';
  }

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

  /**
   * {@inheritdoc}
   */
  public function getCancelUrl() {
    return new Url('abjs.experience_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');
  }

  /**
   * Building form.
   *
   * @param array $form
   *   The form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The state of forms.
   * @param int $eid
   *   The ID of the item to be deleted.
   */
  public function buildForm(array $form, FormStateInterface $form_state, $eid = NULL) {
    $this->id = $eid;
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->database
      ->delete('abjs_experience')
      ->condition('eid', $this->id)
      ->execute();
    $this->database
      ->delete('abjs_test_experience')
      ->condition('eid', $this->id)
      ->execute();
    $this
      ->messenger()
      ->addMessage($this
      ->t('Experience %id has been deleted.', [
      '%id' => $this->id,
    ]));
    $form_state
      ->setRedirect('abjs.experience_admin');
  }

}

Classes

Namesort descending Description
AbjsExperienceDeleteConfirmForm Class for confirm delete experience.