You are here

function recently_read_entity_view in Recently Read 8

Same name and namespace in other branches
  1. 7.3 recently_read.module \recently_read_entity_view()

Implements hook_entity_view().

File

./recently_read.module, line 15
The module will track user activities on the website.

Code

function recently_read_entity_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
  $recentlyRead = \Drupal::service('recently_read');

  // Get recently read config for current entity.
  $readTypeConfig = RecentlyReadType::load($entity
    ->getEntityTypeId());
  if ($readTypeConfig && !$entity->in_preview && $view_mode === "full") {

    // Disable cache.
    $renderer = \Drupal::service('renderer');
    $renderer
      ->addCacheableDependency($build, $entity
      ->id());
    $allowedTypes = $readTypeConfig
      ->getTypes();

    // Check entity type and act properly based on entityType.
    switch ($allowedTypes) {
      case TRUE:
        if (in_array($entity
          ->bundle(), $allowedTypes)) {
          $recentlyRead
            ->insertEntity($entity);
        }
        break;
      case FALSE:
        $recentlyRead
          ->insertEntity($entity);
        break;
    }
  }
}