template_preprocess_forums(&$variables)
Prepares variables for forums templates.
Default template: forums.html.twig.
Parameters
array $variables: An array containing the following elements:
- forums: An array of all forum objects to display for the given taxonomy term ID. If tid = 0 then all the top-level forums are displayed.
- topics: An array of all the topics in the current forum.
- parents: An array of taxonomy term objects that are ancestors of the current term ID.
- term: Taxonomy term of the current forum.
-
sortby: One of the following integers indicating the sort criteria:
- 1: Date - newest first.
- 2: Date - oldest first.
- 3: Posts with the most comments first.
- 4: Posts with the least comments first.
- forum_per_page: The maximum number of topics to display per page.
File
- core/modules/forum/forum.module, line 407
- Provides discussion forums.
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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | function template_preprocess_forums(& $variables ) { $variables [ 'tid' ] = $variables [ 'term' ]->id(); if ( $variables [ 'forums_defined' ] = count ( $variables [ 'forums' ]) || count ( $variables [ 'parents' ])) { if (! empty ( $variables [ 'forums' ])) { $variables [ 'forums' ] = array ( '#theme' => 'forum_list' , '#forums' => $variables [ 'forums' ], '#parents' => $variables [ 'parents' ], '#tid' => $variables [ 'tid' ], ); } if ( $variables [ 'term' ] && empty ( $variables [ 'term' ]->forum_container->value) && ! empty ( $variables [ 'topics' ])) { $forum_topic_list_header = $variables [ 'header' ]; $table = array ( '#theme' => 'table__forum_topic_list' , '#responsive' => FALSE, '#attributes' => array ( 'id' => 'forum-topic-' . $variables [ 'tid' ]), '#header' => array (), '#rows' => array (), ); if (! empty ( $forum_topic_list_header )) { $table [ '#header' ] = $forum_topic_list_header ; } /** @var \Drupal\node\NodeInterface $topic */ foreach ( $variables [ 'topics' ] as $id => $topic ) { $variables [ 'topics' ][ $id ]->icon = array ( '#theme' => 'forum_icon' , '#new_posts' => $topic -> new , '#num_posts' => $topic ->comment_count, '#comment_mode' => $topic ->comment_mode, '#sticky' => $topic ->isSticky(), '#first_new' => $topic ->first_new, ); // We keep the actual tid in forum table, if it's different from the // current tid then it means the topic appears in two forums, one of // them is a shadow copy. if ( $variables [ 'tid' ] != $topic ->forum_tid) { $variables [ 'topics' ][ $id ]->moved = TRUE; $variables [ 'topics' ][ $id ]->title = $topic ->getTitle(); $variables [ 'topics' ][ $id ]->message = \Drupal::l(t( 'This topic has been moved' ), new Url( 'forum.page' , [ 'taxonomy_term' => $topic ->forum_tid])); } else { $variables [ 'topics' ][ $id ]->moved = FALSE; $variables [ 'topics' ][ $id ]->title_link = \Drupal::l( $topic ->getTitle(), $topic ->urlInfo()); $variables [ 'topics' ][ $id ]->message = '' ; } $forum_submitted = array ( '#theme' => 'forum_submitted' , '#topic' => (object) array ( 'uid' => $topic ->getOwnerId(), 'name' => $topic ->getOwner()->getDisplayName(), 'created' => $topic ->getCreatedTime(), )); $variables [ 'topics' ][ $id ]->submitted = drupal_render( $forum_submitted ); $forum_submitted = array ( '#theme' => 'forum_submitted' , '#topic' => isset( $topic ->last_reply) ? $topic ->last_reply : NULL, ); $variables [ 'topics' ][ $id ]->last_reply = drupal_render( $forum_submitted ); $variables [ 'topics' ][ $id ]->new_text = '' ; $variables [ 'topics' ][ $id ]->new_url = '' ; if ( $topic ->new_replies) { $page_number = \Drupal::entityManager()->getStorage( 'comment' ) ->getNewCommentPageNumber( $topic ->comment_count, $topic ->new_replies, $topic , 'comment_forum' ); $query = $page_number ? array ( 'page' => $page_number ) : NULL; $variables [ 'topics' ][ $id ]->new_text = \Drupal::translation()->formatPlural( $topic ->new_replies, '1 new post<span class="visually-hidden"> in topic %title</span>' , '@count new posts<span class="visually-hidden"> in topic %title</span>' , array ( '%title' => $variables [ 'topics' ][ $id ]->label())); $variables [ 'topics' ][ $id ]->new_url = \Drupal::url( 'entity.node.canonical' , [ 'node' => $topic ->id()], [ 'query' => $query , 'fragment' => 'new' ]); } // Build table rows from topics. $row = array (); $row [] = array ( 'data' => array ( $topic ->icon, array ( '#markup' => '<div class="forum__title"><div>' . $topic ->title_link . '</div><div>' . $topic ->submitted . '</div></div>' , ), ), 'class' => array ( 'forum__topic' ), ); if ( $topic ->moved) { $row [] = array ( 'data' => $topic ->message, 'colspan' => '2' , ); } else { $new_replies = '' ; if ( $topic ->new_replies) { $new_replies = '<br /><a href="' . $topic ->new_url . '">' . $topic ->new_text . '</a>' ; } $row [] = array ( 'data' => [ [ '#prefix' => $topic ->comment_count, '#markup' => $new_replies , ], ], 'class' => array ( 'forum__replies' ), ); $row [] = array ( 'data' => $topic ->last_reply, 'class' => array ( 'forum__last-reply' ), ); } $table [ '#rows' ][] = $row ; } $variables [ 'topics' ] = $table ; $variables [ 'topics_pager' ] = array ( '#type' => 'pager' , ); } } } |
Please login to continue.