fd7125846c6a7d46287705fb5828cefc510bd3b3
[yaffs-website] / web / core / modules / comment / src / CommentTypeForm.php
1 <?php
2
3 namespace Drupal\comment;
4
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;
12
13 /**
14  * Base form handler for comment type edit forms.
15  *
16  * @internal
17  */
18 class CommentTypeForm extends EntityForm {
19
20   /**
21    * Entity manager service.
22    *
23    * @var \Drupal\Core\Entity\EntityManagerInterface
24    */
25   protected $entityManager;
26
27   /**
28    * A logger instance.
29    *
30    * @var \Psr\Log\LoggerInterface
31    */
32   protected $logger;
33
34   /**
35    * The comment manager.
36    *
37    * @var \Drupal\comment\CommentManagerInterface
38    */
39   protected $commentManager;
40
41   /**
42    * {@inheritdoc}
43    */
44   public static function create(ContainerInterface $container) {
45     return new static(
46       $container->get('entity.manager'),
47       $container->get('logger.factory')->get('comment'),
48       $container->get('comment.manager')
49     );
50   }
51
52   /**
53    * Constructs a CommentTypeFormController
54    *
55    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
56    *   The entity manager service.
57    * @param \Psr\Log\LoggerInterface $logger
58    *   A logger instance.
59    * @param \Drupal\comment\CommentManagerInterface $comment_manager
60    *   The comment manager.
61    */
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;
66   }
67
68   /**
69    * {@inheritdoc}
70    */
71   public function form(array $form, FormStateInterface $form_state) {
72     $form = parent::form($form, $form_state);
73
74     $comment_type = $this->entity;
75
76     $form['label'] = [
77       '#type' => 'textfield',
78       '#title' => t('Label'),
79       '#maxlength' => 255,
80       '#default_value' => $comment_type->label(),
81       '#required' => TRUE,
82     ];
83     $form['id'] = [
84       '#type' => 'machine_name',
85       '#default_value' => $comment_type->id(),
86       '#machine_name' => [
87         'exists' => '\Drupal\comment\Entity\CommentType::load',
88       ],
89       '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
90       '#disabled' => !$comment_type->isNew(),
91     ];
92
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'),
98     ];
99
100     if ($comment_type->isNew()) {
101       $options = [];
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();
107         }
108       }
109       $form['target_entity_type_id'] = [
110         '#type' => 'select',
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.'),
115       ];
116     }
117     else {
118       $form['target_entity_type_id_display'] = [
119         '#type' => 'item',
120         '#markup' => $this->entityManager->getDefinition($comment_type->getTargetEntityTypeId())->getLabel(),
121         '#title' => t('Target entity type'),
122       ];
123     }
124
125     if ($this->moduleHandler->moduleExists('content_translation')) {
126       $form['language'] = [
127         '#type' => 'details',
128         '#title' => t('Language settings'),
129         '#group' => 'additional_settings',
130       ];
131
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(),
138         ],
139         '#default_value' => $language_configuration,
140       ];
141
142       $form['#submit'][] = 'language_configuration_element_submit';
143     }
144
145     $form['actions'] = ['#type' => 'actions'];
146     $form['actions']['submit'] = [
147       '#type' => 'submit',
148       '#value' => t('Save'),
149     ];
150
151     return $form;
152   }
153
154   /**
155    * {@inheritdoc}
156    */
157   public function save(array $form, FormStateInterface $form_state) {
158     $comment_type = $this->entity;
159     $status = $comment_type->save();
160
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]);
165     }
166     else {
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]);
170     }
171
172     $form_state->setRedirectUrl($comment_type->urlInfo('collection'));
173   }
174
175 }