Version 1
[yaffs-website] / web / core / modules / comment / src / Form / CommentTypeDeleteForm.php
1 <?php
2
3 namespace Drupal\comment\Form;
4
5 use Drupal\comment\CommentManagerInterface;
6 use Drupal\Core\Entity\EntityDeleteForm;
7 use Drupal\Core\Entity\EntityManager;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\field\Entity\FieldStorageConfig;
10 use Psr\Log\LoggerInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12
13 /**
14  * Provides a confirmation form for deleting a comment type entity.
15  */
16 class CommentTypeDeleteForm extends EntityDeleteForm {
17
18   /**
19    * The comment manager service.
20    *
21    * @var \Drupal\comment\CommentManagerInterface
22    */
23   protected $commentManager;
24
25   /**
26    * The entity manager service.
27    *
28    * @var \Drupal\Core\Entity\EntityManager
29    */
30   protected $entityManager;
31
32   /**
33    * A logger instance.
34    *
35    * @var \Psr\Log\LoggerInterface
36    */
37   protected $logger;
38
39   /**
40    * The entity being used by this form.
41    *
42    * @var \Drupal\comment\CommentTypeInterface
43    */
44   protected $entity;
45
46   /**
47    * Constructs a query factory object.
48    *
49    * @param \Drupal\comment\CommentManagerInterface $comment_manager
50    *   The comment manager service.
51    * @param \Drupal\Core\Entity\EntityManager $entity_manager
52    *   The entity manager service.
53    * @param \Psr\Log\LoggerInterface $logger
54    *   A logger instance.
55    */
56   public function __construct(CommentManagerInterface $comment_manager, EntityManager $entity_manager, LoggerInterface $logger) {
57     $this->commentManager = $comment_manager;
58     $this->entityManager = $entity_manager;
59     $this->logger = $logger;
60   }
61
62   /**
63    * {@inheritdoc}
64    */
65   public static function create(ContainerInterface $container) {
66     return new static(
67       $container->get('comment.manager'),
68       $container->get('entity.manager'),
69       $container->get('logger.factory')->get('comment')
70     );
71   }
72
73   /**
74    * {@inheritdoc}
75    */
76   public function buildForm(array $form, FormStateInterface $form_state) {
77     $comments = $this->entityTypeManager->getStorage('comment')->getQuery()
78       ->condition('comment_type', $this->entity->id())
79       ->execute();
80     $entity_type = $this->entity->getTargetEntityTypeId();
81     $caption = '';
82     foreach (array_keys($this->commentManager->getFields($entity_type)) as $field_name) {
83       /** @var \Drupal\field\FieldStorageConfigInterface $field_storage */
84       if (($field_storage = FieldStorageConfig::loadByName($entity_type, $field_name)) && $field_storage->getSetting('comment_type') == $this->entity->id() && !$field_storage->isDeleted()) {
85         $caption .= '<p>' . $this->t('%label is used by the %field field on your site. You can not remove this comment type until you have removed the field.', [
86           '%label' => $this->entity->label(),
87           '%field' => $field_storage->label(),
88         ]) . '</p>';
89       }
90     }
91
92     if (!empty($comments)) {
93       $caption .= '<p>' . $this->formatPlural(count($comments), '%label is used by 1 comment on your site. You can not remove this comment type until you have removed all of the %label comments.', '%label is used by @count comments on your site. You may not remove %label until you have removed all of the %label comments.', ['%label' => $this->entity->label()]) . '</p>';
94     }
95     if ($caption) {
96       $form['description'] = ['#markup' => $caption];
97       return $form;
98     }
99     else {
100       return parent::buildForm($form, $form_state);
101     }
102   }
103
104 }