You are here

CropTypeDeleteForm.php in Crop API 8

Same filename and directory in other branches
  1. 8.2 src/Form/CropTypeDeleteForm.php

Namespace

Drupal\crop\Form

File

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

namespace Drupal\crop\Form;

use Drupal\Core\Entity\EntityConfirmFormBase;
use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a form for crop type deletion.
 */
class CropTypeDeleteForm extends EntityConfirmFormBase {

  /**
   * The query factory to create entity queries.
   *
   * @var \Drupal\Core\Entity\Query\QueryFactory
   */
  protected $queryFactory;

  /**
   * String translation manager.
   *
   * @var \Drupal\Core\StringTranslation\TranslationInterface
   */
  protected $translation;

  /**
   * Constructs a new CropTypeDeleteForm object.
   *
   * @param \Drupal\Core\Entity\Query\QueryFactory $query_factory
   *   The entity query object.
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
   *   The string translation manager.
   */
  public function __construct(QueryFactory $query_factory, TranslationInterface $string_translation) {
    $this->queryFactory = $query_factory;
    $this->translation = $string_translation;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('entity.query'), $container
      ->get('string_translation'));
  }

  /**
   * {@inheritdoc}
   */
  public function getQuestion() {
    return t('Are you sure you want to delete the crop type %type?', [
      '%type' => $this->entity
        ->label(),
    ]);
  }

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $count = $this->queryFactory
      ->get('crop')
      ->condition('type', $this->entity
      ->id())
      ->count()
      ->execute();
    if ($count) {
      $form['#title'] = $this
        ->getQuestion();
      $form['description'] = [
        '#prefix' => '<p>',
        '#markup' => $this->translation
          ->formatPlural($count, '%type is used by 1 piece of content on your site. You can not remove this content type until you have removed all of the %type content.', '%type is used by @count pieces of content on your site. You may not remove %type until you have removed all of the %type content.', [
          '%type' => $this->entity
            ->label(),
        ]),
        '#suffix' => '</p>',
      ];
      return $form;
    }
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->entity
      ->delete();
    $t_args = [
      '%name' => $this->entity
        ->label(),
    ];
    drupal_set_message($this
      ->t('The crop type %name has been deleted.', $t_args));
    $this
      ->logger('crop')
      ->notice('Deleted crop type %name.', $t_args);
    $form_state
      ->setRedirectUrl($this
      ->getCancelUrl());
  }

}

Classes

Namesort descending Description
CropTypeDeleteForm Provides a form for crop type deletion.