Skip to content
Snippets Groups Projects

#3455439 - Added a container for services used in Field/NodeReadTime

1 file
+ 73
5
Compare changes
  • Side-by-side
  • Inline
@@ -2,8 +2,13 @@
namespace Drupal\node_read_time\Plugin\Field;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Field\FieldItemList;
use Drupal\Core\TypedData\ComputedItemListTrait;
use Drupal\Core\TypedData\DataDefinitionInterface;
use Drupal\Core\TypedData\TraversableTypedDataInterface;
use Drupal\Core\TypedData\TypedDataInterface;
use Drupal\node_read_time\Calculate\ReadingTime;
/**
* Calculate the reading time for the entity.
@@ -13,13 +18,75 @@ use Drupal\Core\TypedData\ComputedItemListTrait;
class NodeReadTime extends FieldItemList {
use ComputedItemListTrait;
/**
* The Reading Time service.
*
* @var \Drupal\node_read_time\Calculate\ReadingTime
*/
protected ReadingTime $readingTime;
/**
* Configuration Factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected ConfigFactoryInterface $configFactory;
/**
* Constructs a NodeReadTime object given its definition and context.
*
* @param \Drupal\Core\TypedData\DataDefinitionInterface $definition
* The data definition.
* @param string $name
* (optional) The name of the created property, or NULL if it is the root
* of a typed data tree. Defaults to NULL.
* @param \Drupal\Core\TypedData\TypedDataInterface $parent
* (optional) The parent object of the data property, or NULL if it is the
* root of a typed data tree. Defaults to NULL.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* Configuration Factory.
* @param \Drupal\node_read_time\Calculate\ReadingTime $reading_time
* The Reading Time service.
*
* @see \Drupal\Core\TypedData\TypedDataManager::create()
*/
public function __construct(
DataDefinitionInterface $definition,
$name = NULL,
TypedDataInterface $parent = NULL,
ConfigFactoryInterface $config_factory,
ReadingTime $reading_time,
) {
parent::__construct($definition, $name, $parent);
$this->configFactory = $config_factory;
$this->readingTime = $reading_time;
}
/**
* {@inheritDoc}
*/
public static function createInstance(
$definition,
$name = NULL,
TraversableTypedDataInterface $parent = NULL,
): static {
$container = \Drupal::getContainer();
return new static(
$definition,
$name,
$parent,
$container->get('config.factory'),
$container->get('node_read_time.reading_time'),
);
}
/**
* Computes the field value.
*/
protected function computeValue() {
$entity = $this->getEntity();
$config = \Drupal::config('node_read_time.settings')
$config = $this->configFactory->get('node_read_time.settings')
->get('reading_time')['container'];
$reading_time = NULL;
@@ -27,16 +94,17 @@ class NodeReadTime extends FieldItemList {
if (isset($config[$entity_type]) && $config[$entity_type]['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('node_read_time.reading_time');
$reading_time = $reading_time_service
$words_per_minute = $this->configFactory
->get('node_read_time.settings')
->get('reading_time')['words_per_minute'] ?: 225;
$reading_time = $this->readingTime
->setWordsPerMinute($words_per_minute)
->collectWords($entity)
->calculateReadingTime()
->getReadingTime();
// Clear the words variable.
$reading_time_service->setWords(0);
$this->readingTime->setWords(0);
}
$this->list[0] = $this->createItem(0, $reading_time);
}
Loading