Version 1
[yaffs-website] / web / modules / contrib / crop / src / Form / CropTypeDeleteForm.php
1 <?php
2
3 namespace Drupal\crop\Form;
4
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;
9 use Drupal\Core\Url;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11
12 /**
13  * Provides a form for crop type deletion.
14  */
15 class CropTypeDeleteForm extends EntityConfirmFormBase {
16
17   /**
18    * The query factory to create entity queries.
19    *
20    * @var \Drupal\Core\Entity\Query\QueryFactory
21    */
22   protected $queryFactory;
23
24   /**
25    * String translation manager.
26    *
27    * @var \Drupal\Core\StringTranslation\TranslationInterface
28    */
29   protected $translation;
30
31   /**
32    * Constructs a new CropTypeDeleteForm object.
33    *
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.
38    */
39   public function __construct(QueryFactory $query_factory, TranslationInterface $string_translation) {
40     $this->queryFactory = $query_factory;
41     $this->translation = $string_translation;
42   }
43
44   /**
45    * {@inheritdoc}
46    */
47   public static function create(ContainerInterface $container) {
48     return new static(
49       $container->get('entity.query'),
50       $container->get('string_translation')
51     );
52   }
53
54   /**
55    * {@inheritdoc}
56    */
57   public function getQuestion() {
58     return t('Are you sure you want to delete the crop type %type?', array('%type' => $this->entity->label()));
59   }
60
61   /**
62    * {@inheritdoc}
63    */
64   public function getCancelUrl() {
65     return new Url('crop.overview_types');
66   }
67
68   /**
69    * {@inheritdoc}
70    */
71   public function getConfirmText() {
72     return t('Delete');
73   }
74
75   /**
76    * {@inheritdoc}
77    */
78   public function buildForm(array $form, FormStateInterface $form_state) {
79     $count = $this->queryFactory->get('crop')
80       ->condition('type', $this->entity->id())
81       ->count()
82       ->execute();
83     if ($count) {
84       $form['#title'] = $this->getQuestion();
85       $form['description'] = [
86         '#prefix' => '<p>',
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())),
88         '#suffix' => '</p>',
89       ];
90       return $form;
91     }
92
93     return parent::buildForm($form, $form_state);
94   }
95
96   /**
97    * {@inheritdoc}
98    */
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);
104
105     $form_state->setRedirectUrl($this->getCancelUrl());
106   }
107
108 }