diff --git a/estimated_read_time.module b/estimated_read_time.module index ad8e5723e9a3b7d4afa0a38fa357371a458ecdbe..60c002d091da01986780942aa34ab8b237ffbbca 100644 --- a/estimated_read_time.module +++ b/estimated_read_time.module @@ -29,10 +29,38 @@ function estimated_read_time_theme($existing, $type, $theme, $path) { * Implements hook_entity_presave(). */ function estimated_read_time_entity_presave(EntityInterface $entity) { + // Set the read time on an entity update. if (!$entity instanceof FieldableEntityInterface) { return; } + if ($entity->isNew()) { + return; + } + + estimated_read_time_set_estimated_read_time($entity); +} + +/** + * Implements hook_entity_insert(). + */ +function estimated_read_time_entity_insert(EntityInterface $entity) { + // Set the read time for new entities. + if (!$entity instanceof FieldableEntityInterface) { + return; + } + + estimated_read_time_set_estimated_read_time($entity); + $entity->save(); +} + +/** + * Sets the read time on an entity. + * + * @param \Drupal\Core\Entity\EntityInterface $entity + * The entity being acted on. + */ +function estimated_read_time_set_estimated_read_time(EntityInterface $entity) { $fieldDefinitions = $entity->getFieldDefinitions(); foreach ($fieldDefinitions as $field) {