/**
  * {@inheritdoc}
  */
 public function viewElements(FieldItemListInterface $items)
 {
     $elements = array();
     $output = array();
     $field_name = $this->fieldDefinition->getName();
     $entity = $items->getEntity();
     $status = $items->status;
     if ($status != CommentItemInterface::HIDDEN && empty($entity->in_preview) && !in_array($this->viewMode, array('search_result', 'search_index'))) {
         $comment_settings = $this->getFieldSettings();
         // Only attempt to render comments if the entity has visible comments.
         // Unpublished comments are not included in
         // $entity->get($field_name)->comment_count, but unpublished comments
         // should display if the user is an administrator.
         $elements['#cache']['contexts'][] = 'user.roles';
         if ($this->currentUser->hasPermission('access comments') || $this->currentUser->hasPermission('administer comments')) {
             // This is a listing of Comment entities, so associate its list cache
             // tag for correct invalidation.
             $output['comments']['#cache']['tags'] = $this->entityManager->getDefinition('comment')->getListCacheTags();
             if ($entity->get($field_name)->comment_count || $this->currentUser->hasPermission('administer comments')) {
                 $mode = $comment_settings['default_mode'];
                 $comments_per_page = $comment_settings['per_page'];
                 $comments = $this->storage->loadThread($entity, $field_name, $mode, $comments_per_page, $this->getSetting('pager_id'));
                 if ($comments) {
                     comment_prepare_thread($comments);
                     $build = $this->viewBuilder->viewMultiple($comments);
                     $build['pager']['#type'] = 'pager';
                     if ($this->getSetting('pager_id')) {
                         $build['pager']['#element'] = $this->getSetting('pager_id');
                     }
                     $output['comments'] += $build;
                 }
             }
         }
         // Append comment form if the comments are open and the form is set to
         // display below the entity. Do not show the form for the print view mode.
         if ($status == CommentItemInterface::OPEN && $comment_settings['form_location'] == CommentItemInterface::FORM_BELOW && $this->viewMode != 'print') {
             // Only show the add comment form if the user has permission.
             $elements['#cache']['contexts'][] = 'user.roles';
             if ($this->currentUser->hasPermission('post comments')) {
                 // All users in the "anonymous" role can use the same form: it is fine
                 // for this form to be stored in the render cache.
                 if ($this->currentUser->isAnonymous()) {
                     $comment = $this->storage->create(array('entity_type' => $entity->getEntityTypeId(), 'entity_id' => $entity->id(), 'field_name' => $field_name, 'comment_type' => $this->getFieldSetting('comment_type'), 'pid' => NULL));
                     $output['comment_form'] = $this->entityFormBuilder->getForm($comment);
                 } else {
                     $callback = 'comment.post_render_cache:renderForm';
                     $context = array('entity_type' => $entity->getEntityTypeId(), 'entity_id' => $entity->id(), 'field_name' => $field_name);
                     $placeholder = drupal_render_cache_generate_placeholder($callback, $context);
                     $output['comment_form'] = array('#post_render_cache' => array($callback => array($context)), '#markup' => $placeholder);
                 }
             }
         }
         $elements[] = $output + array('#comment_type' => $this->getFieldSetting('comment_type'), '#comment_display_mode' => $this->getFieldSetting('default_mode'), 'comments' => array(), 'comment_form' => array());
     }
     return $elements;
 }
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     if ($form_state->getValue('confirm')) {
         $this->commentStorage->delete($this->comments);
         $count = count($form_state->getValue('comments'));
         $this->logger('content')->notice('Deleted @count comments.', array('@count' => $count));
         drupal_set_message($this->formatPlural($count, 'Deleted 1 comment.', 'Deleted @count comments.'));
     }
     $form_state->setRedirectUrl($this->getCancelUrl());
 }
示例#3
0
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, array &$form_state)
 {
     if ($form_state['values']['confirm']) {
         $this->commentStorage->delete($this->comments);
         $count = count($form_state['values']['comments']);
         watchdog('content', 'Deleted @count comments.', array('@count' => $count));
         drupal_set_message(format_plural($count, 'Deleted 1 comment.', 'Deleted @count comments.'));
     }
     $form_state['redirect_route'] = $this->getCancelUrl();
 }
 /**
  * {@inheritdoc}
  */
 public function viewElements(FieldItemListInterface $items)
 {
     $elements = array();
     $output = array();
     $field_name = $this->fieldDefinition->getName();
     $entity = $items->getEntity();
     $status = $items->status;
     if ($status != CommentItemInterface::HIDDEN && empty($entity->in_preview) && !in_array($this->viewMode, array('search_result', 'search_index'))) {
         $comment_settings = $this->getFieldSettings();
         // Only attempt to render comments if the entity has visible comments.
         // Unpublished comments are not included in
         // $entity->get($field_name)->comment_count, but unpublished comments
         // should display if the user is an administrator.
         $elements['#cache']['contexts'][] = 'user.permissions';
         if ($this->currentUser->hasPermission('access comments') || $this->currentUser->hasPermission('administer comments')) {
             $output['comments'] = [];
             if ($entity->get($field_name)->comment_count || $this->currentUser->hasPermission('administer comments')) {
                 $mode = $comment_settings['default_mode'];
                 $comments_per_page = $comment_settings['per_page'];
                 $comments = $this->storage->loadThread($entity, $field_name, $mode, $comments_per_page, $this->getSetting('pager_id'));
                 if ($comments) {
                     $build = $this->viewBuilder->viewMultiple($comments);
                     $build['pager']['#type'] = 'pager';
                     if ($this->getSetting('pager_id')) {
                         $build['pager']['#element'] = $this->getSetting('pager_id');
                     }
                     $output['comments'] += $build;
                 }
             }
         }
         // Append comment form if the comments are open and the form is set to
         // display below the entity. Do not show the form for the print view mode.
         if ($status == CommentItemInterface::OPEN && $comment_settings['form_location'] == CommentItemInterface::FORM_BELOW && $this->viewMode != 'print') {
             // Only show the add comment form if the user has permission.
             $elements['#cache']['contexts'][] = 'user.roles';
             if ($this->currentUser->hasPermission('post comments')) {
                 $output['comment_form'] = ['#lazy_builder' => ['comment.lazy_builders:renderForm', [$entity->getEntityTypeId(), $entity->id(), $field_name, $this->getFieldSetting('comment_type')]]];
                 // @todo Remove this in https://www.drupal.org/node/2543334. Until
                 //   then, \Drupal\Core\Render\Renderer::hasPoorCacheability() isn't
                 //   integrated with cache context bubbling, so this duplicates the
                 //   contexts added by \Drupal\comment\CommentForm::form().
                 $output['comment_form']['#cache']['contexts'][] = 'user.permissions';
                 $output['comment_form']['#cache']['contexts'][] = 'user.roles:authenticated';
                 if ($this->currentUser->isAuthenticated()) {
                     $output['comment_form']['#cache']['contexts'][] = 'user';
                 }
             }
         }
         $elements[] = $output + array('#comment_type' => $this->getFieldSetting('comment_type'), '#comment_display_mode' => $this->getFieldSetting('default_mode'), 'comments' => array(), 'comment_form' => array());
     }
     return $elements;
 }
 /**
  * {@inheritdoc}
  */
 public function viewElements(FieldItemListInterface $items, $langcode)
 {
     $elements = array();
     $output = array();
     $field_name = $this->fieldDefinition->getName();
     $entity = $items->getEntity();
     $status = $items->status;
     if ($status != CommentItemInterface::HIDDEN && empty($entity->in_preview) && !in_array($this->viewMode, array('search_result', 'search_index'))) {
         $comment_settings = $this->getFieldSettings();
         // Only attempt to render comments if the entity has visible comments.
         // Unpublished comments are not included in
         // $entity->get($field_name)->comment_count, but unpublished comments
         // should display if the user is an administrator.
         $elements['#cache']['contexts'][] = 'user.permissions';
         if ($this->currentUser->hasPermission('access comments') || $this->currentUser->hasPermission('administer comments')) {
             $output['comments'] = [];
             if ($entity->get($field_name)->comment_count || $this->currentUser->hasPermission('administer comments')) {
                 $mode = $comment_settings['default_mode'];
                 $comments_per_page = $comment_settings['per_page'];
                 $comments = $this->storage->loadThread($entity, $field_name, $mode, $comments_per_page, $this->getSetting('pager_id'));
                 if ($comments) {
                     $build = $this->viewBuilder->viewMultiple($comments);
                     $build['pager']['#type'] = 'pager';
                     // CommentController::commentPermalink() calculates the page number
                     // where a specific comment appears and does a subrequest pointing to
                     // that page, we need to pass that subrequest route to our pager to
                     // keep the pager working.
                     $build['pager']['#route_name'] = $this->routeMatch->getRouteObject();
                     $build['pager']['#route_parameters'] = $this->routeMatch->getRawParameters()->all();
                     if ($this->getSetting('pager_id')) {
                         $build['pager']['#element'] = $this->getSetting('pager_id');
                     }
                     $output['comments'] += $build;
                 }
             }
         }
         // Append comment form if the comments are open and the form is set to
         // display below the entity. Do not show the form for the print view mode.
         if ($status == CommentItemInterface::OPEN && $comment_settings['form_location'] == CommentItemInterface::FORM_BELOW && $this->viewMode != 'print') {
             // Only show the add comment form if the user has permission.
             $elements['#cache']['contexts'][] = 'user.roles';
             if ($this->currentUser->hasPermission('post comments')) {
                 $output['comment_form'] = ['#lazy_builder' => ['comment.lazy_builders:renderForm', [$entity->getEntityTypeId(), $entity->id(), $field_name, $this->getFieldSetting('comment_type')]], '#create_placeholder' => TRUE];
             }
         }
         $elements[] = $output + array('#comment_type' => $this->getFieldSetting('comment_type'), '#comment_display_mode' => $this->getFieldSetting('default_mode'), 'comments' => array(), 'comment_form' => array());
     }
     return $elements;
 }
示例#6
0
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     $operation = $form_state->getValue('operation');
     $cids = $form_state->getValue('comments');
     foreach ($cids as $cid) {
         // Delete operation handled in \Drupal\comment\Form\ConfirmDeleteMultiple
         // see \Drupal\comment\Controller\AdminController::adminPage().
         if ($operation == 'unpublish') {
             $comment = $this->commentStorage->load($cid);
             $comment->setPublished(FALSE);
             $comment->save();
         } elseif ($operation == 'publish') {
             $comment = $this->commentStorage->load($cid);
             $comment->setPublished(TRUE);
             $comment->save();
         }
     }
     drupal_set_message($this->t('The update has been performed.'));
     $form_state->setRedirect('comment.admin');
 }
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, array &$form_state)
 {
     $operation = $form_state['values']['operation'];
     $cids = $form_state['values']['comments'];
     foreach ($cids as $cid) {
         // Delete operation handled in \Drupal\comment\Form\ConfirmDeleteMultiple
         // see \Drupal\comment\Controller\AdminController::adminPage().
         if ($operation == 'unpublish') {
             $comment = $this->commentStorage->load($cid);
             $comment->setPublished(FALSE);
             $comment->save();
         } elseif ($operation == 'publish') {
             $comment = $this->commentStorage->load($cid);
             $comment->setPublished(TRUE);
             $comment->save();
         }
     }
     drupal_set_message($this->t('The update has been performed.'));
     $form_state['redirect_route'] = array('route_name' => 'comment.admin');
 }
 /**
  * {@inheritdoc}
  */
 public function getTitle()
 {
     return t('Unapproved comments (@count)', array('@count' => $this->commentStorage->getUnapprovedCount()));
 }
示例#9
0
 /**
  * {@inheritdoc}
  */
 public function viewElements(FieldItemListInterface $items)
 {
     $elements = array();
     $output = array();
     $field_name = $this->fieldDefinition->getName();
     $entity = $items->getEntity();
     $status = $items->status;
     if ($status != CommentItemInterface::HIDDEN && empty($entity->in_preview) && !in_array($this->viewMode, array('search_result', 'search_index'))) {
         $comment_settings = $this->getFieldSettings();
         // Only attempt to render comments if the entity has visible comments.
         // Unpublished comments are not included in
         // $entity->get($field_name)->comment_count, but unpublished comments
         // should display if the user is an administrator.
         if ($entity->get($field_name)->comment_count && $this->currentUser->hasPermission('access comments') || $this->currentUser->hasPermission('administer comments')) {
             $mode = $comment_settings['default_mode'];
             $comments_per_page = $comment_settings['per_page'];
             $comments = $this->storage->loadThread($entity, $field_name, $mode, $comments_per_page, $this->getSetting('pager_id'));
             if ($comments) {
                 comment_prepare_thread($comments);
                 $build = $this->viewBuilder->viewMultiple($comments);
                 $build['pager']['#theme'] = 'pager';
                 if ($this->getSetting('pager_id')) {
                     $build['pager']['#element'] = $this->getSetting('pager_id');
                 }
                 // The viewElements() method of entity field formatters is run
                 // during the #pre_render phase of rendering an entity. A formatter
                 // builds the content of the field in preparation for theming.
                 // All entity cache tags must be available after the #pre_render phase.
                 // This field formatter is highly exceptional: it renders *another*
                 // entity and this referenced entity has its own #pre_render
                 // callbacks. In order collect the cache tags associated with the
                 // referenced entity it must be passed to drupal_render() so that its
                 // #pre_render callbacks are invoked and its full build array is
                 // assembled. Rendering the referenced entity in place here will allow
                 // its cache tags to be bubbled up and included with those of the
                 // main entity when cache tags are collected for a renderable array
                 // in drupal_render().
                 drupal_render($build, TRUE);
                 $output['comments'] = $build;
             }
         }
         // Append comment form if the comments are open and the form is set to
         // display below the entity. Do not show the form for the print view mode.
         if ($status == CommentItemInterface::OPEN && $comment_settings['form_location'] == COMMENT_FORM_BELOW && $this->viewMode != 'print') {
             // Only show the add comment form if the user has permission.
             if ($this->currentUser->hasPermission('post comments')) {
                 // All users in the "anonymous" role can use the same form: it is fine
                 // for this form to be stored in the render cache.
                 if ($this->currentUser->isAnonymous()) {
                     $comment = $this->storage->create(array('entity_type' => $entity->getEntityTypeId(), 'entity_id' => $entity->id(), 'field_name' => $field_name, 'comment_type' => $this->getFieldSetting('comment_type'), 'pid' => NULL));
                     $output['comment_form'] = $this->entityFormBuilder->getForm($comment);
                 } else {
                     $callback = 'comment.post_render_cache:renderForm';
                     $context = array('entity_type' => $entity->getEntityTypeId(), 'entity_id' => $entity->id(), 'field_name' => $field_name);
                     $placeholder = drupal_render_cache_generate_placeholder($callback, $context);
                     $output['comment_form'] = array('#post_render_cache' => array($callback => array($context)), '#markup' => $placeholder);
                 }
             }
         }
         $elements[] = $output + array('#comment_type' => $this->getFieldSetting('comment_type'), '#comment_display_mode' => $this->getFieldSetting('default_mode'), 'comments' => array(), 'comment_form' => array());
     }
     return $elements;
 }