3 namespace Drupal\crop\Form;
5 use Drupal\Core\Entity\EntityConfirmFormBase;
6 use Drupal\Core\Entity\Query\QueryFactory;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\StringTranslation\TranslationInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
13 * Provides a form for crop type deletion.
15 class CropTypeDeleteForm extends EntityConfirmFormBase {
18 * The query factory to create entity queries.
20 * @var \Drupal\Core\Entity\Query\QueryFactory
22 protected $queryFactory;
25 * String translation manager.
27 * @var \Drupal\Core\StringTranslation\TranslationInterface
29 protected $translation;
32 * Constructs a new CropTypeDeleteForm object.
34 * @param \Drupal\Core\Entity\Query\QueryFactory $query_factory
35 * The entity query object.
36 * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
37 * The string translation manager.
39 public function __construct(QueryFactory $query_factory, TranslationInterface $string_translation) {
40 $this->queryFactory = $query_factory;
41 $this->translation = $string_translation;
47 public static function create(ContainerInterface $container) {
49 $container->get('entity.query'),
50 $container->get('string_translation')
57 public function getQuestion() {
58 return t('Are you sure you want to delete the crop type %type?', array('%type' => $this->entity->label()));
64 public function getCancelUrl() {
65 return new Url('crop.overview_types');
71 public function getConfirmText() {
78 public function buildForm(array $form, FormStateInterface $form_state) {
79 $count = $this->queryFactory->get('crop')
80 ->condition('type', $this->entity->id())
84 $form['#title'] = $this->getQuestion();
85 $form['description'] = [
87 '#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.', array('%type' => $this->entity->label())),
93 return parent::buildForm($form, $form_state);
99 public function submitForm(array &$form, FormStateInterface $form_state) {
100 $this->entity->delete();
101 $t_args = array('%name' => $this->entity->label());
102 drupal_set_message($this->t('The crop type %name has been deleted.', $t_args));
103 $this->logger('crop')->notice('Deleted crop type %name.', $t_args);
105 $form_state->setRedirectUrl($this->getCancelUrl());