Skip to main content

Implementing an Estimated "Read Time" on Articles

Bill Renk | Software Architect

May 1, 2013


Recently, a client asked that we display an “estimated reading time” (which we’re just going to call “read time” from now on) for their articles. This is a usability feature that is ideally designed to keep the user on the page longer by letting them know about how long it will take them to read the article. For implementation in Drupal, we decided to use a custom field. The calculation for the read time itself comes from Brian Cray’s blog post on the matter, where you may also find more background information on the subject overall. The code below shows the field, widget and formatter definitions for the custom field using their respective hooks.  

// Implements hook_field_info().
function read_time_field_field_info() {
  return array(
    'read_time_field' => array(
      'label' => t('Read Time'),
      'description' => t('Time it takes to read article'),
      'default_widget' => 'read_time_field_widget',
      'default_formatter' => 'read_time_field_formatter',
    ),
  );
}
// Implements hook_field_formatter_info().
function read_time_field_field_formatter_info() {
  return array(
    'read_time_field_formatter' => array(
      'label' => t('Read Time Text'),
      'field types' => array('read_time_field'),
      'settings' => array('read_time_fields' =>
        array('body' => 'body'),
        'format_text' => '@m m @s sec',
        'word_per_minute' => '200'
      ),
    ),
  );
}
// Implements hook_field_widget_info().
function read_time_field_field_widget_info() {
  return array(
    'read_time_field_widget' => array(
      'label' => t('Read Time'),
      'field types' => array('read_time_field'),
    ),
  );
}

The formatter settings provide flexibility for the field display and the read time calculation:

/**
 * Implements hook_field_formatter_settings_form().
 *
 * Allows for customization of the read time calculation.
 */
function read_time_field_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];
  $fields = field_info_instances($instance['entity_type'], $instance['bundle']);
  $options = array('title' => t('title'));
  foreach($fields as $key => $field) {
    if(in_array($field['widget']['type'], array('text_textarea_with_summary', 'text_textarea', 'text_long', 'text_with_summary'))) {
      $options[$key] = $field['label'];
    }
  }
  $element['read_time_fields'] = array(
    '#type' => 'checkboxes',
    '#options' => $options,
    '#default_value' => $settings['read_time_fields'],
    '#title' => t('Fields used to calculate read time'),
  );
  $element['format_text'] = array(
    '#title' => t('Choose how users view dates and times:'),
    '#type' => 'textfield',
    '#size' => 10,
    '#default_value' => $settings['format_text'],
    '#description' => t('Enter a string with @m and @s as replacement values. For example: @m minutes @s seconds'),
    '#weight' => 0,
  );
  $element['word_per_minute'] = array(
    '#title' => t('Words Per Minute'),
    '#type' => 'textfield',
    '#size' => 10,
    '#default_value' => $settings['word_per_minute'],
    '#weight' => 1,
  );
  return $element;
}

     Looking at the above code, the first setting allows for selecting which text fields are to be used in the calculation of the read time. This could be useful if your article involves more than just the body field. The ‘format_text’ setting allows for specification of the display format. For instance, enter “@m mins @s sec” and the display will be produce “11 mins 20 secs.” The final setting allows you to tweak the words per minute reading rate. Maybe the site audience is very bright and their rate is higher. The custom read time field is currently available as a sandbox module that can be downloaded here:http://drupal.org/sandbox/brenk28/1865774.


Recommended Next
Development
A Developer's Guide For Contributing To Drupal
Black pixels on a grey background
Development
3 Steps to a Smooth Salesforce Integration
Black pixels on a grey background
Development
Drupal 8 End of Life: What You Need To Know
Woman working on a laptop
Jump back to top