forum.module

Provides discussion forums.

File

drupal/core/modules/forum/forum.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Provides discussion forums.
  5. */
  6. use Drupal\Core\Entity\EntityInterface;
  7. use Drupal\entity\Plugin\Core\Entity\EntityDisplay;
  8. use Drupal\taxonomy\Plugin\Core\Entity\Term;
  9. /**
  10. * Implements hook_help().
  11. */
  12. function forum_help($path, $arg) {
  13. switch ($path) {
  14. case 'admin/help#forum':
  15. $output = '';
  16. $output .= '<h3>' . t('About') . '</h3>';
  17. $output .= '<p>' . t('The Forum module lets you create threaded discussion forums with functionality similar to other message board systems. Forums are useful because they allow community members to discuss topics with one another while ensuring those conversations are archived for later reference. In a forum, users post topics and threads in nested hierarchies, allowing discussions to be categorized and grouped. The forum hierarchy consists of:') . '</p>';
  18. $output .= '<ul>';
  19. $output .= '<li>' . t('Optional containers (for example, <em>Support</em>), which can hold:') . '</li>';
  20. $output .= '<ul><li>' . t('Forums (for example, <em>Installing Drupal</em>), which can hold:') . '</li>';
  21. $output .= '<ul><li>' . t('Forum topics submitted by users (for example, <em>How to start a Drupal 6 Multisite</em>), which start discussions and are starting points for:') . '</li>';
  22. $output .= '<ul><li>' . t('Threaded comments submitted by users (for example, <em>You have these options...</em>).') . '</li>';
  23. $output .= '</ul>';
  24. $output .= '</ul>';
  25. $output .= '</ul>';
  26. $output .= '</ul>';
  27. $output .= '<p>' . t('For more information, see the online handbook entry for <a href="@forum">Forum module</a>.', array('@forum' => 'http://drupal.org/documentation/modules/forum')) . '</p>';
  28. $output .= '<h3>' . t('Uses') . '</h3>';
  29. $output .= '<dl>';
  30. $output .= '<dt>' . t('Setting up forum structure') . '</dt>';
  31. $output .= '<dd>' . t('Visit the <a href="@forums">Forums page</a> to set up containers and forums to hold your discussion topics.', array('@forums' => url('admin/structure/forum'))) . '</dd>';
  32. $output .= '<dt>' . t('Starting a discussion') . '</dt>';
  33. $output .= '<dd>' . t('The <a href="@create-topic">Forum topic</a> link on the <a href="@content-add">Add new content</a> page creates the first post of a new threaded discussion, or thread.', array('@create-topic' => url('node/add/forum'), '@content-add' => url('node/add'))) . '</dd>';
  34. $output .= '<dt>' . t('Forum navigation') . '</dt>';
  35. $output .= '<dd>' . t('Enabling the Forum module provides a default <em>Forums</em> menu item in the Tools menu that links to the <a href="@forums">Forums page</a>.', array('@forums' => url('forum'))) . '</dd>';
  36. $output .= '<dt>' . t('Moving forum topics') . '</dt>';
  37. $output .= '<dd>' . t('A forum topic (and all of its comments) may be moved between forums by selecting a different forum while editing a forum topic. When moving a forum topic between forums, the <em>Leave shadow copy</em> option creates a link in the original forum pointing to the new location.') . '</dd>';
  38. $output .= '<dt>' . t('Locking and disabling comments') . '</dt>';
  39. $output .= '<dd>' . t('Selecting <em>Closed</em> under <em>Comment settings</em> while editing a forum topic will lock (prevent new comments on) the thread. Selecting <em>Hidden</em> under <em>Comment settings</em> while editing a forum topic will hide all existing comments on the thread, and prevent new ones.') . '</dd>';
  40. $output .= '</dl>';
  41. return $output;
  42. case 'admin/structure/forum':
  43. $output = '<p>' . t('Forums contain forum topics. Use containers to group related forums.') . '</p>';
  44. $output .= theme('more_help_link', array('url' => 'admin/help/forum'));
  45. return $output;
  46. case 'admin/structure/forum/add/container':
  47. return '<p>' . t('Use containers to group related forums.') . '</p>';
  48. case 'admin/structure/forum/add/forum':
  49. return '<p>' . t('A forum holds related forum topics.') . '</p>';
  50. case 'admin/structure/forum/settings':
  51. return '<p>' . t('Adjust the display of your forum topics. Organize the forums on the <a href="@forum-structure">forum structure page</a>.', array('@forum-structure' => url('admin/structure/forum'))) . '</p>';
  52. }
  53. }
  54. /**
  55. * Implements hook_theme().
  56. */
  57. function forum_theme() {
  58. return array(
  59. 'forums' => array(
  60. 'template' => 'forums',
  61. 'variables' => array('forums' => NULL, 'topics' => NULL, 'parents' => NULL, 'tid' => NULL, 'sortby' => NULL, 'forum_per_page' => NULL),
  62. ),
  63. 'forum_list' => array(
  64. 'template' => 'forum-list',
  65. 'variables' => array('forums' => NULL, 'parents' => NULL, 'tid' => NULL),
  66. ),
  67. 'forum_topic_list' => array(
  68. 'template' => 'forum-topic-list',
  69. 'variables' => array('tid' => NULL, 'topics' => NULL, 'sortby' => NULL, 'forum_per_page' => NULL),
  70. ),
  71. 'forum_icon' => array(
  72. 'template' => 'forum-icon',
  73. 'variables' => array('new_posts' => NULL, 'num_posts' => 0, 'comment_mode' => 0, 'sticky' => 0, 'first_new' => FALSE),
  74. ),
  75. 'forum_submitted' => array(
  76. 'template' => 'forum-submitted',
  77. 'variables' => array('topic' => NULL),
  78. ),
  79. 'forum_form' => array(
  80. 'render element' => 'form',
  81. 'file' => 'forum.admin.inc',
  82. ),
  83. );
  84. }
  85. /**
  86. * Implements hook_menu().
  87. */
  88. function forum_menu() {
  89. $items['forum'] = array(
  90. 'title' => 'Forums',
  91. 'page callback' => 'forum_page',
  92. 'access arguments' => array('access content'),
  93. 'file' => 'forum.pages.inc',
  94. );
  95. $items['forum/%forum_forum'] = array(
  96. 'title' => 'Forums',
  97. 'title callback' => 'entity_page_label',
  98. 'title arguments' => array(1),
  99. 'page callback' => 'forum_page',
  100. 'page arguments' => array(1),
  101. 'access arguments' => array('access content'),
  102. 'file' => 'forum.pages.inc',
  103. );
  104. $items['admin/structure/forum'] = array(
  105. 'title' => 'Forums',
  106. 'description' => 'Control forum hierarchy settings.',
  107. 'page callback' => 'drupal_get_form',
  108. 'page arguments' => array('forum_overview'),
  109. 'access arguments' => array('administer forums'),
  110. 'file' => 'forum.admin.inc',
  111. );
  112. $items['admin/structure/forum/list'] = array(
  113. 'title' => 'List',
  114. 'type' => MENU_DEFAULT_LOCAL_TASK,
  115. );
  116. $items['admin/structure/forum/add/container'] = array(
  117. 'title' => 'Add container',
  118. 'page callback' => 'forum_form_main',
  119. 'page arguments' => array('container'),
  120. 'access arguments' => array('administer forums'),
  121. 'type' => MENU_LOCAL_ACTION,
  122. 'parent' => 'admin/structure/forum',
  123. 'file' => 'forum.admin.inc',
  124. );
  125. $items['admin/structure/forum/add/forum'] = array(
  126. 'title' => 'Add forum',
  127. 'page callback' => 'forum_form_main',
  128. 'page arguments' => array('forum'),
  129. 'access arguments' => array('administer forums'),
  130. 'type' => MENU_LOCAL_ACTION,
  131. 'parent' => 'admin/structure/forum',
  132. 'file' => 'forum.admin.inc',
  133. );
  134. $items['admin/structure/forum/settings'] = array(
  135. 'title' => 'Settings',
  136. 'weight' => 100,
  137. 'type' => MENU_LOCAL_TASK,
  138. 'parent' => 'admin/structure/forum',
  139. 'route_name' => 'forum_settings',
  140. );
  141. $items['admin/structure/forum/edit/container/%taxonomy_term'] = array(
  142. 'title' => 'Edit container',
  143. 'page callback' => 'forum_form_main',
  144. 'page arguments' => array('container', 5),
  145. 'access arguments' => array('administer forums'),
  146. 'file' => 'forum.admin.inc',
  147. );
  148. $items['admin/structure/forum/edit/forum/%taxonomy_term'] = array(
  149. 'title' => 'Edit forum',
  150. 'page callback' => 'forum_form_main',
  151. 'page arguments' => array('forum', 5),
  152. 'access arguments' => array('administer forums'),
  153. 'file' => 'forum.admin.inc',
  154. );
  155. return $items;
  156. }
  157. /**
  158. * Implements hook_menu_local_tasks().
  159. */
  160. function forum_menu_local_tasks(&$data, $router_item, $root_path) {
  161. global $user;
  162. // Add action link to 'node/add/forum' on 'forum' sub-pages.
  163. if ($root_path == 'forum' || $root_path == 'forum/%') {
  164. $tid = (isset($router_item['page_arguments'][0]) ? $router_item['page_arguments'][0]->id() : 0);
  165. $forum_term = forum_forum_load($tid);
  166. if ($forum_term) {
  167. $links = array();
  168. // Loop through all bundles for forum taxonomy vocabulary field.
  169. $field = field_info_field('taxonomy_forums');
  170. foreach ($field['bundles']['node'] as $type) {
  171. if (node_access('create', $type)) {
  172. $links[$type] = array(
  173. '#theme' => 'menu_local_action',
  174. '#link' => array(
  175. 'title' => t('Add new @node_type', array('@node_type' => node_type_get_label($type))),
  176. 'href' => 'node/add/' . $type . '/' . $forum_term->id(),
  177. ),
  178. );
  179. }
  180. }
  181. if (empty($links)) {
  182. // Authenticated user does not have access to create new topics.
  183. if ($user->uid) {
  184. $links['disallowed'] = array(
  185. '#theme' => 'menu_local_action',
  186. '#link' => array(
  187. 'title' => t('You are not allowed to post new content in the forum.'),
  188. ),
  189. );
  190. }
  191. // Anonymous user does not have access to create new topics.
  192. else {
  193. $links['login'] = array(
  194. '#theme' => 'menu_local_action',
  195. '#link' => array(
  196. 'title' => t('<a href="@login">Log in</a> to post new content in the forum.', array(
  197. '@login' => url('user/login', array('query' => drupal_get_destination())),
  198. )),
  199. 'localized_options' => array('html' => TRUE),
  200. ),
  201. );
  202. }
  203. }
  204. $data['actions'] += $links;
  205. }
  206. }
  207. }
  208. /**
  209. * Implements hook_menu_local_tasks_alter().
  210. *
  211. * Remove the 'Add Forum' and 'Add container' local tasks on the delete form.
  212. */
  213. function forum_menu_local_tasks_alter(&$data, $router_item, $root_path) {
  214. if ($root_path == 'admin/structure/forum' && !empty($router_item['map'][3]) &&
  215. $router_item['map'][3] == 'delete') {
  216. $data = array();
  217. }
  218. }
  219. /**
  220. * Implements hook_entity_bundle_info_alter().
  221. */
  222. function forum_entity_bundle_info_alter(&$bundles) {
  223. // Take over URI construction for taxonomy terms that are forums.
  224. if ($vid = config('forum.settings')->get('vocabulary')) {
  225. if (isset($bundles['taxonomy_term'][$vid])) {
  226. $bundles['taxonomy_term'][$vid]['uri_callback'] = 'forum_uri';
  227. }
  228. }
  229. }
  230. /**
  231. * Entity URI callback used in forum_entity_info_alter().
  232. */
  233. function forum_uri($forum) {
  234. return array(
  235. 'path' => 'forum/' . $forum->id(),
  236. );
  237. }
  238. /**
  239. * Checks whether a node can be used in a forum, based on its content type.
  240. *
  241. * @param \Drupal\Core\Entity\EntityInterface $node
  242. * A node entity.
  243. *
  244. * @return
  245. * Boolean indicating if the node can be assigned to a forum.
  246. */
  247. function _forum_node_check_node_type(EntityInterface $node) {
  248. // Fetch information about the forum field.
  249. $instance = field_info_instance('node', 'taxonomy_forums', $node->type);
  250. return !empty($instance);
  251. }
  252. /**
  253. * Implements hook_node_view().
  254. */
  255. function forum_node_view(EntityInterface $node, EntityDisplay $display, $view_mode) {
  256. $vid = config('forum.settings')->get('vocabulary');
  257. $vocabulary = taxonomy_vocabulary_load($vid);
  258. if (_forum_node_check_node_type($node)) {
  259. if ($view_mode == 'full' && node_is_page($node)) {
  260. // Breadcrumb navigation
  261. $breadcrumb[] = l(t('Home'), NULL);
  262. $breadcrumb[] = l($vocabulary->name, 'forum');
  263. if ($parents = taxonomy_term_load_parents_all($node->forum_tid)) {
  264. $parents = array_reverse($parents);
  265. foreach ($parents as $parent) {
  266. $breadcrumb[] = l($parent->label(), 'forum/' . $parent->id());
  267. }
  268. }
  269. drupal_set_breadcrumb($breadcrumb);
  270. }
  271. }
  272. }
  273. /**
  274. * Implements hook_node_validate().
  275. *
  276. * Checks in particular that the node is assigned only a "leaf" term in the
  277. * forum taxonomy.
  278. */
  279. function forum_node_validate(EntityInterface $node, $form) {
  280. if (_forum_node_check_node_type($node)) {
  281. $langcode = $form['taxonomy_forums']['#language'];
  282. // vocabulary is selected, not a "container" term.
  283. if (!empty($node->taxonomy_forums[$langcode])) {
  284. // Extract the node's proper topic ID.
  285. $containers = config('forum.settings')->get('containers');
  286. foreach ($node->taxonomy_forums[$langcode] as $delta => $item) {
  287. // If no term was selected (e.g. when no terms exist yet), remove the
  288. // item.
  289. if (empty($item['tid'])) {
  290. unset($node->taxonomy_forums[$langcode][$delta]);
  291. continue;
  292. }
  293. $term = taxonomy_term_load($item['tid']);
  294. if (!$term) {
  295. form_set_error('taxonomy_forums', t('Select a forum.'));
  296. continue;
  297. }
  298. $used = db_query_range('SELECT 1 FROM {taxonomy_term_data} WHERE tid = :tid AND vid = :vid', 0, 1, array(
  299. ':tid' => $term->id(),
  300. ':vid' => $term->bundle(),
  301. ))->fetchField();
  302. if ($used && in_array($term->id(), $containers)) {
  303. form_set_error('taxonomy_forums', t('The item %forum is a forum container, not a forum. Select one of the forums below instead.', array('%forum' => $term->label())));
  304. }
  305. }
  306. }
  307. }
  308. }
  309. /**
  310. * Implements hook_node_presave().
  311. *
  312. * Assigns the forum taxonomy when adding a topic from within a forum.
  313. */
  314. function forum_node_presave(EntityInterface $node) {
  315. if (_forum_node_check_node_type($node)) {
  316. // Make sure all fields are set properly:
  317. $node->icon = !empty($node->icon) ? $node->icon : '';
  318. reset($node->taxonomy_forums);
  319. $langcode = key($node->taxonomy_forums);
  320. if (!empty($node->taxonomy_forums[$langcode])) {
  321. $node->forum_tid = $node->taxonomy_forums[$langcode][0]['tid'];
  322. // Only do a shadow copy check if this is not a new node.
  323. if (!$node->isNew()) {
  324. $old_tid = db_query_range("SELECT f.tid FROM {forum} f INNER JOIN {node} n ON f.vid = n.vid WHERE n.nid = :nid ORDER BY f.vid DESC", 0, 1, array(':nid' => $node->nid))->fetchField();
  325. if ($old_tid && isset($node->forum_tid) && ($node->forum_tid != $old_tid) && !empty($node->shadow)) {
  326. // A shadow copy needs to be created. Retain new term and add old term.
  327. $node->taxonomy_forums[$langcode][] = array('tid' => $old_tid);
  328. }
  329. }
  330. }
  331. }
  332. }
  333. /**
  334. * Implements hook_node_update().
  335. */
  336. function forum_node_update(EntityInterface $node) {
  337. if (_forum_node_check_node_type($node)) {
  338. // If this is not a new revision and does exist, update the forum record,
  339. // otherwise insert a new one.
  340. if ($node->getRevisionId() == $node->original->getRevisionId() && db_query('SELECT tid FROM {forum} WHERE nid=:nid', array(':nid' => $node->nid))->fetchField()) {
  341. if (!empty($node->forum_tid)) {
  342. db_update('forum')
  343. ->fields(array('tid' => $node->forum_tid))
  344. ->condition('vid', $node->vid)
  345. ->execute();
  346. }
  347. // The node is removed from the forum.
  348. else {
  349. db_delete('forum')
  350. ->condition('nid', $node->nid)
  351. ->execute();
  352. }
  353. }
  354. else {
  355. if (!empty($node->forum_tid)) {
  356. db_insert('forum')
  357. ->fields(array(
  358. 'tid' => $node->forum_tid,
  359. 'vid' => $node->vid,
  360. 'nid' => $node->nid,
  361. ))
  362. ->execute();
  363. }
  364. }
  365. // If the node has a shadow forum topic, update the record for this
  366. // revision.
  367. if (!empty($node->shadow)) {
  368. db_delete('forum')
  369. ->condition('nid', $node->nid)
  370. ->condition('vid', $node->vid)
  371. ->execute();
  372. db_insert('forum')
  373. ->fields(array(
  374. 'nid' => $node->nid,
  375. 'vid' => $node->vid,
  376. 'tid' => $node->forum_tid,
  377. ))
  378. ->execute();
  379. }
  380. }
  381. }
  382. /**
  383. * Implements hook_node_insert().
  384. */
  385. function forum_node_insert(EntityInterface $node) {
  386. if (_forum_node_check_node_type($node)) {
  387. if (!empty($node->forum_tid)) {
  388. $nid = db_insert('forum')
  389. ->fields(array(
  390. 'tid' => $node->forum_tid,
  391. 'vid' => $node->vid,
  392. 'nid' => $node->nid,
  393. ))
  394. ->execute();
  395. }
  396. }
  397. }
  398. /**
  399. * Implements hook_node_predelete().
  400. */
  401. function forum_node_predelete(EntityInterface $node) {
  402. if (_forum_node_check_node_type($node)) {
  403. db_delete('forum')
  404. ->condition('nid', $node->nid)
  405. ->execute();
  406. db_delete('forum_index')
  407. ->condition('nid', $node->nid)
  408. ->execute();
  409. }
  410. }
  411. /**
  412. * Implements hook_node_load().
  413. */
  414. function forum_node_load($nodes) {
  415. $node_vids = array();
  416. foreach ($nodes as $node) {
  417. if (_forum_node_check_node_type($node)) {
  418. $node_vids[] = $node->vid;
  419. }
  420. }
  421. if (!empty($node_vids)) {
  422. $query = db_select('forum', 'f');
  423. $query
  424. ->fields('f', array('nid', 'tid'))
  425. ->condition('f.vid', $node_vids);
  426. $result = $query->execute();
  427. foreach ($result as $record) {
  428. $nodes[$record->nid]->forum_tid = $record->tid;
  429. }
  430. }
  431. }
  432. /**
  433. * Implements hook_node_info().
  434. */
  435. function forum_node_info() {
  436. return array(
  437. 'forum' => array(
  438. 'name' => t('Forum topic'),
  439. 'base' => 'forum',
  440. 'description' => t('A <em>forum topic</em> starts a new discussion thread within a forum.'),
  441. 'title_label' => t('Subject'),
  442. )
  443. );
  444. }
  445. /**
  446. * Implements hook_permission().
  447. */
  448. function forum_permission() {
  449. $perms = array(
  450. 'administer forums' => array(
  451. 'title' => t('Administer forums'),
  452. ),
  453. );
  454. return $perms;
  455. }
  456. /**
  457. * Implements hook_taxonomy_term_delete().
  458. */
  459. function forum_taxonomy_term_delete(Term $term) {
  460. // For containers, remove the tid from the forum_containers variable.
  461. $config = config('forum.settings');
  462. $containers = $config->get('containers');
  463. $key = array_search($term->id(), $containers);
  464. if ($key !== FALSE) {
  465. unset($containers[$key]);
  466. }
  467. $config->set('containers', $containers)->save();
  468. }
  469. /**
  470. * Implements hook_comment_publish().
  471. *
  472. * This actually handles the insertion and update of published nodes since
  473. * $comment->save() calls hook_comment_publish() for all published comments.
  474. */
  475. function forum_comment_publish($comment) {
  476. _forum_update_forum_index($comment->nid->target_id);
  477. }
  478. /**
  479. * Implements hook_comment_update().
  480. *
  481. * The Comment module doesn't call hook_comment_unpublish() when saving
  482. * individual comments, so we need to check for those here.
  483. */
  484. function forum_comment_update($comment) {
  485. // $comment->save() calls hook_comment_publish() for all published comments,
  486. // so we need to handle all other values here.
  487. if (!$comment->status->value) {
  488. _forum_update_forum_index($comment->nid->target_id);
  489. }
  490. }
  491. /**
  492. * Implements hook_comment_unpublish().
  493. */
  494. function forum_comment_unpublish($comment) {
  495. _forum_update_forum_index($comment->nid->target_id);
  496. }
  497. /**
  498. * Implements hook_comment_delete().
  499. */
  500. function forum_comment_delete($comment) {
  501. _forum_update_forum_index($comment->nid->target_id);
  502. }
  503. /**
  504. * Implements hook_field_storage_pre_insert().
  505. */
  506. function forum_field_storage_pre_insert(EntityInterface $entity, &$skip_fields) {
  507. if ($entity->entityType() == 'node' && $entity->status && _forum_node_check_node_type($entity)) {
  508. $query = db_insert('forum_index')->fields(array('nid', 'title', 'tid', 'sticky', 'created', 'comment_count', 'last_comment_timestamp'));
  509. foreach ($entity->getTranslationLanguages() as $langcode => $language) {
  510. $translation = $entity->getTranslation($langcode, FALSE);
  511. $query->values(array(
  512. 'nid' => $entity->id(),
  513. 'title' => $translation->title->value,
  514. 'tid' => $translation->taxonomy_forums->tid,
  515. 'sticky' => $entity->sticky,
  516. 'created' => $entity->created,
  517. 'comment_count' => 0,
  518. 'last_comment_timestamp' => $entity->created,
  519. ));
  520. }
  521. $query->execute();
  522. }
  523. }
  524. /**
  525. * Implements hook_field_storage_pre_update().
  526. */
  527. function forum_field_storage_pre_update(EntityInterface $entity, &$skip_fields) {
  528. $first_call = &drupal_static(__FUNCTION__, array());
  529. if ($entity->entityType() == 'node' && _forum_node_check_node_type($entity)) {
  530. // If the node is published, update the forum index.
  531. if ($entity->status) {
  532. // We don't maintain data for old revisions, so clear all previous values
  533. // from the table. Since this hook runs once per field, per object, make
  534. // sure we only wipe values once.
  535. if (!isset($first_call[$entity->nid])) {
  536. $first_call[$entity->nid] = FALSE;
  537. db_delete('forum_index')->condition('nid', $entity->nid)->execute();
  538. }
  539. $query = db_insert('forum_index')->fields(array('nid', 'title', 'tid', 'sticky', 'created', 'comment_count', 'last_comment_timestamp'));
  540. foreach ($entity->taxonomy_forums as $language) {
  541. foreach ($language as $item) {
  542. $query->values(array(
  543. 'nid' => $entity->nid,
  544. 'title' => $entity->title,
  545. 'tid' => $item['tid'],
  546. 'sticky' => $entity->sticky,
  547. 'created' => $entity->created,
  548. 'comment_count' => 0,
  549. 'last_comment_timestamp' => $entity->created,
  550. ));
  551. }
  552. }
  553. $query->execute();
  554. // The logic for determining last_comment_count is fairly complex, so
  555. // call _forum_update_forum_index() too.
  556. _forum_update_forum_index($entity->nid);
  557. }
  558. // When a forum node is unpublished, remove it from the forum_index table.
  559. else {
  560. db_delete('forum_index')->condition('nid', $entity->nid)->execute();
  561. }
  562. }
  563. }
  564. /**
  565. * Implements hook_form_BASE_FORM_ID_alter().
  566. */
  567. function forum_form_taxonomy_vocabulary_form_alter(&$form, &$form_state, $form_id) {
  568. $vid = config('forum.settings')->get('vocabulary');
  569. $vocabulary = $form_state['controller']->getEntity();
  570. if ($vid == $vocabulary->id()) {
  571. $form['help_forum_vocab'] = array(
  572. '#markup' => t('This is the designated forum vocabulary. Some of the normal vocabulary options have been removed.'),
  573. '#weight' => -1,
  574. );
  575. // Forum's vocabulary always has single hierarchy. Forums and containers
  576. // have only one parent or no parent for root items. By default this value
  577. // is 0.
  578. $form['hierarchy']['#value'] = TAXONOMY_HIERARCHY_SINGLE;
  579. // Do not allow to delete forum's vocabulary.
  580. $form['actions']['delete']['#access'] = FALSE;
  581. // Do not allow to change a vid of forum's vocabulary.
  582. $form['vid']['#disabled'] = TRUE;
  583. }
  584. }
  585. /**
  586. * Implements hook_form_FORM_ID_alter() for taxonomy_term_form().
  587. */
  588. function forum_form_taxonomy_term_form_alter(&$form, &$form_state, $form_id) {
  589. $vid = config('forum.settings')->get('vocabulary');
  590. if (isset($form['vid']['#value']) && $form['vid']['#value'] == $vid) {
  591. // Hide multiple parents select from forum terms.
  592. $form['relations']['parent']['#access'] = FALSE;
  593. }
  594. }
  595. /**
  596. * Implements hook_form_BASE_FORM_ID_alter() for node_form().
  597. */
  598. function forum_form_node_form_alter(&$form, &$form_state, $form_id) {
  599. if (isset($form['taxonomy_forums'])) {
  600. $langcode = $form['taxonomy_forums']['#language'];
  601. // Make the vocabulary required for 'real' forum-nodes.
  602. $form['taxonomy_forums'][$langcode]['#required'] = TRUE;
  603. $form['taxonomy_forums'][$langcode]['#multiple'] = FALSE;
  604. if (empty($form['taxonomy_forums'][$langcode]['#default_value'])) {
  605. // If there is no default forum already selected, try to get the forum
  606. // ID from the URL (e.g., if we are on a page like node/add/forum/2, we
  607. // expect "2" to be the ID of the forum that was requested).
  608. $requested_forum_id = arg(3);
  609. $form['taxonomy_forums'][$langcode]['#default_value'] = is_numeric($requested_forum_id) ? $requested_forum_id : '';
  610. }
  611. }
  612. }
  613. /**
  614. * Render API callback: Lists nodes based on the element's #query property.
  615. *
  616. * This function can be used as a #pre_render callback.
  617. *
  618. * @see \Drupal\forum\Plugin\block\block\NewTopicsBlock::build()
  619. * @see \Drupal\forum\Plugin\block\block\ActiveTopicsBlock::build()
  620. */
  621. function forum_block_view_pre_render($elements) {
  622. $result = $elements['#query']->execute();
  623. if ($node_title_list = node_title_list($result)) {
  624. $elements['forum_list'] = $node_title_list;
  625. $elements['forum_more'] = array('#theme' => 'more_link', '#url' => 'forum', '#title' => t('Read the latest forum topics.'));
  626. }
  627. return $elements;
  628. }
  629. /**
  630. * Implements hook_form().
  631. */
  632. function forum_form(EntityInterface $node, &$form_state) {
  633. $type = node_type_load($node->type);
  634. $form['title'] = array(
  635. '#type' => 'textfield',
  636. '#title' => check_plain($type->title_label),
  637. '#default_value' => !empty($node->title) ? $node->title : '',
  638. '#required' => TRUE, '#weight' => -5
  639. );
  640. if (!empty($node->nid)) {
  641. $forum_terms = $node->taxonomy_forums;
  642. // If editing, give option to leave shadows.
  643. $shadow = (count($forum_terms) > 1);
  644. $form['shadow'] = array('#type' => 'checkbox', '#title' => t('Leave shadow copy'), '#default_value' => $shadow, '#description' => t('If you move this topic, you can leave a link in the old forum to the new forum.'));
  645. $form['forum_tid'] = array('#type' => 'value', '#value' => $node->forum_tid);
  646. }
  647. return $form;
  648. }
  649. /**
  650. * Returns a tree of all forums for a given taxonomy term ID.
  651. *
  652. * @param $tid
  653. * (optional) Taxonomy term ID of the forum. If not given all forums will be
  654. * returned.
  655. *
  656. * @return
  657. * A tree of taxonomy objects, with the following additional properties:
  658. * - num_topics: Number of topics in the forum.
  659. * - num_posts: Total number of posts in all topics.
  660. * - last_post: Most recent post for the forum.
  661. * - forums: An array of child forums.
  662. */
  663. function forum_forum_load($tid = NULL) {
  664. $cache = &drupal_static(__FUNCTION__, array());
  665. // Return a cached forum tree if available.
  666. if (!isset($tid)) {
  667. $tid = 0;
  668. }
  669. if (isset($cache[$tid])) {
  670. return $cache[$tid];
  671. }
  672. $config = config('forum.settings');
  673. $vid = $config->get('vocabulary');
  674. // Load and validate the parent term.
  675. if ($tid) {
  676. $forum_term = taxonomy_term_load($tid);
  677. if (!$forum_term || ($forum_term->bundle() != $vid)) {
  678. return $cache[$tid] = FALSE;
  679. }
  680. }
  681. // If $tid is 0, create an empty entity to hold the child terms.
  682. elseif ($tid === 0) {
  683. $forum_term = entity_create('taxonomy_term', array(
  684. 'tid' => 0,
  685. 'vid' => $vid,
  686. ));
  687. }
  688. // Determine if the requested term is a container.
  689. if (!$forum_term->id() || in_array($forum_term->id(), $config->get('containers'))) {
  690. $forum_term->container = 1;
  691. }
  692. // Load parent terms.
  693. $forum_term->parents = taxonomy_term_load_parents_all($forum_term->id());
  694. // Load the tree below.
  695. $forums = array();
  696. $_forums = taxonomy_get_tree($vid, $tid, NULL, TRUE);
  697. if (count($_forums)) {
  698. $query = db_select('node_field_data', 'n');
  699. $query->join('node_comment_statistics', 'ncs', 'n.nid = ncs.nid');
  700. $query->join('forum', 'f', 'n.vid = f.vid');
  701. $query->addExpression('COUNT(n.nid)', 'topic_count');
  702. $query->addExpression('SUM(ncs.comment_count)', 'comment_count');
  703. $counts = $query
  704. ->fields('f', array('tid'))
  705. ->condition('n.status', 1)
  706. // @todo This should be actually filtering on the desired node status
  707. // field language and just fall back to the default language.
  708. ->condition('n.default_langcode', 1)
  709. ->groupBy('tid')
  710. ->addTag('node_access')
  711. ->execute()
  712. ->fetchAllAssoc('tid');
  713. }
  714. foreach ($_forums as $forum) {
  715. // Determine if the child term is a container.
  716. if (in_array($forum->id(), $config->get('containers'))) {
  717. $forum->container = 1;
  718. }
  719. // Merge in the topic and post counters.
  720. if (!empty($counts[$forum->id()])) {
  721. $forum->num_topics = $counts[$forum->id()]->topic_count;
  722. $forum->num_posts = $counts[$forum->id()]->topic_count + $counts[$forum->id()]->comment_count;
  723. }
  724. else {
  725. $forum->num_topics = 0;
  726. $forum->num_posts = 0;
  727. }
  728. // Query "Last Post" information for this forum.
  729. $query = db_select('node_field_data', 'n');
  730. $query->join('forum', 'f', 'n.vid = f.vid AND f.tid = :tid', array(':tid' => $forum->id()));
  731. $query->join('node_comment_statistics', 'ncs', 'n.nid = ncs.nid');
  732. $query->join('users', 'u', 'ncs.last_comment_uid = u.uid');
  733. $query->addExpression('CASE ncs.last_comment_uid WHEN 0 THEN ncs.last_comment_name ELSE u.name END', 'last_comment_name');
  734. $topic = $query
  735. ->fields('ncs', array('last_comment_timestamp', 'last_comment_uid'))
  736. ->condition('n.status', 1)
  737. // @todo This should be actually filtering on the desired node status
  738. // field language and just fall back to the default language.
  739. ->condition('n.default_langcode', 1)
  740. ->orderBy('last_comment_timestamp', 'DESC')
  741. ->range(0, 1)
  742. ->addTag('node_access')
  743. ->execute()
  744. ->fetchObject();
  745. // Merge in the "Last Post" information.
  746. $last_post = new stdClass();
  747. if (!empty($topic->last_comment_timestamp)) {
  748. $last_post->created = $topic->last_comment_timestamp;
  749. $last_post->name = $topic->last_comment_name;
  750. $last_post->uid = $topic->last_comment_uid;
  751. }
  752. $forum->last_post = $last_post;
  753. $forums[$forum->id()] = $forum;
  754. }
  755. // Cache the result, and return the tree.
  756. $forum_term->forums = $forums;
  757. $cache[$tid] = $forum_term;
  758. return $forum_term;
  759. }
  760. /**
  761. * Calculates the number of new posts in a forum that the user has not yet read.
  762. *
  763. * Nodes are new if they are newer than HISTORY_READ_LIMIT.
  764. *
  765. * @param $term
  766. * The term ID of the forum.
  767. * @param $uid
  768. * The user ID.
  769. *
  770. * @return
  771. * The number of new posts in the forum that have not been read by the user.
  772. */
  773. function _forum_topics_unread($term, $uid) {
  774. $query = db_select('node_field_data', 'n');
  775. $query->join('forum', 'f', 'n.vid = f.vid AND f.tid = :tid', array(':tid' => $term));
  776. $query->leftJoin('history', 'h', 'n.nid = h.nid AND h.uid = :uid', array(':uid' => $uid));
  777. $query->addExpression('COUNT(n.nid)', 'count');
  778. return $query
  779. ->condition('status', 1)
  780. // @todo This should be actually filtering on the desired node status field
  781. // language and just fall back to the default language.
  782. ->condition('n.default_langcode', 1)
  783. ->condition('n.created', HISTORY_READ_LIMIT, '>')
  784. ->isNull('h.nid')
  785. ->addTag('node_access')
  786. ->execute()
  787. ->fetchField();
  788. }
  789. /**
  790. * Gets all the topics in a forum.
  791. *
  792. * @param $tid
  793. * The term ID of the forum.
  794. * @param $sortby
  795. * One of the following integers indicating the sort criteria:
  796. * - 1: Date - newest first.
  797. * - 2: Date - oldest first.
  798. * - 3: Posts with the most comments first.
  799. * - 4: Posts with the least comments first.
  800. * @param $forum_per_page
  801. * The maximum number of topics to display per page.
  802. *
  803. * @return
  804. * A list of all the topics in a forum.
  805. */
  806. function forum_get_topics($tid, $sortby, $forum_per_page) {
  807. global $user, $forum_topic_list_header;
  808. $forum_topic_list_header = array(
  809. array('data' => t('Topic'), 'field' => 'f.title'),
  810. array('data' => t('Replies'), 'field' => 'f.comment_count'),
  811. array('data' => t('Last reply'), 'field' => 'f.last_comment_timestamp'),
  812. );
  813. $order = _forum_get_topic_order($sortby);
  814. for ($i = 0; $i < count($forum_topic_list_header); $i++) {
  815. if ($forum_topic_list_header[$i]['field'] == $order['field']) {
  816. $forum_topic_list_header[$i]['sort'] = $order['sort'];
  817. }
  818. }
  819. $query = db_select('forum_index', 'f')
  820. ->extend('Drupal\Core\Database\Query\PagerSelectExtender')
  821. ->extend('Drupal\Core\Database\Query\TableSortExtender');
  822. $query->fields('f');
  823. $query
  824. ->condition('f.tid', $tid)
  825. ->addTag('node_access')
  826. ->addMetaData('base_table', 'forum_index')
  827. ->orderBy('f.sticky', 'DESC')
  828. ->orderByHeader($forum_topic_list_header)
  829. ->limit($forum_per_page);
  830. $count_query = db_select('forum_index', 'f');
  831. $count_query->condition('f.tid', $tid);
  832. $count_query->addExpression('COUNT(*)');
  833. $count_query->addTag('node_access');
  834. $count_query->addMetaData('base_table', 'forum_index');
  835. $query->setCountQuery($count_query);
  836. $result = $query->execute();
  837. $nids = array();
  838. foreach ($result as $record) {
  839. $nids[] = $record->nid;
  840. }
  841. if ($nids) {
  842. $nodes = node_load_multiple($nids);
  843. $query = db_select('node_field_data', 'n')
  844. ->extend('Drupal\Core\Database\Query\TableSortExtender');
  845. $query->fields('n', array('nid'));
  846. $query->join('node_comment_statistics', 'ncs', 'n.nid = ncs.nid');
  847. $query->fields('ncs', array('cid', 'last_comment_uid', 'last_comment_timestamp', 'comment_count'));
  848. $query->join('forum_index', 'f', 'f.nid = ncs.nid');
  849. $query->addField('f', 'tid', 'forum_tid');
  850. $query->join('users', 'u', 'n.uid = u.uid');
  851. $query->addField('u', 'name');
  852. $query->join('users', 'u2', 'ncs.last_comment_uid = u2.uid');
  853. $query->addExpression('CASE ncs.last_comment_uid WHEN 0 THEN ncs.last_comment_name ELSE u2.name END', 'last_comment_name');
  854. $query
  855. ->orderBy('f.sticky', 'DESC')
  856. ->orderByHeader($forum_topic_list_header)
  857. ->condition('n.nid', $nids)
  858. // @todo This should be actually filtering on the desired node language
  859. // and just fall back to the default language.
  860. ->condition('n.default_langcode', 1);
  861. $result = array();
  862. foreach ($query->execute() as $row) {
  863. $topic = $nodes[$row->nid];
  864. $topic->comment_mode = $topic->comment;
  865. foreach ($row as $key => $value) {
  866. $topic->{$key} = $value;
  867. }
  868. $result[] = $topic;
  869. }
  870. }
  871. else {
  872. $result = array();
  873. }
  874. $topics = array();
  875. $first_new_found = FALSE;
  876. foreach ($result as $topic) {
  877. if ($user->uid) {
  878. // A forum is new if the topic is new, or if there are new comments since
  879. // the user's last visit.
  880. if ($topic->forum_tid != $tid) {
  881. $topic->new = 0;
  882. }
  883. else {
  884. $history = _forum_user_last_visit($topic->nid);
  885. $topic->new_replies = comment_num_new($topic->nid, $history);
  886. $topic->new = $topic->new_replies || ($topic->last_comment_timestamp > $history);
  887. }
  888. }
  889. else {
  890. // Do not track "new replies" status for topics if the user is anonymous.
  891. $topic->new_replies = 0;
  892. $topic->new = 0;
  893. }
  894. // Make sure only one topic is indicated as the first new topic.
  895. $topic->first_new = FALSE;
  896. if ($topic->new != 0 && !$first_new_found) {
  897. $topic->first_new = TRUE;
  898. $first_new_found = TRUE;
  899. }
  900. if ($topic->comment_count > 0) {
  901. $last_reply = new stdClass();
  902. $last_reply->created = $topic->last_comment_timestamp;
  903. $last_reply->name = $topic->last_comment_name;
  904. $last_reply->uid = $topic->last_comment_uid;
  905. $topic->last_reply = $last_reply;
  906. }
  907. $topics[$topic->nid] = $topic;
  908. }
  909. return $topics;
  910. }
  911. /**
  912. * Implements hook_preprocess_HOOK() for block.html.twig.
  913. */
  914. function forum_preprocess_block(&$variables) {
  915. if ($variables['configuration']['module'] == 'forum') {
  916. $variables['attributes']['role'] = 'navigation';
  917. }
  918. }
  919. /**
  920. * Prepares variables for forums templates.
  921. *
  922. * Default template: forums.html.twig.
  923. *
  924. * @param array $variables
  925. * An array containing the following elements:
  926. * - forums: An array of all forum objects to display for the given taxonomy
  927. * term ID. If tid = 0 then all the top-level forums are displayed.
  928. * - topics: An array of all the topics in the current forum.
  929. * - parents: An array of taxonomy term objects that are ancestors of the
  930. * current term ID.
  931. * - tid: Taxonomy term ID of the current forum.
  932. * - sortby: One of the following integers indicating the sort criteria:
  933. * - 1: Date - newest first.
  934. * - 2: Date - oldest first.
  935. * - 3: Posts with the most comments first.
  936. * - 4: Posts with the least comments first.
  937. * - forum_per_page: The maximum number of topics to display per page.
  938. */
  939. function template_preprocess_forums(&$variables) {
  940. if ($variables['forums_defined'] = count($variables['forums']) || count($variables['parents'])) {
  941. if (!empty($variables['forums'])) {
  942. $variables['forums'] = array(
  943. '#theme' => 'forum_list',
  944. '#forums' => $variables['forums'],
  945. '#parents' => $variables['parents'],
  946. '#tid' => $variables['tid'],
  947. );
  948. }
  949. else {
  950. $variables['forums'] = array();
  951. }
  952. if ($variables['tid'] && array_search($variables['tid'], config('forum.settings')->get('containers')) === FALSE) {
  953. $variables['topics'] = array(
  954. '#theme' => 'forum_topic_list',
  955. '#tid' => $variables['tid'],
  956. '#topics' => $variables['topics'],
  957. '#sortby' => $variables['sortby'],
  958. '#forum_per_page' => $variables['forum_per_page'],
  959. );
  960. }
  961. else {
  962. $variables['topics'] = array();
  963. }
  964. // Provide separate template suggestions based on what's being output. Topic id is also accounted for.
  965. // Check both variables to be safe then the inverse. Forums with topic ID's take precedence.
  966. if ($variables['forums'] && !$variables['topics']) {
  967. $variables['theme_hook_suggestions'][] = 'forums__containers';
  968. $variables['theme_hook_suggestions'][] = 'forums__' . $variables['tid'];
  969. $variables['theme_hook_suggestions'][] = 'forums__containers__' . $variables['tid'];
  970. }
  971. elseif (!$variables['forums'] && $variables['topics']) {
  972. $variables['theme_hook_suggestions'][] = 'forums__topics';
  973. $variables['theme_hook_suggestions'][] = 'forums__' . $variables['tid'];
  974. $variables['theme_hook_suggestions'][] = 'forums__topics__' . $variables['tid'];
  975. }
  976. else {
  977. $variables['theme_hook_suggestions'][] = 'forums__' . $variables['tid'];
  978. }
  979. }
  980. else {
  981. $variables['forums'] = array();
  982. $variables['topics'] = array();
  983. }
  984. }
  985. /**
  986. * Prepares variables for forum list templates.
  987. *
  988. * Default template: forum-list.html.twig.
  989. *
  990. * @param array $variables
  991. * An array containing the following elements:
  992. * - forums: An array of all forum objects to display for the given taxonomy
  993. * term ID. If tid = 0 then all the top-level forums are displayed.
  994. * - parents: An array of taxonomy term objects that are ancestors of the
  995. * current term ID.
  996. * - tid: Taxonomy term ID of the current forum.
  997. */
  998. function template_preprocess_forum_list(&$variables) {
  999. global $user;
  1000. $row = 0;
  1001. // Sanitize each forum so that the template can safely print the data.
  1002. foreach ($variables['forums'] as $id => $forum) {
  1003. $variables['forums'][$id]->description = filter_xss_admin($forum->description->value);
  1004. $variables['forums'][$id]->link = url("forum/" . $forum->id());
  1005. $variables['forums'][$id]->name = check_plain($forum->label());
  1006. $variables['forums'][$id]->is_container = !empty($forum->container);
  1007. $variables['forums'][$id]->zebra = $row % 2 == 0 ? 'odd' : 'even';
  1008. $row++;
  1009. $variables['forums'][$id]->new_text = '';
  1010. $variables['forums'][$id]->new_url = '';
  1011. $variables['forums'][$id]->new_topics = 0;
  1012. $variables['forums'][$id]->old_topics = $forum->num_topics;
  1013. $variables['forums'][$id]->icon_class = 'default';
  1014. $variables['forums'][$id]->icon_title = t('No new posts');
  1015. if ($user->uid) {
  1016. $variables['forums'][$id]->new_topics = _forum_topics_unread($forum->id(), $user->uid);
  1017. if ($variables['forums'][$id]->new_topics) {
  1018. $variables['forums'][$id]->new_text = format_plural($variables['forums'][$id]->new_topics, '1 new post<span class="element-invisible"> in forum %title</span>', '@count new posts<span class="element-invisible"> in forum %title</span>', array('%title' => $variables['forums'][$id]->label()));
  1019. $variables['forums'][$id]->new_url = url('forum/' . $forum->id(), array('fragment' => 'new'));
  1020. $variables['forums'][$id]->icon_class = 'new';
  1021. $variables['forums'][$id]->icon_title = t('New posts');
  1022. }
  1023. $variables['forums'][$id]->old_topics = $forum->num_topics - $variables['forums'][$id]->new_topics;
  1024. }
  1025. $variables['forums'][$id]->last_reply = theme('forum_submitted', array('topic' => $forum->last_post));
  1026. }
  1027. $variables['pager'] = array(
  1028. '#theme' => 'pager',
  1029. );
  1030. // Give meaning to $tid for themers. $tid actually stands for term ID.
  1031. $variables['forum_id'] = $variables['tid'];
  1032. unset($variables['tid']);
  1033. }
  1034. /**
  1035. * Prepares variables for forum topic list templates.
  1036. *
  1037. * Default template: forum-topic-list.html.twig.
  1038. *
  1039. * @param array $variables
  1040. * An array containing the following elements:
  1041. * - tid: Taxonomy term ID of the current forum.
  1042. * - topics: An array of all the topics in the current forum.
  1043. * - forum_per_page: The maximum number of topics to display per page.
  1044. */
  1045. function template_preprocess_forum_topic_list(&$variables) {
  1046. global $forum_topic_list_header;
  1047. // Create the tablesorting header.
  1048. $ts = tablesort_init($forum_topic_list_header);
  1049. $header = '';
  1050. foreach ($forum_topic_list_header as $cell) {
  1051. $cell = tablesort_header($cell, $forum_topic_list_header, $ts);
  1052. $header .= _theme_table_cell($cell, TRUE);
  1053. }
  1054. $variables['header'] = $header;
  1055. if (!empty($variables['topics'])) {
  1056. $row = 0;
  1057. foreach ($variables['topics'] as $id => $topic) {
  1058. $variables['topics'][$id]->icon = array(
  1059. '#theme' => 'forum_icon',
  1060. '#new_posts' => $topic->new,
  1061. '#num_posts' => $topic->comment_count,
  1062. '#comment_mode' => $topic->comment_mode,
  1063. '#sticky' => $topic->sticky,
  1064. '#first_new' => $topic->first_new,
  1065. );
  1066. $variables['topics'][$id]->zebra = $row % 2 == 0 ? 'odd' : 'even';
  1067. $row++;
  1068. // We keep the actual tid in forum table, if it's different from the
  1069. // current tid then it means the topic appears in two forums, one of
  1070. // them is a shadow copy.
  1071. if ($variables['tid'] != $topic->forum_tid) {
  1072. $variables['topics'][$id]->moved = TRUE;
  1073. $variables['topics'][$id]->title = check_plain($topic->title);
  1074. $variables['topics'][$id]->message = l(t('This topic has been moved'), "forum/$topic->forum_tid");
  1075. }
  1076. else {
  1077. $variables['topics'][$id]->moved = FALSE;
  1078. $variables['topics'][$id]->title = l($topic->title, "node/$topic->nid");
  1079. $variables['topics'][$id]->message = '';
  1080. }
  1081. $variables['topics'][$id]->created = theme('forum_submitted', array('topic' => $topic));
  1082. $variables['topics'][$id]->last_reply = theme('forum_submitted', array('topic' => isset($topic->last_reply) ? $topic->last_reply : NULL));
  1083. $variables['topics'][$id]->new_text = '';
  1084. $variables['topics'][$id]->new_url = '';
  1085. if ($topic->new_replies) {
  1086. $variables['topics'][$id]->new_text = format_plural($topic->new_replies, '1 new post<span class="element-invisible"> in topic %title</span>', '@count new posts<span class="element-invisible"> in topic %title</span>', array('%title' => $variables['topics'][$id]->title));
  1087. $variables['topics'][$id]->new_url = url("node/$topic->nid", array('query' => comment_new_page_count($topic->comment_count, $topic->new_replies, $topic), 'fragment' => 'new'));
  1088. }
  1089. }
  1090. }
  1091. else {
  1092. // Make this safe for the template.
  1093. $variables['topics'] = array();
  1094. }
  1095. // Give meaning to $tid for themers. $tid actually stands for term id.
  1096. $variables['topic_id'] = $variables['tid'];
  1097. unset($variables['tid']);
  1098. $variables['pager'] = array(
  1099. '#theme' => 'pager',
  1100. );
  1101. }
  1102. /**
  1103. * Prepares variables for forum icon templates.
  1104. *
  1105. * Default template: forum-icon.html.twig.
  1106. *
  1107. * @param array $variables
  1108. * An array containing the following elements:
  1109. * - new_posts: Indicates whether or not the topic contains new posts.
  1110. * - num_posts: The total number of posts in all topics.
  1111. * - comment_mode: An integer indicating whether comments are open, closed,
  1112. * or hidden.
  1113. * - sticky: Indicates whether the topic is sticky.
  1114. * - first_new: Indicates whether this is the first topic with new posts.
  1115. */
  1116. function template_preprocess_forum_icon(&$variables) {
  1117. $variables['hot_threshold'] = config('forum.settings')->get('topics.hot_threshold');
  1118. if ($variables['num_posts'] > $variables['hot_threshold']) {
  1119. $icon_status_class = $variables['new_posts'] ? 'hot-new' : 'hot';
  1120. $variables['icon_title'] = $variables['new_posts'] ? t('Hot topic, new comments') : t('Hot topic');
  1121. }
  1122. else {
  1123. $icon_status_class = $variables['new_posts'] ? 'new' : 'default';
  1124. $variables['icon_title'] = $variables['new_posts'] ? t('New comments') : t('Normal topic');
  1125. }
  1126. if ($variables['comment_mode'] == COMMENT_NODE_CLOSED || $variables['comment_mode'] == COMMENT_NODE_HIDDEN) {
  1127. $icon_status_class = 'closed';
  1128. $variables['icon_title'] = t('Closed topic');
  1129. }
  1130. if ($variables['sticky'] == 1) {
  1131. $icon_status_class = 'sticky';
  1132. $variables['icon_title'] = t('Sticky topic');
  1133. }
  1134. $variables['attributes']['class'][] = 'icon';
  1135. $variables['attributes']['class'][] = 'topic-status-' . $icon_status_class;
  1136. $variables['attributes']['title'] = $variables['icon_title'];
  1137. }
  1138. /**
  1139. * Prepares variables for forum submission information templates.
  1140. *
  1141. * The submission information will be displayed in the forum list and topic
  1142. * list.
  1143. *
  1144. * Default template: forum-submitted.html.twig.
  1145. *
  1146. * @param array $variables
  1147. * An array containing the following elements:
  1148. * - topic: The topic object.
  1149. */
  1150. function template_preprocess_forum_submitted(&$variables) {
  1151. $variables['author'] = isset($variables['topic']->uid) ? theme('username', array('account' => $variables['topic'])) : '';
  1152. $variables['time'] = isset($variables['topic']->created) ? format_interval(REQUEST_TIME - $variables['topic']->created) : '';
  1153. }
  1154. /**
  1155. * Gets the last time the user viewed a node.
  1156. *
  1157. * @param $nid
  1158. * The node ID.
  1159. *
  1160. * @return
  1161. * The timestamp when the user last viewed this node, if the user has
  1162. * previously viewed the node; otherwise HISTORY_READ_LIMIT.
  1163. */
  1164. function _forum_user_last_visit($nid) {
  1165. global $user;
  1166. $history = &drupal_static(__FUNCTION__, array());
  1167. if (empty($history)) {
  1168. $result = db_query('SELECT nid, timestamp FROM {history} WHERE uid = :uid', array(':uid' => $user->uid));
  1169. foreach ($result as $t) {
  1170. $history[$t->nid] = $t->timestamp > HISTORY_READ_LIMIT ? $t->timestamp : HISTORY_READ_LIMIT;
  1171. }
  1172. }
  1173. return isset($history[$nid]) ? $history[$nid] : HISTORY_READ_LIMIT;
  1174. }
  1175. /**
  1176. * Gets topic sorting information based on an integer code.
  1177. *
  1178. * @param $sortby
  1179. * One of the following integers indicating the sort criteria:
  1180. * - 1: Date - newest first.
  1181. * - 2: Date - oldest first.
  1182. * - 3: Posts with the most comments first.
  1183. * - 4: Posts with the least comments first.
  1184. *
  1185. * @return
  1186. * An array with the following values:
  1187. * - field: A field for an SQL query.
  1188. * - sort: 'asc' or 'desc'.
  1189. */
  1190. function _forum_get_topic_order($sortby) {
  1191. switch ($sortby) {
  1192. case 1:
  1193. return array('field' => 'f.last_comment_timestamp', 'sort' => 'desc');
  1194. break;
  1195. case 2:
  1196. return array('field' => 'f.last_comment_timestamp', 'sort' => 'asc');
  1197. break;
  1198. case 3:
  1199. return array('field' => 'f.comment_count', 'sort' => 'desc');
  1200. break;
  1201. case 4:
  1202. return array('field' => 'f.comment_count', 'sort' => 'asc');
  1203. break;
  1204. }
  1205. }
  1206. /**
  1207. * Updates the taxonomy index for a given node.
  1208. *
  1209. * @param $nid
  1210. * The ID of the node to update.
  1211. */
  1212. function _forum_update_forum_index($nid) {
  1213. $count = db_query('SELECT COUNT(cid) FROM {comment} c INNER JOIN {forum_index} i ON c.nid = i.nid WHERE c.nid = :nid AND c.status = :status', array(
  1214. ':nid' => $nid,
  1215. ':status' => COMMENT_PUBLISHED,
  1216. ))->fetchField();
  1217. if ($count > 0) {
  1218. // Comments exist.
  1219. $last_reply = db_query_range('SELECT cid, name, created, uid FROM {comment} WHERE nid = :nid AND status = :status ORDER BY cid DESC', 0, 1, array(
  1220. ':nid' => $nid,
  1221. ':status' => COMMENT_PUBLISHED,
  1222. ))->fetchObject();
  1223. db_update('forum_index')
  1224. ->fields( array(
  1225. 'comment_count' => $count,
  1226. 'last_comment_timestamp' => $last_reply->created,
  1227. ))
  1228. ->condition('nid', $nid)
  1229. ->execute();
  1230. }
  1231. else {
  1232. // Comments do not exist.
  1233. // @todo This should be actually filtering on the desired node language and
  1234. // just fall back to the default language.
  1235. $node = db_query('SELECT uid, created FROM {node_field_data} WHERE nid = :nid AND default_langcode = 1', array(':nid' => $nid))->fetchObject();
  1236. db_update('forum_index')
  1237. ->fields( array(
  1238. 'comment_count' => 0,
  1239. 'last_comment_timestamp' => $node->created,
  1240. ))
  1241. ->condition('nid', $nid)
  1242. ->execute();
  1243. }
  1244. }
  1245. /**
  1246. * Implements hook_rdf_mapping().
  1247. */
  1248. function forum_rdf_mapping() {
  1249. return array(
  1250. array(
  1251. 'type' => 'node',
  1252. 'bundle' => 'forum',
  1253. 'mapping' => array(
  1254. 'rdftype' => array('sioc:Post', 'sioct:BoardPost'),
  1255. 'taxonomy_forums' => array(
  1256. 'predicates' => array('sioc:has_container'),
  1257. 'type' => 'rel',
  1258. ),
  1259. ),
  1260. ),
  1261. array(
  1262. 'type' => 'taxonomy_term',
  1263. 'bundle' => 'forums',
  1264. 'mapping' => array(
  1265. 'rdftype' => array('sioc:Container', 'sioc:Forum'),
  1266. ),
  1267. ),
  1268. );
  1269. }

Functions

Namesort descending Description
forum_block_view_pre_render Render API callback: Lists nodes based on the element's #query property.
forum_comment_delete Implements hook_comment_delete().
forum_comment_publish Implements hook_comment_publish().
forum_comment_unpublish Implements hook_comment_unpublish().
forum_comment_update Implements hook_comment_update().
forum_entity_bundle_info_alter Implements hook_entity_bundle_info_alter().
forum_field_storage_pre_insert Implements hook_field_storage_pre_insert().
forum_field_storage_pre_update Implements hook_field_storage_pre_update().
forum_form Implements hook_form().
forum_form_node_form_alter Implements hook_form_BASE_FORM_ID_alter() for node_form().
forum_form_taxonomy_term_form_alter Implements hook_form_FORM_ID_alter() for taxonomy_term_form().
forum_form_taxonomy_vocabulary_form_alter Implements hook_form_BASE_FORM_ID_alter().
forum_forum_load Returns a tree of all forums for a given taxonomy term ID.
forum_get_topics Gets all the topics in a forum.
forum_help Implements hook_help().
forum_menu Implements hook_menu().
forum_menu_local_tasks Implements hook_menu_local_tasks().
forum_menu_local_tasks_alter Implements hook_menu_local_tasks_alter().
forum_node_info Implements hook_node_info().
forum_node_insert Implements hook_node_insert().
forum_node_load Implements hook_node_load().
forum_node_predelete Implements hook_node_predelete().
forum_node_presave Implements hook_node_presave().
forum_node_update Implements hook_node_update().
forum_node_validate Implements hook_node_validate().
forum_node_view Implements hook_node_view().
forum_permission Implements hook_permission().
forum_preprocess_block Implements hook_preprocess_HOOK() for block.html.twig.
forum_rdf_mapping Implements hook_rdf_mapping().
forum_taxonomy_term_delete Implements hook_taxonomy_term_delete().
forum_theme Implements hook_theme().
forum_uri Entity URI callback used in forum_entity_info_alter().
template_preprocess_forums Prepares variables for forums templates.
template_preprocess_forum_icon Prepares variables for forum icon templates.
template_preprocess_forum_list Prepares variables for forum list templates.
template_preprocess_forum_submitted Prepares variables for forum submission information templates.
template_preprocess_forum_topic_list Prepares variables for forum topic list templates.
_forum_get_topic_order Gets topic sorting information based on an integer code.
_forum_node_check_node_type Checks whether a node can be used in a forum, based on its content type.
_forum_topics_unread Calculates the number of new posts in a forum that the user has not yet read.
_forum_update_forum_index Updates the taxonomy index for a given node.
_forum_user_last_visit Gets the last time the user viewed a node.