3 namespace Drupal\comment;
5 use Drupal\Core\Entity\EntityForm;
6 use Drupal\Core\Entity\EntityManagerInterface;
7 use Drupal\Core\Entity\EntityTypeInterface;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\language\Entity\ContentLanguageSettings;
10 use Psr\Log\LoggerInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
14 * Base form handler for comment type edit forms.
18 class CommentTypeForm extends EntityForm {
21 * Entity manager service.
23 * @var \Drupal\Core\Entity\EntityManagerInterface
25 protected $entityManager;
30 * @var \Psr\Log\LoggerInterface
35 * The comment manager.
37 * @var \Drupal\comment\CommentManagerInterface
39 protected $commentManager;
44 public static function create(ContainerInterface $container) {
46 $container->get('entity.manager'),
47 $container->get('logger.factory')->get('comment'),
48 $container->get('comment.manager')
53 * Constructs a CommentTypeFormController
55 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
56 * The entity manager service.
57 * @param \Psr\Log\LoggerInterface $logger
59 * @param \Drupal\comment\CommentManagerInterface $comment_manager
60 * The comment manager.
62 public function __construct(EntityManagerInterface $entity_manager, LoggerInterface $logger, CommentManagerInterface $comment_manager) {
63 $this->entityManager = $entity_manager;
64 $this->logger = $logger;
65 $this->commentManager = $comment_manager;
71 public function form(array $form, FormStateInterface $form_state) {
72 $form = parent::form($form, $form_state);
74 $comment_type = $this->entity;
77 '#type' => 'textfield',
78 '#title' => t('Label'),
80 '#default_value' => $comment_type->label(),
84 '#type' => 'machine_name',
85 '#default_value' => $comment_type->id(),
87 'exists' => '\Drupal\comment\Entity\CommentType::load',
89 '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
90 '#disabled' => !$comment_type->isNew(),
93 $form['description'] = [
94 '#type' => 'textarea',
95 '#default_value' => $comment_type->getDescription(),
96 '#description' => t('Describe this comment type. The text will be displayed on the <em>Comment types</em> administration overview page.'),
97 '#title' => t('Description'),
100 if ($comment_type->isNew()) {
102 foreach ($this->entityManager->getDefinitions() as $entity_type) {
103 // Only expose entities that have field UI enabled, only those can
104 // get comment fields added in the UI.
105 if ($entity_type->get('field_ui_base_route')) {
106 $options[$entity_type->id()] = $entity_type->getLabel();
109 $form['target_entity_type_id'] = [
111 '#default_value' => $comment_type->getTargetEntityTypeId(),
112 '#title' => t('Target entity type'),
113 '#options' => $options,
114 '#description' => t('The target entity type can not be changed after the comment type has been created.'),
118 $form['target_entity_type_id_display'] = [
120 '#markup' => $this->entityManager->getDefinition($comment_type->getTargetEntityTypeId())->getLabel(),
121 '#title' => t('Target entity type'),
125 if ($this->moduleHandler->moduleExists('content_translation')) {
126 $form['language'] = [
127 '#type' => 'details',
128 '#title' => t('Language settings'),
129 '#group' => 'additional_settings',
132 $language_configuration = ContentLanguageSettings::loadByEntityTypeBundle('comment', $comment_type->id());
133 $form['language']['language_configuration'] = [
134 '#type' => 'language_configuration',
135 '#entity_information' => [
136 'entity_type' => 'comment',
137 'bundle' => $comment_type->id(),
139 '#default_value' => $language_configuration,
142 $form['#submit'][] = 'language_configuration_element_submit';
145 $form['actions'] = ['#type' => 'actions'];
146 $form['actions']['submit'] = [
148 '#value' => t('Save'),
157 public function save(array $form, FormStateInterface $form_state) {
158 $comment_type = $this->entity;
159 $status = $comment_type->save();
161 $edit_link = $this->entity->link($this->t('Edit'));
162 if ($status == SAVED_UPDATED) {
163 $this->messenger()->addStatus(t('Comment type %label has been updated.', ['%label' => $comment_type->label()]));
164 $this->logger->notice('Comment type %label has been updated.', ['%label' => $comment_type->label(), 'link' => $edit_link]);
167 $this->commentManager->addBodyField($comment_type->id());
168 $this->messenger()->addStatus(t('Comment type %label has been added.', ['%label' => $comment_type->label()]));
169 $this->logger->notice('Comment type %label has been added.', ['%label' => $comment_type->label(), 'link' => $edit_link]);
172 $form_state->setRedirectUrl($comment_type->urlInfo('collection'));