template_preprocess_forum_list

template_preprocess_forum_list(&$variables)

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 543
Provides discussion forums.

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 = array('#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>', array('%title' => $variables['forums'][$id]->label()));
        $variables['forums'][$id]->new_url = \Drupal::url('forum.page', ['taxonomy_term' => $forum->id()], ['fragment' => 'new']);
        $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 = array('#theme' => 'forum_submitted', '#topic' => $forum->last_post);
    $variables['forums'][$id]->last_reply = drupal_render($forum_submitted);
  }

  $variables['pager'] = array(
    '#type' => 'pager',
  );

  // Give meaning to $tid for themers. $tid actually stands for term ID.
  $variables['forum_id'] = $variables['tid'];
  unset($variables['tid']);
}
doc_Drupal
2016-10-29 09:46:42
Comments
Leave a Comment

Please login to continue.