f860eb4e175428fcef57a3ed31a1010614b57c5c
[yaffs-website] / web / modules / contrib / media_entity / src / Form / MediaBundleDeleteConfirm.php
1 <?php
2
3 namespace Drupal\media_entity\Form;
4
5 use Drupal\Core\Entity\Query\QueryFactory;
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 bundle deletion.
12  */
13 class MediaBundleDeleteConfirm extends EntityDeleteForm {
14
15   /**
16    * The query factory to create entity queries.
17    *
18    * @var \Drupal\Core\Entity\Query\QueryFactory
19    */
20   protected $queryFactory;
21
22   /**
23    * Constructs a new MediaBundleDeleteConfirm object.
24    *
25    * @param \Drupal\Core\Entity\Query\QueryFactory $query_factory
26    *   The entity query object.
27    */
28   public function __construct(QueryFactory $query_factory) {
29     $this->queryFactory = $query_factory;
30   }
31
32   /**
33    * {@inheritdoc}
34    */
35   public static function create(ContainerInterface $container) {
36     return new static(
37       $container->get('entity.query')
38     );
39   }
40
41   /**
42    * {@inheritdoc}
43    */
44   public function buildForm(array $form, FormStateInterface $form_state) {
45     $num_entities = $this->queryFactory->get('media')
46       ->condition('bundle', $this->entity->id())
47       ->count()
48       ->execute();
49     if ($num_entities) {
50       $caption = '<p>' . $this->formatPlural($num_entities, '%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()]) . '</p>';
51       $form['#title'] = $this->getQuestion();
52       $form['description'] = ['#markup' => $caption];
53       return $form;
54     }
55
56     return parent::buildForm($form, $form_state);
57   }
58
59 }