Skip to content

Commit

Permalink
Merge pull request #5 from lukeyouell/develop
Browse files Browse the repository at this point in the history
readTime function
  • Loading branch information
lukeyouell authored Jun 18, 2018
2 parents feebe29 + a0d5f98 commit 18d1b1f
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

All notable changes to this project will be documented in this file.

## 1.2.0 - 2018-06-18

### Added
- Calculate the read time of the whole entry based on it's field layout

## 1.1.0 - 2018-06-06

### Changed
Expand Down
24 changes: 21 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ The average user read speed is set at 200 words per minute by default, this can

## Using the Filter

The `|readTime` filter returns a human time duration of how long it takes the average user to read the provided content. The value provided can be a string or an array of values.
The `|readTime` filter returns the human time duration of how long it takes the average user to read the provided content. The value provided can be a string or an array of values.

Seconds are included by default, but can be disabled by using `|readTime(false)`

Expand All @@ -60,6 +60,26 @@ Returns: 2 minutes, 40 seconds
Returns: 3 minutes
```

## Using the Function

The `readTime()` function returns the average read time for the whole entry based on it's field layout.

Seconds are included by default, but can be disabled by adding a second parameter of `false`

#### Examples

```twig
{{ readTime(entry) }}
Returns: 9 minutes, 40 seconds
```

```twig
{{ readTime(entry, false) }}
Returns: 10 minutes
```

## Overriding Plugin Settings

If you create a [config file](https://docs.craftcms.com/v3/configuration.html) in your `config` folder called `read-time.php`, you can override the plugin’s settings in the Control Panel. Since that config file is fully [multi-environment](https://docs.craftcms.com/v3/configuration.html) aware, this is a handy way to have different settings across multiple environments.
Expand All @@ -78,6 +98,4 @@ return [

Some things to do, and ideas for potential features:

- Twig extension that calculates the read time for all of the fields that exist within a given entry `{{ readTime(entry) }}` for example

Brought to you by [Luke Youell](https://github.com/lukeyouell)
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "lukeyouell/craft-readtime",
"description": "Calculate the estimated read time for content.",
"type": "craft-plugin",
"version": "1.1.0",
"version": "1.2.0",
"keywords": [
"craft",
"cms",
Expand Down
38 changes: 36 additions & 2 deletions src/twigextensions/ReadTimeTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use Craft;
use craft\helpers\DateTimeHelper;

use yii\base\ErrorException;

class ReadTimeTwigExtension extends \Twig_Extension
{
// Public Methods
Expand All @@ -25,14 +27,46 @@ public function getName()
return 'readTime';
}

public function getFunctions()
{
return [
new \Twig_SimpleFunction('readTime', [$this, 'readTimeFunction']),
];
}

public function getFilters()
{
return [
new \Twig_SimpleFilter('readTime', [$this, 'readTime']),
new \Twig_SimpleFilter('readTime', [$this, 'readTimeFilter']),
];
}

public function readTime($value = null, $showSeconds = true)
public function readTimeFunction($element, $showSeconds = true)
{
$settings = ReadTime::$plugin->getSettings();
$wpm = $settings->wordsPerMinute;
$totalSeconds = 0;

foreach ($element->getFieldLayout()->getFields() as $field) {
try {
$fieldVal = $element->getFieldValue($field->handle);

$value = is_array($fieldVal) ? implode(' ', $fieldVal) : (string)$fieldVal;
$words = str_word_count(strip_tags($value));
$seconds = floor($words / $wpm * 60);

$totalSeconds = $totalSeconds + $seconds;
} catch (ErrorException $e) {
continue;
}
}

$duration = DateTimeHelper::secondsToHumanTimeDuration($seconds, $showSeconds);

return $duration;
}

public function readTimeFilter($value = null, $showSeconds = true)
{
$settings = ReadTime::$plugin->getSettings();

Expand Down

0 comments on commit 18d1b1f

Please sign in to comment.