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.
16 class CommentTypeForm extends EntityForm {
19 * Entity manager service.
21 * @var \Drupal\Core\Entity\EntityManagerInterface
23 protected $entityManager;
28 * @var \Psr\Log\LoggerInterface
33 * The comment manager.
35 * @var \Drupal\comment\CommentManagerInterface
37 protected $commentManager;
42 public static function create(ContainerInterface $container) {
44 $container->get('entity.manager'),
45 $container->get('logger.factory')->get('comment'),
46 $container->get('comment.manager')
51 * Constructs a CommentTypeFormController
53 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
54 * The entity manager service.
55 * @param \Psr\Log\LoggerInterface $logger
57 * @param \Drupal\comment\CommentManagerInterface $comment_manager
58 * The comment manager.
60 public function __construct(EntityManagerInterface $entity_manager, LoggerInterface $logger, CommentManagerInterface $comment_manager) {
61 $this->entityManager = $entity_manager;
62 $this->logger = $logger;
63 $this->commentManager = $comment_manager;
69 public function form(array $form, FormStateInterface $form_state) {
70 $form = parent::form($form, $form_state);
72 $comment_type = $this->entity;
75 '#type' => 'textfield',
76 '#title' => t('Label'),
78 '#default_value' => $comment_type->label(),
82 '#type' => 'machine_name',
83 '#default_value' => $comment_type->id(),
85 'exists' => '\Drupal\comment\Entity\CommentType::load',
87 '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
88 '#disabled' => !$comment_type->isNew(),
91 $form['description'] = [
92 '#type' => 'textarea',
93 '#default_value' => $comment_type->getDescription(),
94 '#description' => t('Describe this comment type. The text will be displayed on the <em>Comment types</em> administration overview page.'),
95 '#title' => t('Description'),
98 if ($comment_type->isNew()) {
100 foreach ($this->entityManager->getDefinitions() as $entity_type) {
101 // Only expose entities that have field UI enabled, only those can
102 // get comment fields added in the UI.
103 if ($entity_type->get('field_ui_base_route')) {
104 $options[$entity_type->id()] = $entity_type->getLabel();
107 $form['target_entity_type_id'] = [
109 '#default_value' => $comment_type->getTargetEntityTypeId(),
110 '#title' => t('Target entity type'),
111 '#options' => $options,
112 '#description' => t('The target entity type can not be changed after the comment type has been created.')
116 $form['target_entity_type_id_display'] = [
118 '#markup' => $this->entityManager->getDefinition($comment_type->getTargetEntityTypeId())->getLabel(),
119 '#title' => t('Target entity type'),
123 if ($this->moduleHandler->moduleExists('content_translation')) {
124 $form['language'] = [
125 '#type' => 'details',
126 '#title' => t('Language settings'),
127 '#group' => 'additional_settings',
130 $language_configuration = ContentLanguageSettings::loadByEntityTypeBundle('comment', $comment_type->id());
131 $form['language']['language_configuration'] = [
132 '#type' => 'language_configuration',
133 '#entity_information' => [
134 'entity_type' => 'comment',
135 'bundle' => $comment_type->id(),
137 '#default_value' => $language_configuration,
140 $form['#submit'][] = 'language_configuration_element_submit';
143 $form['actions'] = ['#type' => 'actions'];
144 $form['actions']['submit'] = [
146 '#value' => t('Save'),
155 public function save(array $form, FormStateInterface $form_state) {
156 $comment_type = $this->entity;
157 $status = $comment_type->save();
159 $edit_link = $this->entity->link($this->t('Edit'));
160 if ($status == SAVED_UPDATED) {
161 drupal_set_message(t('Comment type %label has been updated.', ['%label' => $comment_type->label()]));
162 $this->logger->notice('Comment type %label has been updated.', ['%label' => $comment_type->label(), 'link' => $edit_link]);
165 $this->commentManager->addBodyField($comment_type->id());
166 drupal_set_message(t('Comment type %label has been added.', ['%label' => $comment_type->label()]));
167 $this->logger->notice('Comment type %label has been added.', ['%label' => $comment_type->label(), 'link' => $edit_link]);
170 $form_state->setRedirectUrl($comment_type->urlInfo('collection'));