/** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $edit = $form_state->getUserInput(); $form['comments'] = array('#prefix' => '<ul>', '#suffix' => '</ul>', '#tree' => TRUE); // array_filter() returns only elements with actual values. $comment_counter = 0; $this->comments = $this->commentStorage->loadMultiple(array_keys(array_filter($edit['comments']))); foreach ($this->comments as $comment) { $cid = $comment->id(); $form['comments'][$cid] = array('#type' => 'hidden', '#value' => $cid, '#prefix' => '<li>', '#suffix' => String::checkPlain($comment->label()) . '</li>'); $comment_counter++; } $form['operation'] = array('#type' => 'hidden', '#value' => 'delete'); if (!$comment_counter) { drupal_set_message($this->t('There do not appear to be any comments to delete, or your selected comment was deleted by another administrator.')); $form_state->setRedirect('comment.admin'); } return parent::buildForm($form, $form_state); }
/** * Form constructor for the comment overview administration form. * * @param array $form * An associative array containing the structure of the form. * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. * @param string $type * The type of the overview form ('approval' or 'new'). * * @return array * The form structure. */ public function buildForm(array $form, FormStateInterface $form_state, $type = 'new') { // Build an 'Update options' form. $form['options'] = array('#type' => 'details', '#title' => $this->t('Update options'), '#open' => TRUE, '#attributes' => array('class' => array('container-inline'))); if ($type == 'approval') { $options['publish'] = $this->t('Publish the selected comments'); } else { $options['unpublish'] = $this->t('Unpublish the selected comments'); } $options['delete'] = $this->t('Delete the selected comments'); $form['options']['operation'] = array('#type' => 'select', '#title' => $this->t('Action'), '#title_display' => 'invisible', '#options' => $options, '#default_value' => 'publish'); $form['options']['submit'] = array('#type' => 'submit', '#value' => $this->t('Update')); // Load the comments that need to be displayed. $status = $type == 'approval' ? CommentInterface::NOT_PUBLISHED : CommentInterface::PUBLISHED; $header = array('subject' => array('data' => $this->t('Subject'), 'specifier' => 'subject'), 'author' => array('data' => $this->t('Author'), 'specifier' => 'name', 'class' => array(RESPONSIVE_PRIORITY_MEDIUM)), 'posted_in' => array('data' => $this->t('Posted in'), 'class' => array(RESPONSIVE_PRIORITY_LOW)), 'changed' => array('data' => $this->t('Updated'), 'specifier' => 'changed', 'sort' => 'desc', 'class' => array(RESPONSIVE_PRIORITY_LOW)), 'operations' => $this->t('Operations')); $cids = $this->commentStorage->getQuery()->condition('status', $status)->tableSort($header)->pager(50)->execute(); /** @var $comments \Drupal\comment\CommentInterface[] */ $comments = $this->commentStorage->loadMultiple($cids); // Build a table listing the appropriate comments. $options = array(); $destination = $this->getDestinationArray(); $commented_entity_ids = array(); $commented_entities = array(); foreach ($comments as $comment) { $commented_entity_ids[$comment->getCommentedEntityTypeId()][] = $comment->getCommentedEntityId(); } foreach ($commented_entity_ids as $entity_type => $ids) { $commented_entities[$entity_type] = $this->entityManager->getStorage($entity_type)->loadMultiple($ids); } foreach ($comments as $comment) { /** @var $commented_entity \Drupal\Core\Entity\EntityInterface */ $commented_entity = $commented_entities[$comment->getCommentedEntityTypeId()][$comment->getCommentedEntityId()]; $username = array('#theme' => 'username', '#account' => $comment->getOwner()); $body = ''; if (!empty($comment->comment_body->value)) { $body = $comment->comment_body->value; } $comment_permalink = $comment->permalink(); $attributes = $comment_permalink->getOption('attributes') ?: array(); $attributes += array('title' => Unicode::truncate($body, 128)); $comment_permalink->setOption('attributes', $attributes); $options[$comment->id()] = array('title' => array('data' => array('#title' => $comment->getSubject() ?: $comment->id())), 'subject' => array('data' => array('#type' => 'link', '#title' => $comment->getSubject(), '#url' => $comment_permalink)), 'author' => drupal_render($username), 'posted_in' => array('data' => array('#type' => 'link', '#title' => $commented_entity->label(), '#access' => $commented_entity->access('view'), '#url' => $commented_entity->urlInfo())), 'changed' => $this->dateFormatter->format($comment->getChangedTime(), 'short')); $comment_uri_options = $comment->urlInfo()->getOptions(); $links = array(); $links['edit'] = array('title' => $this->t('Edit'), 'url' => Url::fromRoute('entity.comment.edit_form', ['comment' => $comment->id()], $comment_uri_options + ['query' => $destination])); if ($this->moduleHandler->moduleExists('content_translation') && $this->moduleHandler->invoke('content_translation', 'translate_access', array($comment))->isAllowed()) { $links['translate'] = array('title' => $this->t('Translate'), 'url' => Url::fromRoute('entity.comment.content_translation_overview', ['comment' => $comment->id()], $comment_uri_options + ['query' => $destination])); } $options[$comment->id()]['operations']['data'] = array('#type' => 'operations', '#links' => $links); } $form['comments'] = array('#type' => 'tableselect', '#header' => $header, '#options' => $options, '#empty' => $this->t('No comments available.')); $form['pager'] = array('#type' => 'pager'); return $form; }