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