Chris@0: entityTypeManager = $entity_type_manager; Chris@0: $this->commentStorage = $entity_type_manager->getStorage('comment'); Chris@0: $this->dateFormatter = $date_formatter; Chris@0: $this->moduleHandler = $module_handler; Chris@0: $this->tempStoreFactory = $temp_store_factory; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public static function create(ContainerInterface $container) { Chris@0: return new static( Chris@0: $container->get('entity_type.manager'), Chris@0: $container->get('date.formatter'), Chris@0: $container->get('module_handler'), Chris@14: $container->get('tempstore.private') Chris@0: ); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getFormId() { Chris@0: return 'comment_admin_overview'; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Form constructor for the comment overview administration form. Chris@0: * Chris@0: * @param array $form Chris@0: * An associative array containing the structure of the form. Chris@0: * @param \Drupal\Core\Form\FormStateInterface $form_state Chris@0: * The current state of the form. Chris@0: * @param string $type Chris@0: * The type of the overview form ('approval' or 'new'). Chris@0: * Chris@0: * @return array Chris@0: * The form structure. Chris@0: */ Chris@0: public function buildForm(array $form, FormStateInterface $form_state, $type = 'new') { Chris@0: Chris@0: // Build an 'Update options' form. Chris@0: $form['options'] = [ Chris@0: '#type' => 'details', Chris@0: '#title' => $this->t('Update options'), Chris@0: '#open' => TRUE, Chris@0: '#attributes' => ['class' => ['container-inline']], Chris@0: ]; Chris@0: Chris@0: if ($type == 'approval') { Chris@0: $options['publish'] = $this->t('Publish the selected comments'); Chris@0: } Chris@0: else { Chris@0: $options['unpublish'] = $this->t('Unpublish the selected comments'); Chris@0: } Chris@0: $options['delete'] = $this->t('Delete the selected comments'); Chris@0: Chris@0: $form['options']['operation'] = [ Chris@0: '#type' => 'select', Chris@0: '#title' => $this->t('Action'), Chris@0: '#title_display' => 'invisible', Chris@0: '#options' => $options, Chris@0: '#default_value' => 'publish', Chris@0: ]; Chris@0: $form['options']['submit'] = [ Chris@0: '#type' => 'submit', Chris@0: '#value' => $this->t('Update'), Chris@0: ]; Chris@0: Chris@0: // Load the comments that need to be displayed. Chris@0: $status = ($type == 'approval') ? CommentInterface::NOT_PUBLISHED : CommentInterface::PUBLISHED; Chris@0: $header = [ Chris@0: 'subject' => [ Chris@0: 'data' => $this->t('Subject'), Chris@0: 'specifier' => 'subject', Chris@0: ], Chris@0: 'author' => [ Chris@0: 'data' => $this->t('Author'), Chris@0: 'specifier' => 'name', Chris@0: 'class' => [RESPONSIVE_PRIORITY_MEDIUM], Chris@0: ], Chris@0: 'posted_in' => [ Chris@0: 'data' => $this->t('Posted in'), Chris@0: 'class' => [RESPONSIVE_PRIORITY_LOW], Chris@0: ], Chris@0: 'changed' => [ Chris@0: 'data' => $this->t('Updated'), Chris@0: 'specifier' => 'changed', Chris@0: 'sort' => 'desc', Chris@0: 'class' => [RESPONSIVE_PRIORITY_LOW], Chris@0: ], Chris@0: 'operations' => $this->t('Operations'), Chris@0: ]; Chris@0: $cids = $this->commentStorage->getQuery() Chris@0: ->condition('status', $status) Chris@0: ->tableSort($header) Chris@0: ->pager(50) Chris@0: ->execute(); Chris@0: Chris@0: /** @var $comments \Drupal\comment\CommentInterface[] */ Chris@0: $comments = $this->commentStorage->loadMultiple($cids); Chris@0: Chris@0: // Build a table listing the appropriate comments. Chris@0: $options = []; Chris@0: $destination = $this->getDestinationArray(); Chris@0: Chris@0: $commented_entity_ids = []; Chris@0: $commented_entities = []; Chris@0: Chris@0: foreach ($comments as $comment) { Chris@0: $commented_entity_ids[$comment->getCommentedEntityTypeId()][] = $comment->getCommentedEntityId(); Chris@0: } Chris@0: Chris@0: foreach ($commented_entity_ids as $entity_type => $ids) { Chris@0: $commented_entities[$entity_type] = $this->entityTypeManager Chris@0: ->getStorage($entity_type) Chris@0: ->loadMultiple($ids); Chris@0: } Chris@0: Chris@0: foreach ($comments as $comment) { Chris@0: /** @var $commented_entity \Drupal\Core\Entity\EntityInterface */ Chris@0: $commented_entity = $commented_entities[$comment->getCommentedEntityTypeId()][$comment->getCommentedEntityId()]; Chris@0: $comment_permalink = $comment->permalink(); Chris@0: if ($comment->hasField('comment_body') && ($body = $comment->get('comment_body')->value)) { Chris@0: $attributes = $comment_permalink->getOption('attributes') ?: []; Chris@0: $attributes += ['title' => Unicode::truncate($body, 128)]; Chris@0: $comment_permalink->setOption('attributes', $attributes); Chris@0: } Chris@0: $options[$comment->id()] = [ Chris@0: 'title' => ['data' => ['#title' => $comment->getSubject() ?: $comment->id()]], Chris@0: 'subject' => [ Chris@0: 'data' => [ Chris@0: '#type' => 'link', Chris@0: '#title' => $comment->getSubject(), Chris@0: '#url' => $comment_permalink, Chris@0: ], Chris@0: ], Chris@0: 'author' => [ Chris@0: 'data' => [ Chris@0: '#theme' => 'username', Chris@0: '#account' => $comment->getOwner(), Chris@0: ], Chris@0: ], Chris@0: 'posted_in' => [ Chris@0: 'data' => [ Chris@0: '#type' => 'link', Chris@0: '#title' => $commented_entity->label(), Chris@0: '#access' => $commented_entity->access('view'), Chris@18: '#url' => $commented_entity->toUrl(), Chris@0: ], Chris@0: ], Chris@0: 'changed' => $this->dateFormatter->format($comment->getChangedTimeAcrossTranslations(), 'short'), Chris@0: ]; Chris@18: $comment_uri_options = $comment->toUrl()->getOptions() + ['query' => $destination]; Chris@0: $links = []; Chris@0: $links['edit'] = [ Chris@0: 'title' => $this->t('Edit'), Chris@18: 'url' => $comment->toUrl('edit-form', $comment_uri_options), Chris@0: ]; Chris@0: if ($this->moduleHandler->moduleExists('content_translation') && $this->moduleHandler->invoke('content_translation', 'translate_access', [$comment])->isAllowed()) { Chris@0: $links['translate'] = [ Chris@0: 'title' => $this->t('Translate'), Chris@18: 'url' => $comment->toUrl('drupal:content-translation-overview', $comment_uri_options), Chris@0: ]; Chris@0: } Chris@0: $options[$comment->id()]['operations']['data'] = [ Chris@0: '#type' => 'operations', Chris@0: '#links' => $links, Chris@0: ]; Chris@0: } Chris@0: Chris@0: $form['comments'] = [ Chris@0: '#type' => 'tableselect', Chris@0: '#header' => $header, Chris@0: '#options' => $options, Chris@0: '#empty' => $this->t('No comments available.'), Chris@0: ]; Chris@0: Chris@0: $form['pager'] = ['#type' => 'pager']; Chris@0: Chris@0: return $form; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function validateForm(array &$form, FormStateInterface $form_state) { Chris@0: $form_state->setValue('comments', array_diff($form_state->getValue('comments'), [0])); Chris@0: // We can't execute any 'Update options' if no comments were selected. Chris@0: if (count($form_state->getValue('comments')) == 0) { Chris@0: $form_state->setErrorByName('', $this->t('Select one or more comments to perform the update on.')); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function submitForm(array &$form, FormStateInterface $form_state) { Chris@0: $operation = $form_state->getValue('operation'); Chris@0: $cids = $form_state->getValue('comments'); Chris@0: /** @var \Drupal\comment\CommentInterface[] $comments */ Chris@0: $comments = $this->commentStorage->loadMultiple($cids); Chris@0: if ($operation != 'delete') { Chris@0: foreach ($comments as $comment) { Chris@0: if ($operation == 'unpublish') { Chris@0: $comment->setUnpublished(); Chris@0: } Chris@0: elseif ($operation == 'publish') { Chris@0: $comment->setPublished(); Chris@0: } Chris@0: $comment->save(); Chris@0: } Chris@17: $this->messenger()->addStatus($this->t('The update has been performed.')); Chris@0: $form_state->setRedirect('comment.admin'); Chris@0: } Chris@0: else { Chris@0: $info = []; Chris@0: /** @var \Drupal\comment\CommentInterface $comment */ Chris@0: foreach ($comments as $comment) { Chris@0: $langcode = $comment->language()->getId(); Chris@0: $info[$comment->id()][$langcode] = $langcode; Chris@0: } Chris@0: $this->tempStoreFactory Chris@17: ->get('entity_delete_multiple_confirm') Chris@17: ->set($this->currentUser()->id() . ':comment', $info); Chris@17: $form_state->setRedirect('entity.comment.delete_multiple_form'); Chris@0: } Chris@0: } Chris@0: Chris@0: }