function _tracker_remove

Same name in other branches
  1. 7.x modules/tracker/tracker.module \_tracker_remove()
  2. 9 core/modules/tracker/tracker.module \_tracker_remove()
  3. 8.9.x core/modules/tracker/tracker.module \_tracker_remove()
  4. 10 core/modules/tracker/tracker.module \_tracker_remove()

Cleans up indexed data when nodes or comments are removed.

Parameters

int $nid: The node ID.

int $uid: The author of the node or comment.

int $changed: The last changed timestamp of the node.

2 calls to _tracker_remove()
tracker_comment_delete in core/modules/tracker/tracker.module
Implements hook_ENTITY_TYPE_delete() for comment entities.
tracker_comment_update in core/modules/tracker/tracker.module
Implements hook_ENTITY_TYPE_update() for comment entities.

File

core/modules/tracker/tracker.module, line 285

Code

function _tracker_remove($nid, $uid = NULL, $changed = NULL) {
    $node = Node::load($nid);
    $connection = \Drupal::database();
    // The user only keeps their subscription if the node exists.
    if ($node) {
        // And they are the author of the node.
        $keep_subscription = $node->getOwnerId() == $uid;
        // Or if they have commented on the node.
        if (!$keep_subscription) {
            // Check if the user has commented at least once on the given nid.
            $keep_subscription = \Drupal::entityQuery('comment')->accessCheck(FALSE)
                ->condition('entity_type', 'node')
                ->condition('entity_id', $nid)
                ->condition('uid', $uid)
                ->condition('status', CommentInterface::PUBLISHED)
                ->range(0, 1)
                ->count()
                ->execute();
        }
        // If we haven't found a reason to keep the user's subscription, delete it.
        if (!$keep_subscription) {
            $connection->delete('tracker_user')
                ->condition('nid', $nid)
                ->condition('uid', $uid)
                ->execute();
        }
        // Now we need to update the (possibly) changed timestamps for other users
        // and the node itself.
        // We only need to do this if the removed item has a timestamp that equals
        // or exceeds the listed changed timestamp for the node.
        $tracker_node = $connection->query('SELECT [nid], [changed] FROM {tracker_node} WHERE [nid] = :nid', [
            ':nid' => $nid,
        ])
            ->fetchObject();
        if ($tracker_node && $changed >= $tracker_node->changed) {
            // If we're here, the item being removed is *possibly* the item that
            // established the node's changed timestamp.
            // We just have to recalculate things from scratch.
            $changed = _tracker_calculate_changed($node);
            // And then we push the out the new changed timestamp to our denormalized
            // tables.
            $connection->update('tracker_node')
                ->fields([
                'changed' => $changed,
                'published' => $node->isPublished(),
            ])
                ->condition('nid', $nid)
                ->execute();
            $connection->update('tracker_node')
                ->fields([
                'changed' => $changed,
                'published' => $node->isPublished(),
            ])
                ->condition('nid', $nid)
                ->execute();
        }
    }
    else {
        // If the node doesn't exist, remove everything.
        $connection->delete('tracker_node')
            ->condition('nid', $nid)
            ->execute();
        $connection->delete('tracker_user')
            ->condition('nid', $nid)
            ->execute();
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.