history_read example

/** * #lazy_builder callback; attaches the last read timestamp for a node. * * @param int $node_id * The node ID for which to attach the last read timestamp. * * @return array * A renderable array containing the last read timestamp. */
  public static function lazyBuilder($node_id) {
    $element = [];
    $element['#attached']['drupalSettings']['history']['lastReadTimestamps'][$node_id] = (int) history_read($node_id);
    return $element;
  }

}

  public function readNode(Request $request, NodeInterface $node) {
    if ($this->currentUser()->isAnonymous()) {
      throw new AccessDeniedHttpException();
    }

    // Update the history table, stating that this user viewed this node.     history_write($node->id());

    return new JsonResponse((int) history_read($node->id()));
  }

}
/** * {@inheritdoc} */
  public function getCountNewComments(EntityInterface $entity$field_name = NULL, $timestamp = 0) {
    // @todo Replace module handler with optional history service injection     // after https://www.drupal.org/node/2081585.     if ($this->currentUser->isAuthenticated() && $this->moduleHandler->moduleExists('history')) {
      // Retrieve the timestamp at which the current user last viewed this entity.       if (!$timestamp) {
        if ($entity->getEntityTypeId() == 'node') {
          $timestamp = history_read($entity->id());
        }
        else {
          $function = $entity->getEntityTypeId() . '_last_viewed';
          if (function_exists($function)) {
            $timestamp = $function($entity->id());
          }
          else {
            // Default to 30 days ago.             // @todo Remove this else branch when we have a generic             // HistoryRepository service in https://www.drupal.org/node/3267011.             $timestamp = COMMENT_NEW_LIMIT;
          }
if (!empty($links)) {
        $entity_links['comment__' . $field_name] = [
          '#theme' => 'links__entity__comment__' . $field_name,
          '#links' => $links,
          '#attributes' => ['class' => ['links', 'inline']],
        ];
        if ($view_mode == 'teaser' && $this->moduleHandler->moduleExists('history') && $this->currentUser->isAuthenticated()) {
          $entity_links['comment__' . $field_name]['#cache']['contexts'][] = 'user';
          $entity_links['comment__' . $field_name]['#attached']['library'][] = 'comment/drupal.node-new-comments-link';
          // Embed the metadata for the "X new comments" link (if any) on this           // entity.           $entity_links['comment__' . $field_name]['#attached']['drupalSettings']['history']['lastReadTimestamps'][$entity->id()] = (int) history_read($entity->id());
          $new_comments = $this->commentManager->getCountNewComments($entity);
          if ($new_comments > 0) {
            $page_number = $this->entityTypeManager
              ->getStorage('comment')
              ->getNewCommentPageNumber($entity->{$field_name}->comment_count, $new_comments$entity$field_name);
            $query = $page_number ? ['page' => $page_number] : NULL;
            $value = [
              'new_comment_count' => (int) $new_comments,
              'first_new_comment_link' => $entity->toUrl('canonical', [
                'query' => $query,
                'fragment' => 'new',
              ])
Home | Imprint | This part of the site doesn't use cookies.