Same name and namespace in other branches
  1. 10 core/modules/forum/forum.module \template_preprocess_forums()
  2. 6.x modules/forum/forum.module \template_preprocess_forums()
  3. 7.x modules/forum/forum.module \template_preprocess_forums()
  4. 9 core/modules/forum/forum.module \template_preprocess_forums()

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 406
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'] = [
        '#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 = [
        '#theme' => 'table__forum_topic_list',
        '#responsive' => FALSE,
        '#attributes' => [
          'id' => 'forum-topic-' . $variables['tid'],
        ],
        '#header' => [],
        '#rows' => [],
      ];
      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 = [
          '#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 = Link::fromTextAndUrl(t('This topic has been moved'), Url::fromRoute('forum.page', [
            'taxonomy_term' => $topic->forum_tid,
          ]))
            ->toString();
        }
        else {
          $variables['topics'][$id]->moved = FALSE;
          $variables['topics'][$id]->title_link = Link::fromTextAndUrl($topic
            ->getTitle(), $topic
            ->toUrl())
            ->toString();
          $variables['topics'][$id]->message = '';
        }
        $forum_submitted = [
          '#theme' => 'forum_submitted',
          '#topic' => (object) [
            'uid' => $topic
              ->getOwnerId(),
            'name' => $topic
              ->getOwner()
              ->getDisplayName(),
            'created' => $topic
              ->getCreatedTime(),
          ],
        ];
        $variables['topics'][$id]->submitted = \Drupal::service('renderer')
          ->render($forum_submitted);
        $forum_submitted = [
          '#theme' => 'forum_submitted',
          '#topic' => isset($topic->last_reply) ? $topic->last_reply : NULL,
        ];
        $variables['topics'][$id]->last_reply = \Drupal::service('renderer')
          ->render($forum_submitted);
        $variables['topics'][$id]->new_text = '';
        $variables['topics'][$id]->new_url = '';
        if ($topic->new_replies) {
          $page_number = \Drupal::entityTypeManager()
            ->getStorage('comment')
            ->getNewCommentPageNumber($topic->comment_count, $topic->new_replies, $topic, 'comment_forum');
          $query = $page_number ? [
            '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>', [
            '%title' => $variables['topics'][$id]
              ->label(),
          ]);
          $variables['topics'][$id]->new_url = Url::fromRoute('entity.node.canonical', [
            'node' => $topic
              ->id(),
          ], [
            'query' => $query,
            'fragment' => 'new',
          ])
            ->toString();
        }

        // Build table rows from topics.
        $row = [];
        $row[] = [
          'data' => [
            $topic->icon,
            [
              '#markup' => '<div class="forum__title"><div>' . $topic->title_link . '</div><div>' . $topic->submitted . '</div></div>',
            ],
          ],
          'class' => [
            'forum__topic',
          ],
        ];
        if ($topic->moved) {
          $row[] = [
            '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[] = [
            'data' => [
              [
                '#prefix' => $topic->comment_count,
                '#markup' => $new_replies,
              ],
            ],
            'class' => [
              'forum__replies',
            ],
          ];
          $row[] = [
            'data' => $topic->last_reply,
            'class' => [
              'forum__last-reply',
            ],
          ];
        }
        $table['#rows'][] = $row;
      }
      $variables['topics_original'] = $variables['topics'];
      $variables['topics'] = $table;
      $variables['topics_pager'] = [
        '#type' => 'pager',
      ];
    }
  }
}