function 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.
  • tid: Taxonomy term ID 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

drupal/core/modules/forum/forum.module, line 1016
Provides discussion forums.

Code

function template_preprocess_forums(&$variables) {
  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'],
      );
    }
    else {
      $variables['forums'] = array();
    }
    if ($variables['tid'] && array_search($variables['tid'], config('forum.settings')
      ->get('containers')) === FALSE) {
      $variables['topics'] = array(
        '#theme' => 'forum_topic_list',
        '#tid' => $variables['tid'],
        '#topics' => $variables['topics'],
        '#sortby' => $variables['sortby'],
        '#forum_per_page' => $variables['forum_per_page'],
      );
    }
    else {
      $variables['topics'] = array();
    }

    // Provide separate template suggestions based on what's being output. Topic id is also accounted for.
    // Check both variables to be safe then the inverse. Forums with topic ID's take precedence.
    if ($variables['forums'] && !$variables['topics']) {
      $variables['theme_hook_suggestions'][] = 'forums__containers';
      $variables['theme_hook_suggestions'][] = 'forums__' . $variables['tid'];
      $variables['theme_hook_suggestions'][] = 'forums__containers__' . $variables['tid'];
    }
    elseif (!$variables['forums'] && $variables['topics']) {
      $variables['theme_hook_suggestions'][] = 'forums__topics';
      $variables['theme_hook_suggestions'][] = 'forums__' . $variables['tid'];
      $variables['theme_hook_suggestions'][] = 'forums__topics__' . $variables['tid'];
    }
    else {
      $variables['theme_hook_suggestions'][] = 'forums__' . $variables['tid'];
    }
  }
  else {
    $variables['forums'] = array();
    $variables['topics'] = array();
  }
}