function template_preprocess_forum_list
Same name in other branches
- 7.x modules/forum/forum.module \template_preprocess_forum_list()
- 9 core/modules/forum/forum.module \template_preprocess_forum_list()
- 8.9.x core/modules/forum/forum.module \template_preprocess_forum_list()
- 11.x core/modules/forum/forum.module \template_preprocess_forum_list()
Prepares variables for forum list templates.
Default template: forum-list.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.
- parents: An array of taxonomy term objects that are ancestors of the current term ID.
- tid: Taxonomy term ID of the current forum.
File
-
core/
modules/ forum/ forum.module, line 562
Code
function template_preprocess_forum_list(&$variables) {
$user = \Drupal::currentUser();
$row = 0;
// Sanitize each forum so that the template can safely print the data.
foreach ($variables['forums'] as $id => $forum) {
$variables['forums'][$id]->description = [
'#markup' => $forum->description->value,
];
$variables['forums'][$id]->link = forum_uri($forum);
$variables['forums'][$id]->name = $forum->label();
$variables['forums'][$id]->is_container = !empty($forum->forum_container->value);
$variables['forums'][$id]->zebra = $row % 2 == 0 ? 'odd' : 'even';
$row++;
$variables['forums'][$id]->new_text = '';
$variables['forums'][$id]->new_url = '';
$variables['forums'][$id]->new_topics = 0;
$variables['forums'][$id]->old_topics = $forum->num_topics;
$variables['forums'][$id]->icon_class = 'default';
$variables['forums'][$id]->icon_title = t('No new posts');
if ($user->isAuthenticated()) {
$variables['forums'][$id]->new_topics = \Drupal::service('forum_manager')->unreadTopics($forum->id(), $user->id());
if ($variables['forums'][$id]->new_topics) {
$variables['forums'][$id]->new_text = \Drupal::translation()->formatPlural($variables['forums'][$id]->new_topics, '1 new post<span class="visually-hidden"> in forum %title</span>', '@count new posts<span class="visually-hidden"> in forum %title</span>', [
'%title' => $variables['forums'][$id]->label(),
]);
$variables['forums'][$id]->new_url = Url::fromRoute('forum.page', [
'taxonomy_term' => $forum->id(),
], [
'fragment' => 'new',
])
->toString();
$variables['forums'][$id]->icon_class = 'new';
$variables['forums'][$id]->icon_title = t('New posts');
}
$variables['forums'][$id]->old_topics = $forum->num_topics - $variables['forums'][$id]->new_topics;
}
$forum_submitted = [
'#theme' => 'forum_submitted',
'#topic' => $forum->last_post,
];
$variables['forums'][$id]->last_reply = \Drupal::service('renderer')->render($forum_submitted);
}
$variables['pager'] = [
'#type' => 'pager',
];
// Give meaning to $tid for themers. $tid actually stands for term ID.
$variables['forum_id'] = $variables['tid'];
unset($variables['tid']);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.