コード例 #1
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.
         $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;
 }
コード例 #2
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.
         $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;
 }
コード例 #3
0
 /**
  * {@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;
 }
コード例 #4
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;
 }