You are here

public function NodeReadTime::render in Node read time 8

Render function for the node_read_time field.

Displays the node read time.

inheritdoc}

Overrides FieldPluginBase::render

File

src/Plugin/views/field/NodeReadTime.php, line 107

Class

NodeReadTime
Field handler to node read time.

Namespace

Drupal\node_read_time\Plugin\views\field

Code

public function render(ResultRow $values) {
  $node = $this
    ->getEntity($values);
  $config = $this->configManager
    ->get('node_read_time.settings');
  $bundles = $config
    ->get('reading_time')['container'];
  if ($bundles) {
    foreach ($bundles as $machine_name => $bundle) {
      if ($bundle['is_activated'] && $machine_name === $node
        ->getType()) {

        // If words per minute is not set, give an average of 225.
        $words_per_minute = $this->configManager
          ->get('node_read_time.settings')
          ->get('reading_time')['words_per_minute'] ?: 225;
        $reading_time = $this->readingTime
          ->setWordsPerMinute($words_per_minute)
          ->collectWords($node)
          ->calculateReadingTime()
          ->getReadingTime();

        // Clear the words variable.
        $this->readingTime
          ->setWords(0);
        return [
          '#theme' => 'reading_time',
          '#reading_time' => $reading_time,
        ];
      }
    }
  }
  return $this->options['message_not_enable'];
}