3 namespace Drupal\comment\Form;
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;
14 * Provides a confirmation form for deleting a comment type entity.
18 class CommentTypeDeleteForm extends EntityDeleteForm {
21 * The comment manager service.
23 * @var \Drupal\comment\CommentManagerInterface
25 protected $commentManager;
28 * The entity manager service.
30 * @var \Drupal\Core\Entity\EntityManager
32 protected $entityManager;
37 * @var \Psr\Log\LoggerInterface
42 * The entity being used by this form.
44 * @var \Drupal\comment\CommentTypeInterface
49 * Constructs a query factory object.
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
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;
67 public static function create(ContainerInterface $container) {
69 $container->get('comment.manager'),
70 $container->get('entity.manager'),
71 $container->get('logger.factory')->get('comment')
78 public function buildForm(array $form, FormStateInterface $form_state) {
79 $comments = $this->entityTypeManager->getStorage('comment')->getQuery()
80 ->condition('comment_type', $this->entity->id())
82 $entity_type = $this->entity->getTargetEntityTypeId();
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(),
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>';
98 $form['description'] = ['#markup' => $caption];
102 return parent::buildForm($form, $form_state);