Skip to content
Snippets Groups Projects

#3455450: Rendered the NodeCharactersReadTime and view fields

4 files
+ 183
4
Compare changes
  • Side-by-side
  • Inline
Files
4
+ 37
0
<?php
namespace Drupal\node_read_time\Plugin\Field;
use Drupal\Core\Field\FieldItemList;
use Drupal\Core\TypedData\ComputedItemListTrait;
/**
* Calculate the reading time with option "characters per minute" for the entity.
*
* @package Drupal\node_read_time\Plugin\Field
*/
class NodeCharactersReadTime extends FieldItemList {
use ComputedItemListTrait;
/**
* Computes the field value.
*/
protected function computeValue(): void {
$entity = $this->getEntity();
$config = \Drupal::config('node_read_time.settings')
->get('characters_reading_time')['container'];
$reading_time = NULL;
$entity_type = $entity->getType();
if (isset($config[$entity_type]) && $config[$entity_type]['is_activated']) {
$reading_time_service = \Drupal::service('node_read_time.reading_time');
$reading_time = $reading_time_service
->collectWords($entity)
->calculateReadingTime('characters')
->getReadingTime();
// Clear the words variable.
$reading_time_service->setWords(0);
}
$this->list[0] = $this->createItem(0, $reading_time);
}
}
Loading