template_preprocess_forums

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

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',
      );
    }
  }
}
doc_Drupal
2016-10-29 09:46:42
Comments
Leave a Comment

Please login to continue.