3 namespace Drupal\comment\Form;
5 use Drupal\comment\CommentInterface;
6 use Drupal\comment\CommentStorageInterface;
7 use Drupal\Component\Utility\Unicode;
8 use Drupal\Core\Datetime\DateFormatterInterface;
9 use Drupal\Core\Entity\EntityManagerInterface;
10 use Drupal\Core\Extension\ModuleHandlerInterface;
11 use Drupal\Core\Form\FormBase;
12 use Drupal\Core\Form\FormStateInterface;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
16 * Provides the comments overview administration form.
18 class CommentAdminOverview extends FormBase {
23 * @var \Drupal\Core\Entity\EntityManagerInterface
25 protected $entityManager;
28 * The comment storage.
30 * @var \Drupal\comment\CommentStorageInterface
32 protected $commentStorage;
35 * The date formatter service.
37 * @var \Drupal\Core\Datetime\DateFormatterInterface
39 protected $dateFormatter;
44 * @var \Drupal\Core\Extension\ModuleHandlerInterface
46 protected $moduleHandler;
49 * Creates a CommentAdminOverview form.
51 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
52 * The entity manager service.
53 * @param \Drupal\comment\CommentStorageInterface $comment_storage
54 * The comment storage.
55 * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
56 * The date formatter service.
57 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
60 public function __construct(EntityManagerInterface $entity_manager, CommentStorageInterface $comment_storage, DateFormatterInterface $date_formatter, ModuleHandlerInterface $module_handler) {
61 $this->entityManager = $entity_manager;
62 $this->commentStorage = $comment_storage;
63 $this->dateFormatter = $date_formatter;
64 $this->moduleHandler = $module_handler;
70 public static function create(ContainerInterface $container) {
72 $container->get('entity.manager'),
73 $container->get('entity.manager')->getStorage('comment'),
74 $container->get('date.formatter'),
75 $container->get('module_handler')
82 public function getFormId() {
83 return 'comment_admin_overview';
87 * Form constructor for the comment overview administration form.
90 * An associative array containing the structure of the form.
91 * @param \Drupal\Core\Form\FormStateInterface $form_state
92 * The current state of the form.
94 * The type of the overview form ('approval' or 'new').
99 public function buildForm(array $form, FormStateInterface $form_state, $type = 'new') {
101 // Build an 'Update options' form.
103 '#type' => 'details',
104 '#title' => $this->t('Update options'),
106 '#attributes' => ['class' => ['container-inline']],
109 if ($type == 'approval') {
110 $options['publish'] = $this->t('Publish the selected comments');
113 $options['unpublish'] = $this->t('Unpublish the selected comments');
115 $options['delete'] = $this->t('Delete the selected comments');
117 $form['options']['operation'] = [
119 '#title' => $this->t('Action'),
120 '#title_display' => 'invisible',
121 '#options' => $options,
122 '#default_value' => 'publish',
124 $form['options']['submit'] = [
126 '#value' => $this->t('Update'),
129 // Load the comments that need to be displayed.
130 $status = ($type == 'approval') ? CommentInterface::NOT_PUBLISHED : CommentInterface::PUBLISHED;
133 'data' => $this->t('Subject'),
134 'specifier' => 'subject',
137 'data' => $this->t('Author'),
138 'specifier' => 'name',
139 'class' => [RESPONSIVE_PRIORITY_MEDIUM],
142 'data' => $this->t('Posted in'),
143 'class' => [RESPONSIVE_PRIORITY_LOW],
146 'data' => $this->t('Updated'),
147 'specifier' => 'changed',
149 'class' => [RESPONSIVE_PRIORITY_LOW],
151 'operations' => $this->t('Operations'),
153 $cids = $this->commentStorage->getQuery()
154 ->condition('status', $status)
159 /** @var $comments \Drupal\comment\CommentInterface[] */
160 $comments = $this->commentStorage->loadMultiple($cids);
162 // Build a table listing the appropriate comments.
164 $destination = $this->getDestinationArray();
166 $commented_entity_ids = [];
167 $commented_entities = [];
169 foreach ($comments as $comment) {
170 $commented_entity_ids[$comment->getCommentedEntityTypeId()][] = $comment->getCommentedEntityId();
173 foreach ($commented_entity_ids as $entity_type => $ids) {
174 $commented_entities[$entity_type] = $this->entityManager->getStorage($entity_type)->loadMultiple($ids);
177 foreach ($comments as $comment) {
178 /** @var $commented_entity \Drupal\Core\Entity\EntityInterface */
179 $commented_entity = $commented_entities[$comment->getCommentedEntityTypeId()][$comment->getCommentedEntityId()];
180 $comment_permalink = $comment->permalink();
181 if ($comment->hasField('comment_body') && ($body = $comment->get('comment_body')->value)) {
182 $attributes = $comment_permalink->getOption('attributes') ?: [];
183 $attributes += ['title' => Unicode::truncate($body, 128)];
184 $comment_permalink->setOption('attributes', $attributes);
186 $options[$comment->id()] = [
187 'title' => ['data' => ['#title' => $comment->getSubject() ?: $comment->id()]],
191 '#title' => $comment->getSubject(),
192 '#url' => $comment_permalink,
197 '#theme' => 'username',
198 '#account' => $comment->getOwner(),
204 '#title' => $commented_entity->label(),
205 '#access' => $commented_entity->access('view'),
206 '#url' => $commented_entity->urlInfo(),
209 'changed' => $this->dateFormatter->format($comment->getChangedTimeAcrossTranslations(), 'short'),
211 $comment_uri_options = $comment->urlInfo()->getOptions() + ['query' => $destination];
214 'title' => $this->t('Edit'),
215 'url' => $comment->urlInfo('edit-form', $comment_uri_options),
217 if ($this->moduleHandler->moduleExists('content_translation') && $this->moduleHandler->invoke('content_translation', 'translate_access', [$comment])->isAllowed()) {
218 $links['translate'] = [
219 'title' => $this->t('Translate'),
220 'url' => $comment->urlInfo('drupal:content-translation-overview', $comment_uri_options),
223 $options[$comment->id()]['operations']['data'] = [
224 '#type' => 'operations',
229 $form['comments'] = [
230 '#type' => 'tableselect',
231 '#header' => $header,
232 '#options' => $options,
233 '#empty' => $this->t('No comments available.'),
236 $form['pager'] = ['#type' => 'pager'];
244 public function validateForm(array &$form, FormStateInterface $form_state) {
245 $form_state->setValue('comments', array_diff($form_state->getValue('comments'), [0]));
246 // We can't execute any 'Update options' if no comments were selected.
247 if (count($form_state->getValue('comments')) == 0) {
248 $form_state->setErrorByName('', $this->t('Select one or more comments to perform the update on.'));
255 public function submitForm(array &$form, FormStateInterface $form_state) {
256 $operation = $form_state->getValue('operation');
257 $cids = $form_state->getValue('comments');
259 foreach ($cids as $cid) {
260 // Delete operation handled in \Drupal\comment\Form\ConfirmDeleteMultiple
261 // see \Drupal\comment\Controller\AdminController::adminPage().
262 if ($operation == 'unpublish') {
263 $comment = $this->commentStorage->load($cid);
264 $comment->setPublished(FALSE);
267 elseif ($operation == 'publish') {
268 $comment = $this->commentStorage->load($cid);
269 $comment->setPublished(TRUE);
273 drupal_set_message($this->t('The update has been performed.'));
274 $form_state->setRedirect('comment.admin');