a181858b86309acd7a8372ad626ef947efd94107
[yaffs-website] / web / core / modules / media / src / Form / MediaTypeDeleteConfirmForm.php
1 <?php
2
3 namespace Drupal\media\Form;
4
5 use Drupal\Core\Entity\EntityTypeManagerInterface;
6 use Drupal\Core\Entity\EntityDeleteForm;
7 use Drupal\Core\Form\FormStateInterface;
8 use Symfony\Component\DependencyInjection\ContainerInterface;
9
10 /**
11  * Provides a form for media type deletion.
12  */
13 class MediaTypeDeleteConfirmForm extends EntityDeleteForm {
14
15   /**
16    * The entity type manager.
17    *
18    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
19    */
20   protected $entityTypeManager;
21
22   /**
23    * Constructs a new MediaTypeDeleteConfirm object.
24    *
25    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
26    *   The entity type manager.
27    */
28   public function __construct(EntityTypeManagerInterface $entity_type_manager) {
29     $this->entityTypeManager = $entity_type_manager;
30   }
31
32   /**
33    * {@inheritdoc}
34    */
35   public static function create(ContainerInterface $container) {
36     return new static(
37       $container->get('entity_type.manager')
38     );
39   }
40
41   /**
42    * {@inheritdoc}
43    */
44   public function buildForm(array $form, FormStateInterface $form_state) {
45     $num_entities = $this->entityTypeManager->getStorage('media')->getQuery()
46       ->condition('bundle', $this->entity->id())
47       ->count()
48       ->execute();
49     if ($num_entities) {
50       $form['#title'] = $this->getQuestion();
51       $form['description'] = [
52         '#type' => 'inline_template',
53         '#template' => '<p>{{ message }}</p>',
54         '#context' => [
55           'message' => $this->formatPlural($num_entities,
56             '%type is used by @count media item on your site. You can not remove this media type until you have removed all of the %type media items.',
57             '%type is used by @count media items on your site. You can not remove this media type until you have removed all of the %type media items.',
58             ['%type' => $this->entity->label()]),
59         ],
60       ];
61
62       return $form;
63     }
64
65     return parent::buildForm($form, $form_state);
66   }
67
68 }