/**
  * Form constructor for the comment overview administration form.
  *
  * @param array $form
  *   An associative array containing the structure of the form.
  * @param array $form_state
  *   An associative array containing 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, array &$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 = drupal_get_destination();
     $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_prepare_author($comment));
         $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()) + $comment_permalink->toRenderArray()), 'author' => drupal_render($username), 'posted_in' => array('data' => array('#type' => 'link', '#title' => $commented_entity->label(), '#access' => $commented_entity->access('view')) + $commented_entity->urlInfo()->toRenderArray()), 'changed' => $this->date->format($comment->getChangedTime(), 'short'));
         $comment_uri_options = $comment->urlInfo()->getOptions();
         $links = array();
         $links['edit'] = array('title' => $this->t('Edit'), 'route_name' => 'comment.edit_page', 'route_parameters' => array('comment' => $comment->id()), 'options' => $comment_uri_options, 'query' => $destination);
         if ($this->moduleHandler->invoke('content_translation', 'translate_access', array($comment))) {
             $links['translate'] = array('title' => $this->t('Translate'), 'route_name' => 'content_translation.translation_overview_comment', 'route_parameters' => array('comment' => $comment->id()), 'options' => $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('#theme' => 'pager');
     return $form;
 }
 /**
  * Overrides Drupal\Core\Entity\EntityViewBuilder::buildComponents().
  *
  * In addition to modifying the content key on entities, this implementation
  * will also set the comment entity key which all comments carry.
  *
  * @throws \InvalidArgumentException
  *   Thrown when a comment is attached to an entity that no longer exists.
  */
 public function buildComponents(array &$build, array $entities, array $displays, $view_mode, $langcode = NULL)
 {
     /** @var \Drupal\comment\CommentInterface[] $entities */
     if (empty($entities)) {
         return;
     }
     // Pre-load associated users into cache to leverage multiple loading.
     $uids = array();
     foreach ($entities as $entity) {
         $uids[] = $entity->getOwnerId();
     }
     $this->entityManager->getStorage('user')->loadMultiple(array_unique($uids));
     parent::buildComponents($build, $entities, $displays, $view_mode, $langcode);
     // Load all the entities that have comments attached.
     $commented_entity_ids = array();
     $commented_entities = array();
     foreach ($entities as $entity) {
         $commented_entity_ids[$entity->getCommentedEntityTypeId()][] = $entity->getCommentedEntityId();
     }
     // Load entities in bulk. This is more performant than using
     // $comment->getCommentedEntity() as we can load them in bulk per type.
     foreach ($commented_entity_ids as $entity_type => $entity_ids) {
         $commented_entities[$entity_type] = $this->entityManager->getStorage($entity_type)->loadMultiple($entity_ids);
     }
     foreach ($entities as $id => $entity) {
         if (isset($commented_entities[$entity->getCommentedEntityTypeId()][$entity->getCommentedEntityId()])) {
             $commented_entity = $commented_entities[$entity->getCommentedEntityTypeId()][$entity->getCommentedEntityId()];
         } else {
             throw new \InvalidArgumentException(t('Invalid entity for comment.'));
         }
         $build[$id]['#entity'] = $entity;
         $build[$id]['#theme'] = 'comment__' . $entity->getFieldName() . '__' . $commented_entity->bundle();
         $display = $displays[$entity->bundle()];
         if ($display->getComponent('links')) {
             $callback = get_called_class() . '::renderLinks';
             $context = array('comment_entity_id' => $entity->id(), 'view_mode' => $view_mode, 'langcode' => $langcode, 'commented_entity_type' => $commented_entity->getEntityTypeId(), 'commented_entity_id' => $commented_entity->id(), 'in_preview' => !empty($entity->in_preview));
             $placeholder = drupal_render_cache_generate_placeholder($callback, $context);
             $build[$id]['links'] = array('#post_render_cache' => array($callback => array($context)), '#markup' => $placeholder);
         }
         $account = comment_prepare_author($entity);
         if (\Drupal::config('user.settings')->get('signatures') && $account->getSignature()) {
             $build[$id]['signature'] = array('#type' => 'processed_text', '#text' => $account->getSignature(), '#format' => $account->getSignatureFormat(), '#langcode' => $entity->language()->getId());
         }
         if (!isset($build[$id]['#attached'])) {
             $build[$id]['#attached'] = array();
         }
         $build[$id]['#attached']['library'][] = 'comment/drupal.comment-by-viewer';
         if ($this->moduleHandler->moduleExists('history') && \Drupal::currentUser()->isAuthenticated()) {
             $build[$id]['#attached']['library'][] = 'comment/drupal.comment-new-indicator';
             // Embed the metadata for the comment "new" indicators on this node.
             $build[$id]['#post_render_cache']['history_attach_timestamp'] = array(array('node_id' => $commented_entity->id()));
         }
     }
 }