You are here

public function NodeViewCountController::updateCounter in Node view count 8

Ajax request handler for updating node's view count statistics.

Parameters

\Symfony\Component\HttpFoundation\Request $request: Symfony Request object. Request should have next query params:

  • nid : Id of node to update statistics for.
  • uid : Id of user which viewed the node.
  • view_mode : View mode if the node.

Return value

\Symfony\Component\HttpFoundation\Response Update operation status response.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

1 string reference to 'NodeViewCountController::updateCounter'
nodeviewcount.routing.yml in ./nodeviewcount.routing.yml
nodeviewcount.routing.yml

File

src/Controller/NodeViewCountController.php, line 82

Class

NodeViewCountController
Controller for updating nodeviewcount statistics for node.

Namespace

Drupal\nodeviewcount\Controller

Code

public function updateCounter(Request $request) {
  $data = [
    'status' => FALSE,
  ];
  $nid = $request->request
    ->filter('nid', FALSE, FILTER_VALIDATE_INT);
  $uid = $request->request
    ->filter('uid', FALSE, FILTER_VALIDATE_INT);
  $uip = $request->request
    ->filter('uip', FALSE, FILTER_VALIDATE_IP);
  $view_mode = $request
    ->get('view_mode');
  if ($nid !== FALSE && $uid !== FALSE) {

    /** @var \Drupal\node\NodeTypeInterface[] $node_types */
    $node_storage = $this->entityTypeManager
      ->getStorage('node');
    $node = $node_storage
      ->load($nid);
    $result = $this->moduleHandler
      ->invokeAll('nodeviewcount_insert', [
      $node,
      $view_mode,
    ]);
    if (!in_array(FALSE, $result, TRUE)) {
      $this->recordsManager
        ->insertRecord($uid, $nid, $uip);
      $data['status'] = TRUE;
    }
  }
  return new JsonResponse($data);
}