ded5b23cdb66c0de43dd78d87b7cab1315b5c588
[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  * @internal
17  */
18 class CommentTypeDeleteForm extends EntityDeleteForm {
19
20   /**
21    * The comment manager service.
22    *
23    * @var \Drupal\comment\CommentManagerInterface
24    */
25   protected $commentManager;
26
27   /**
28    * The entity manager service.
29    *
30    * @var \Drupal\Core\Entity\EntityManager
31    */
32   protected $entityManager;
33
34   /**
35    * A logger instance.
36    *
37    * @var \Psr\Log\LoggerInterface
38    */
39   protected $logger;
40
41   /**
42    * The entity being used by this form.
43    *
44    * @var \Drupal\comment\CommentTypeInterface
45    */
46   protected $entity;
47
48   /**
49    * Constructs a query factory object.
50    *
51    * @param \Drupal\comment\CommentManagerInterface $comment_manager
52    *   The comment manager service.
53    * @param \Drupal\Core\Entity\EntityManager $entity_manager
54    *   The entity manager service.
55    * @param \Psr\Log\LoggerInterface $logger
56    *   A logger instance.
57    */
58   public function __construct(CommentManagerInterface $comment_manager, EntityManager $entity_manager, LoggerInterface $logger) {
59     $this->commentManager = $comment_manager;
60     $this->entityManager = $entity_manager;
61     $this->logger = $logger;
62   }
63
64   /**
65    * {@inheritdoc}
66    */
67   public static function create(ContainerInterface $container) {
68     return new static(
69       $container->get('comment.manager'),
70       $container->get('entity.manager'),
71       $container->get('logger.factory')->get('comment')
72     );
73   }
74
75   /**
76    * {@inheritdoc}
77    */
78   public function buildForm(array $form, FormStateInterface $form_state) {
79     $comments = $this->entityTypeManager->getStorage('comment')->getQuery()
80       ->condition('comment_type', $this->entity->id())
81       ->execute();
82     $entity_type = $this->entity->getTargetEntityTypeId();
83     $caption = '';
84     foreach (array_keys($this->commentManager->getFields($entity_type)) as $field_name) {
85       /** @var \Drupal\field\FieldStorageConfigInterface $field_storage */
86       if (($field_storage = FieldStorageConfig::loadByName($entity_type, $field_name)) && $field_storage->getSetting('comment_type') == $this->entity->id() && !$field_storage->isDeleted()) {
87         $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.', [
88           '%label' => $this->entity->label(),
89           '%field' => $field_storage->label(),
90         ]) . '</p>';
91       }
92     }
93
94     if (!empty($comments)) {
95       $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>';
96     }
97     if ($caption) {
98       $form['description'] = ['#markup' => $caption];
99       return $form;
100     }
101     else {
102       return parent::buildForm($form, $form_state);
103     }
104   }
105
106 }