reading_time_estimator
Overview
The Reading Time Estimator module calculates and displays the estimated reading time for content based on configurable text fields. It automatically analyzes the word count from selected fields and displays a user-friendly reading time estimate on node pages.
Features
- Configurable Reading Speed: Set custom words-per-minute reading speed (default: 200 WPM)
- Content Type Support: Configure which text fields to include for each content type
- Automatic Calculation: Automatically calculates reading time based on selected fields
- Display Management: Reading time can be enabled/disabled per content type via the display settings
- Template Support: Customizable Twig template for displaying reading time
- Cache Integration: Properly integrated with Drupal's cache system
Installation
- Place the module in
/web/modules/custom_modules/reading_time_estimator/ -
Enable the module via Drush:
drush en reading_time_estimatorOr via the admin interface at
/admin/modules -
Clear Drupal cache:
drush cr
Configuration
Basic Configuration
-
Navigate to Configuration → Reading Time Estimator Settings (
/admin/config/content/reading-time-estimator) -
Set Reading Speed:
- Configure the average reading speed in words per minute
- Default: 200 words per minute
- Range: 50-500 WPM
-
Configure Content Types:
- For each content type, select which text fields should be included in the reading time calculation
- Only text fields (text_long, text_with_summary, string_long, text) are available for selection
- You can select multiple fields per content type
Display Settings
-
Navigate to Structure → Content types → [Your Content Type] → Manage display
-
Find the Reading Time Estimate component in the disabled section
-
Enable and configure the component:
- Drag it to the desired position
- Configure visibility settings if needed
- Save the configuration
How It Works
- Field Selection: The module analyzes text from the fields you've configured for each content type
- Word Counting: It strips HTML tags, decodes entities, and counts words in the selected fields
- Calculation: Reading time = Total words ÷ Reading speed (minimum 1 minute)
- Display: The reading time is displayed using the
reading-time-estimate.html.twigtemplate
Usage
Display Reading Time
Once configured, the reading time estimate will automatically appear on node pages where:
- The content type has fields configured in the module settings
- The "Reading Time Estimate" component is enabled in the display settings
- The node has content in the selected fields
Customizing the Display
The reading time is rendered using the reading-time-estimate.html.twig template located at:
templates/reading-time-estimate.html.twig
You can override this template in your theme to customize the appearance.
Available Variables:
reading_time: The estimated reading time in minutes (integer)
Example Override:
{# In your theme's templates/reading-time-estimate.html.twig #} <div class="custom-reading-time"> <i class="icon-clock"></i> <span>{{ reading_time }} min read</span> </div>
Styling
The module includes basic CSS in css/reading-time-estimate.css. You can override styles in your theme:
.reading-time-estimate { /* Your custom styles */ }
Programmatic Usage
Calculate Reading Time Programmatically
use Drupal\reading_time_estimator\Service\ReadingTimeCalculator; // Get the service $calculator = \Drupal::service('reading_time_estimator.calculator'); // Calculate reading time for a node $node = Node::load(123); $reading_time = $calculator->calculateReadingTime($node); // $reading_time will be an integer representing minutes
Get Available Text Fields
$calculator = \Drupal::service('reading_time_estimator.calculator'); $available_fields = $calculator->getAvailableTextFields('article'); // Returns array of field names and labels // ['body' => 'Body', 'field_summary' => 'Summary']
Technical Details
Hooks Implemented
hook_entity_extra_field_info(): Adds "Reading Time Estimate" as an extra field for all node typeshook_ENTITY_TYPE_view(): Calculates and displays reading time when the component is enabledhook_theme(): Registers the reading time estimate template
Services
reading_time_estimator.calculator: Main service for calculating reading time- Class:
Drupal\reading_time_estimator\Service\ReadingTimeCalculator - Dependencies:
config.factory,entity_field.manager
- Class:
Configuration Schema
The module stores configuration in reading_time_estimator.settings:
reading_speed: Integer (words per minute)content_types.{bundle}.fields: Array of field names to include
Requirements
- Drupal 9, 10, or 11
- PHP 7.4 or higher
- Dependencies:
drupal:fielddrupal:node
Troubleshooting
Reading Time Not Displaying
-
Check Configuration:
- Ensure fields are selected for the content type in module settings
- Verify the content has text in the selected fields
-
Check Display Settings:
- Navigate to the content type's display settings
- Ensure "Reading Time Estimate" component is enabled and visible
-
Clear Cache:
drush cr
Reading Time Shows 0 or Incorrect
- Verify Field Selection: Make sure the correct fields are selected in configuration
- Check Field Content: Ensure the selected fields contain actual text content
- Review Reading Speed: Adjust the reading speed setting if estimates seem off
Development
File Structure
reading_time_estimator/ ├── css/ │ └── reading-time-estimate.css ├── src/ │ ├── Form/ │ │ └── ReadingTimeEstimatorConfigForm.php │ └── Service/ │ └── ReadingTimeCalculator.php ├── templates/ │ └── reading-time-estimate.html.twig ├── reading_time_estimator.info.yml ├── reading_time_estimator.libraries.yml ├── reading_time_estimator.links.menu.yml ├── reading_time_estimator.module ├── reading_time_estimator.routing.yml ├── reading_time_estimator.services.yml └── README.md
Extending the Module
To add custom field types for reading time calculation, modify the getAvailableTextFields() method in ReadingTimeCalculator.php:
// Add custom field types if (in_array($field_type, ['text_long', 'text_with_summary', 'string_long', 'text', 'your_custom_type'])) { $fields[$field_name] = $field_definition->getLabel(); }
Support
For issues or feature requests, please contact the development team or create an issue in the project repository.
License
This module is part of the TTN Drupal project.