public CommentViewBuilder::buildComponents(array &$build, array $entities, array $displays, $view_mode)
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.
Overrides EntityViewBuilder::buildComponents
File
- core/modules/comment/src/CommentViewBuilder.php, line 84
Class
- CommentViewBuilder
- View builder handler for comments.
Namespace
Drupal\comment
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | public function buildComponents( array & $build , array $entities , array $displays , $view_mode ) { /** @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 ); // A counter to track the indentation level. $current_indent = 0; foreach ( $entities as $id => $entity ) { if ( $build [ $id ][ '#comment_threaded' ]) { $comment_indent = count ( explode ( '.' , $entity ->getThread())) - 1; if ( $comment_indent > $current_indent ) { // Set 1 to indent this comment from the previous one (its parent). // Set only one extra level of indenting even if the difference in // depth is higher. $build [ $id ][ '#comment_indent' ] = 1; $current_indent ++; } else { // Set zero if this comment is on the same level as the previous one // or negative value to point an amount indents to close. $build [ $id ][ '#comment_indent' ] = $comment_indent - $current_indent ; $current_indent = $comment_indent ; } } // Commented entities already loaded after self::getBuildDefaults(). $commented_entity = $entity ->getCommentedEntity(); $build [ $id ][ '#entity' ] = $entity ; $build [ $id ][ '#theme' ] = 'comment__' . $entity ->getFieldName() . '__' . $commented_entity ->bundle(); $display = $displays [ $entity ->bundle()]; if ( $display ->getComponent( 'links' )) { $build [ $id ][ 'links' ] = array ( '#lazy_builder' => [ 'comment.lazy_builders:renderLinks' , [ $entity ->id(), $view_mode , $entity ->language()->getId(), ! empty ( $entity ->in_preview), ]], '#create_placeholder' => TRUE, ); } if (!isset( $build [ $id ][ '#attached' ])) { $build [ $id ][ '#attached' ] = array (); } $build [ $id ][ '#attached' ][ 'library' ][] = 'comment/drupal.comment-by-viewer' ; if ( $this ->moduleHandler->moduleExists( 'history' ) && $this ->currentUser->isAuthenticated()) { $build [ $id ][ '#attached' ][ 'library' ][] = 'comment/drupal.comment-new-indicator' ; // Embed the metadata for the comment "new" indicators on this node. $build [ $id ][ 'history' ] = [ '#lazy_builder' => [ 'history_attach_timestamp' , [ $commented_entity ->id()]], '#create_placeholder' => TRUE, ]; } } if ( $build [ $id ][ '#comment_threaded' ]) { // The final comment must close up some hanging divs. $build [ $id ][ '#comment_indent_final' ] = $current_indent ; } } |
Please login to continue.