diff --git a/core/modules/history/history.module b/core/modules/history/history.module index 8d552ef..a03d3ac 100644 --- a/core/modules/history/history.module +++ b/core/modules/history/history.module @@ -175,6 +175,7 @@ function history_library_info() { * @param array $context * An array with the following keys: * - entity_id: the entity ID for which to attach the last read timestamp. + * - entity_type: the type ID of the entity. * * @return array $element * The updated $element. diff --git a/core/modules/history/history.services.yml b/core/modules/history/history.services.yml index 64fcc21..a466b56 100644 --- a/core/modules/history/history.services.yml +++ b/core/modules/history/history.services.yml @@ -1,4 +1,4 @@ services: history.repository: class: Drupal\history\HistoryRepository - arguments: ['@database', '@current_user'] + arguments: ['@database'] diff --git a/core/modules/history/lib/Drupal/history/HistoryRepository.php b/core/modules/history/lib/Drupal/history/HistoryRepository.php index e91ba67..d81c43e 100644 --- a/core/modules/history/lib/Drupal/history/HistoryRepository.php +++ b/core/modules/history/lib/Drupal/history/HistoryRepository.php @@ -8,13 +8,14 @@ namespace Drupal\history; use Drupal\Core\Database\Connection; +use Drupal\Core\DependencyInjection\DependencySerialization; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Session\AccountInterface; /** * Provides history repository service. */ -class HistoryRepository implements HistoryRepositoryInterface { +class HistoryRepository extends DependencySerialization implements HistoryRepositoryInterface { /** * Array of history keyed by entity type and entity id. @@ -31,23 +32,13 @@ class HistoryRepository implements HistoryRepositoryInterface { protected $connection; /** - * The current user. - * - * @var \Drupal\Core\Session\AccountInterface - */ - protected $currentUser; - - /** * Constructs the history repository service. * * @param \Drupal\Core\Database\Connection $connection * The database connection. - * @param \Drupal\Core\Session\AccountInterface $current_user - * The current user. */ - public function __construct(Connection $connection, AccountInterface $current_user) { + public function __construct(Connection $connection) { $this->connection = $connection; - $this->currentUser = $current_user; } /** @@ -58,8 +49,8 @@ public function getLastViewed($entity_type, $entity_ids, AccountInterface $accou $entities_to_read = array(); foreach ($entity_ids as $entity_id) { - if (isset($this->history[$entity_type][$entity_id])) { - $return[$entity_id] = $this->history[$entity_type][$entity_id]; + if (isset($this->history[$entity_type][$account->id()][$entity_id])) { + $return[$entity_id] = $this->history[$entity_type][$account->id()][$entity_id]; } else { $entities_to_read[$entity_id] = 0; @@ -80,10 +71,10 @@ public function getLastViewed($entity_type, $entity_ids, AccountInterface $accou foreach ($result as $row) { $entities_to_read[$row->entity_id] = (int) $row->timestamp; } - if (!isset($this->history[$entity_type])) { - $this->history[$entity_type] = array(); + if (!isset($this->history[$entity_type][$account->id()])) { + $this->history[$entity_type][$account->id()] = array(); } - $this->history[$entity_type] += $entities_to_read; + $this->history[$entity_type][$account->id()] += $entities_to_read; return $return + $entities_to_read; } @@ -102,7 +93,7 @@ public function updateLastViewed(EntityInterface $entity, AccountInterface $acco ->fields(array('timestamp' => REQUEST_TIME)) ->execute(); // Update cached value. - $this->history[$entity->getEntityTypeId()][$entity->id()] = REQUEST_TIME; + $this->history[$entity->getEntityTypeId()][$account->id()][$entity->id()] = REQUEST_TIME; } } @@ -137,7 +128,7 @@ public function deleteByEntity(EntityInterface $entity) { ->condition('entity_type', $entity->getEntityTypeId()) ->execute(); // Clean static cache. - unset($this->history[$entity->getEntityTypeId()][$entity->id()]); + $this->resetCache(); } /** @@ -147,4 +138,23 @@ public function resetCache() { $this->history = array(); } + /** + * {@inheritdoc} + */ + public function __sleep() { + $vars = parent::__sleep(); + // Do not serialize static cache. + unset($vars['history']); + return $vars; + } + + /** + * {@inheritdoc} + */ + public function __wakeup() { + parent::__wakeup(); + // Initialize static cache. + $this->history = array(); + } + }