CommentTypeDeleteForm.php in Zircon Profile 8
File
core/modules/comment/src/Form/CommentTypeDeleteForm.php
View source
<?php
namespace Drupal\comment\Form;
use Drupal\comment\CommentManagerInterface;
use Drupal\Core\Entity\EntityDeleteForm;
use Drupal\Core\Entity\EntityManager;
use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\Core\Form\FormStateInterface;
use Drupal\field\Entity\FieldStorageConfig;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class CommentTypeDeleteForm extends EntityDeleteForm {
public $queryFactory;
protected $commentManager;
protected $entityManager;
protected $logger;
protected $entity;
public function __construct(QueryFactory $query_factory, CommentManagerInterface $comment_manager, EntityManager $entity_manager, LoggerInterface $logger) {
$this->queryFactory = $query_factory;
$this->commentManager = $comment_manager;
$this->entityManager = $entity_manager;
$this->logger = $logger;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.query'), $container
->get('comment.manager'), $container
->get('entity.manager'), $container
->get('logger.factory')
->get('comment'));
}
public function buildForm(array $form, FormStateInterface $form_state) {
$comments = $this->queryFactory
->get('comment')
->condition('comment_type', $this->entity
->id())
->execute();
$entity_type = $this->entity
->getTargetEntityTypeId();
$caption = '';
foreach (array_keys($this->commentManager
->getFields($entity_type)) as $field_name) {
if (($field_storage = FieldStorageConfig::loadByName($entity_type, $field_name)) && $field_storage
->getSetting('comment_type') == $this->entity
->id() && !$field_storage
->isDeleted()) {
$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.', array(
'%label' => $this->entity
->label(),
'%field' => $field_storage
->label(),
)) . '</p>';
}
}
if (!empty($comments)) {
$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.', array(
'%label' => $this->entity
->label(),
)) . '</p>';
}
if ($caption) {
$form['description'] = array(
'#markup' => $caption,
);
return $form;
}
else {
return parent::buildForm($form, $form_state);
}
}
}