You are here

function node_read_time_node_view in Node read time 8

Implements hook_ENTITY_TYPE_view().

File

./node_read_time.module, line 61
Reading time module.

Code

function node_read_time_node_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
  if ($display
    ->getComponent('reading_time')) {
    $config = \Drupal::config('node_read_time.settings')
      ->get('reading_time')['container'];
    if ($config[$entity
      ->getType()]['is_activated']) {

      // If words per minute is not set, give an average of 225.
      $words_per_minute = \Drupal::config('node_read_time.settings')
        ->get('reading_time')['words_per_minute'] ?: 225;
      $reading_time_service = \Drupal::service('reading_time');
      $reading_time = $reading_time_service
        ->setWordsPerMinute($words_per_minute)
        ->collectWords($entity)
        ->calculateReadingTime()
        ->getReadingTime();

      // Clear the words variable.
      $reading_time_service
        ->setWords(0);
      $build['reading_time'] = [
        '#theme' => 'reading_time',
        '#reading_time' => $reading_time,
      ];
    }
  }
}