You are here

function nodeviewcount_node_view in Node view count 8

Same name and namespace in other branches
  1. 7.3 nodeviewcount.module \nodeviewcount_node_view()
  2. 7 nodeviewcount.module \nodeviewcount_node_view()
  3. 7.2 nodeviewcount.module \nodeviewcount_node_view()

Implements hook_ENTITY_TYPE_view().

File

./nodeviewcount.module, line 15
Primarily Drupal hooks for nodeviewcount module.

Code

function nodeviewcount_node_view(array &$build, NodeInterface $node, EntityViewDisplayInterface $display, $view_mode) {

  /** @var \Drupal\nodeviewcount\NodeViewCountRecordsManager $records_manager */
  $records_manager = Drupal::service('nodeviewcount.records_manager');

  /** @var \Drupal\user\UserInterface $user */
  $user = Drupal::currentUser();
  $uip = Drupal::request()
    ->getClientIp();
  if ($records_manager
    ->isRecordableForNodeType($node) && $records_manager
    ->isRecordableForUserRole($user) && $records_manager
    ->isRecordableForViewMode($view_mode)) {
    $build['#attached']['library'][] = 'nodeviewcount/nodeviewcount.base';
    $build['#attached']['drupalSettings']['nodeviewcount']['url'] = Url::fromRoute('nodeviewcount.update_counter')
      ->toString();
    $settings = [
      'nid' => $node
        ->id(),
      'uid' => $user
        ->id(),
      'uip' => $uip,
      'view_mode' => $view_mode,
    ];
    $build['#attached']['drupalSettings']['nodeviewcount']['data'][$node
      ->id()] = $settings;
  }
}