You are here

UnitDeleteMultiple.php in Booking and Availability Management Tools for Drupal 8

File

modules/bat_unit/src/Form/UnitDeleteMultiple.php
View source
<?php

/**
 * @file
 * Contains \Drupal\bat_unit\Form\UnitDeleteMultiple.
 */
namespace Drupal\bat_unit\Form;

use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\TempStore\PrivateTempStoreFactory;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a unit deletion confirmation form.
 */
class UnitDeleteMultiple extends ConfirmFormBase {

  /**
   * The array of units to delete.
   *
   * @var string[][]
   */
  protected $unitInfo = [];

  /**
   * The tempstore factory.
   *
   * @var \Drupal\Core\TempStore\PrivateTempStoreFactory
   */
  protected $tempStoreFactory;

  /**
   * Constructs a DeleteMultiple form object.
   *
   * @param \Drupal\Core\TempStore\PrivateTempStoreFactory $temp_store_factory
   *   The tempstore factory.
   */
  public function __construct(PrivateTempStoreFactory $temp_store_factory) {
    $this->tempStoreFactory = $temp_store_factory;
  }

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

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

  /**
   * {@inheritdoc}
   */
  public function getQuestion() {
    return $this
      ->formatPlural(count($this->unitInfo), 'Are you sure you want to delete this unit?', 'Are you sure you want to delete these units?');
  }

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $this->unitInfo = $this->tempStoreFactory
      ->get('unit_multiple_delete_confirm')
      ->get($this
      ->currentUser()
      ->id());
    $form = parent::buildForm($form, $form_state);
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    if ($form_state
      ->getValue('confirm') && !empty($this->unitInfo)) {
      bat_unit_delete_multiple(array_keys($this->unitInfo));
    }
  }

}

Classes

Namesort descending Description
UnitDeleteMultiple Provides a unit deletion confirmation form.