Commit f099e8c8 authored by alexpott's avatar alexpott

Issue #2201659 by ocsilalala, larowlan | andypost: Fix serialization of the...

Issue #2201659 by ocsilalala, larowlan | andypost: Fix serialization of the forum manager and dependency on current_user service.
parent 01a3e190
......@@ -82,7 +82,7 @@ public function forumPage(TermInterface $taxonomy_term) {
$taxonomy_term->parents = $this->forumManager->getParents($taxonomy_term->id());
if (empty($taxonomy_term->forum_container->value)) {
$build = $this->forumManager->getTopics($taxonomy_term->id());
$build = $this->forumManager->getTopics($taxonomy_term->id(), $this->currentUser());
$topics = $build['topics'];
$header = $build['header'];
}
......
......@@ -9,7 +9,9 @@
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\DependencyInjection\DependencySerialization;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\comment\CommentInterface;
use Drupal\field\FieldInfo;
......@@ -18,7 +20,7 @@
/**
* Provides forum manager service.
*/
class ForumManager implements ForumManagerInterface {
class ForumManager extends DependencySerialization implements ForumManagerInterface {
/**
* Forum sort order, newest first.
......@@ -135,13 +137,11 @@ public function __construct(ConfigFactoryInterface $config_factory, EntityManage
/**
* {@inheritdoc}
*/
public function getTopics($tid) {
public function getTopics($tid, AccountInterface $account) {
$config = $this->configFactory->get('forum.settings');
$forum_per_page = $config->get('topics.page_limit');
$sortby = $config->get('topics.order');
$user = \Drupal::currentUser();
$header = array(
array('data' => $this->t('Topic'), 'field' => 'f.title'),
array('data' => $this->t('Replies'), 'field' => 'f.comment_count'),
......@@ -230,14 +230,14 @@ public function getTopics($tid) {
$topics = array();
$first_new_found = FALSE;
foreach ($result as $topic) {
if ($user->isAuthenticated()) {
if ($account->isAuthenticated()) {
// A forum is new if the topic is new, or if there are new comments since
// the user's last visit.
if ($topic->forum_tid != $tid) {
$topic->new = 0;
}
else {
$history = $this->lastVisit($topic->id());
$history = $this->lastVisit($topic->id(), $account);
$topic->new_replies = $this->numberNew($topic->id(), $history);
$topic->new = $topic->new_replies || ($topic->last_comment_timestamp > $history);
}
......@@ -321,18 +321,18 @@ protected function numberNew($nid, $timestamp) {
*
* @param int $nid
* The node ID.
* @param \Drupal\Core\Session\AccountInterface $account
* Account to fetch last time for.
*
* @return int
* The timestamp when the user last viewed this node, if the user has
* previously viewed the node; otherwise HISTORY_READ_LIMIT.
*/
protected function lastVisit($nid) {
$user = \Drupal::currentUser();
protected function lastVisit($nid, AccountInterface $account) {
if (empty($this->history[$nid])) {
$result = $this->connection->select('history', 'h')
->fields('h', array('nid', 'timestamp'))
->condition('uid', $user->id())
->condition('uid', $account->id())
->execute();
foreach ($result as $t) {
$this->history[$t->nid] = $t->timestamp > HISTORY_READ_LIMIT ? $t->timestamp : HISTORY_READ_LIMIT;
......@@ -473,7 +473,7 @@ public function resetCache() {
// Reset the index.
$this->index = NULL;
// Reset history.
$this->history = NULL;
$this->history = array();
}
/**
......@@ -559,4 +559,27 @@ protected function t($string, array $args = array(), array $options = array()) {
return $this->translationManager->translate($string, $args, $options);
}
/**
* {@inheritdoc}
*/
public function __sleep() {
$vars = parent::__sleep();
// Do not serialize static cache.
unset($vars['history'], $vars['index'], $vars['lastPostData'], $vars['forumChildren'], $vars['forumStatistics']);
return $vars;
}
/**
* {@inheritdoc}
*/
public function __wakeup() {
parent::__wakeup();
// Initialize static cache.
$this->history = array();
$this->lastPostData = array();
$this->forumChildren = array();
$this->forumStatistics = array();
$this->index = NULL;
}
}
......@@ -7,6 +7,7 @@
namespace Drupal\forum;
use Drupal\Core\Session\AccountInterface;
use Drupal\node\NodeInterface;
/**
......@@ -19,11 +20,13 @@ interface ForumManagerInterface {
*
* @param int $tid
* Term ID.
* @param \Drupal\Core\Session\AccountInterface $account
* Account to fetch topics for.
*
* @return array
* Array with keys 'topics' and 'header'.
*/
public function getTopics($tid);
public function getTopics($tid, AccountInterface $account);
/**
* Utility method to fetch the child forums for a given forum.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment