Skip to content

Commit

Permalink
Merge pull request #13 from lukeyouell/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
lukeyouell authored Feb 21, 2019
2 parents 3ae4b36 + c4e1766 commit 7e8e43e
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 43 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.5.0 - 2019-02-21

### Added
- Matrix field support

## 1.4.0 - 2018-07-31

### Added
Expand Down
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,18 @@ Returns: 3 minutes

## Using the Function

The `readTime()` function returns a [TimeModel](#timemodel) for the whole entry based on it's field layout.
The `readTime()` function returns a [TimeModel](#timemodel) for matrix fields or 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` - this only affects the human time format.

#### Examples

```twig
{{ readTime(entry) }}
Returns: 9 minutes, 40 seconds
{{ readTime(entry) }} or {{ readTime(entry.matrixField.all()) }}
```

```twig
{{ readTime(entry, false) }}
Returns: 10 minutes
{{ readTime(entry, false) }} or {{ readTime(entry.matrixField.all(), false) }}
```

## TimeModel
Expand All @@ -92,6 +88,8 @@ Outputting a TimeModel object without attaching a property or method will return
{{ string|readTime }}
{{ readTime(entry) }}
{{ readTime(entry.matrixField.all()) }}
```

### Properties
Expand Down
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.4.0",
"version": "1.5.0",
"keywords": [
"craft",
"cms",
Expand Down
89 changes: 54 additions & 35 deletions src/twigextensions/ReadTimeTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,48 +48,67 @@ public function readTimeFunction($element, $showSeconds = true)
$totalSeconds = 0;
$vals = '';

foreach ($element->getFieldLayout()->getFields() as $field) {
try {
// If field is a matrix then loop through fields in block
if ($field instanceof \craft\fields\Matrix) {
foreach($element->getFieldValue($field->handle)->all() as $block) {
$blockFields = $block->getFieldLayout()->getFields();

foreach ($blockFields as $blockField) {
$value = $block->getFieldValue($blockField->handle);
$seconds = $this->valToSeconds($value);
$totalSeconds = $totalSeconds + $seconds;
}
}
} elseif($field instanceof \verbb\supertable\fields\SuperTableField) {
foreach($element->getFieldValue($field->handle)->all() as $block) {
$blockFields = $block->getFieldLayout()->getFields();

foreach ($blockFields as $blockField) {
if ($blockField instanceof \craft\fields\Matrix) {
foreach($block->getFieldValue($blockField->handle)->all() as $matrix) {
$matrixFields = $matrix->getFieldLayout()->getFields();

foreach ($matrixFields as $matrixField) {
$value = $matrix->getFieldValue($matrixField->handle);
$seconds = $this->valToSeconds($value);
$totalSeconds = $totalSeconds + $seconds;
}
}
} else {
if ($element instanceof \craft\elements\Entry) {
// Provided value is an entry

foreach ($element->getFieldLayout()->getFields() as $field) {
try {
// If field is a matrix then loop through fields in block
if ($field instanceof \craft\fields\Matrix) {
foreach($element->getFieldValue($field->handle)->all() as $block) {
$blockFields = $block->getFieldLayout()->getFields();

foreach ($blockFields as $blockField) {
$value = $block->getFieldValue($blockField->handle);
$seconds = $this->valToSeconds($value);
$totalSeconds = $totalSeconds + $seconds;
}
}
} elseif($field instanceof \verbb\supertable\fields\SuperTableField) {
foreach($element->getFieldValue($field->handle)->all() as $block) {
$blockFields = $block->getFieldLayout()->getFields();

foreach ($blockFields as $blockField) {
if ($blockField instanceof \craft\fields\Matrix) {
foreach($block->getFieldValue($blockField->handle)->all() as $matrix) {
$matrixFields = $matrix->getFieldLayout()->getFields();

foreach ($matrixFields as $matrixField) {
$value = $matrix->getFieldValue($matrixField->handle);
$seconds = $this->valToSeconds($value);
$totalSeconds = $totalSeconds + $seconds;
}
}
} else {
$value = $block->getFieldValue($blockField->handle);
$seconds = $this->valToSeconds($value);
$totalSeconds = $totalSeconds + $seconds;
}
}
}
} else {
$value = $element->getFieldValue($field->handle);
$seconds = $this->valToSeconds($value);
$totalSeconds = $totalSeconds + $seconds;
}
} catch (ErrorException $e) {
continue;
}
}
} elseif(is_array($element)) {
// Provided value is a matrix field
Craft::info('matrix field provided', 'readtime');

foreach ($element as $block) {
if ($block instanceof \craft\elements\MatrixBlock) {
$blockFields = $block->getFieldLayout()->getFields();

foreach ($blockFields as $blockField) {
$value = $block->getFieldValue($blockField->handle);
$seconds = $this->valToSeconds($value);
$totalSeconds = $totalSeconds + $seconds;
}
} else {
$value = $element->getFieldValue($field->handle);
$seconds = $this->valToSeconds($value);
$totalSeconds = $totalSeconds + $seconds;
}
} catch (ErrorException $e) {
continue;
}
}

Expand Down

0 comments on commit 7e8e43e

Please sign in to comment.