Read_time modifier working with bard that has sets? #4341
Replies: 3 comments 7 replies
-
@austriker27 I ran into the same issue and ended up writing a modifier that would try to flatten everything. But it's far from working well, so I'd prefer a core fix. I've opened an issue for you. <?php
namespace App\Modifiers;
use Statamic\Modifiers\Modifier;
class TextOnly extends Modifier
{
/**
* Modify a value.
*
* @param mixed $value The value to be modified
* @param array $params Any parameters used in the modifier
* @param array $context Contextual values
* @return mixed
*/
public function index($value, $params, $context)
{
$value = array_flatten(json_decode(json_encode($value), false));
$fields = ['lead', 'title', 'description', 'content'];
$text = '';
foreach ($value as $item) {
foreach ($fields as $field) {
if (isset($item->$field) && is_string($item->$field)) {
$text .= $item->$field . ' ';
}
}
}
return strip_tags($text);
}
} I’m using the modifier this way in my Antlers template: {{ newsbuilder | text_only | strip_tags | read_time }} min read. |
Beta Was this translation helpful? Give feedback.
-
Not sure what the gents think but in an ideal world I'd love if you could just do this... (the {{ blog_content | read_time:180 }} min read |
Beta Was this translation helpful? Give feedback.
-
Keep an eye on statamic/ideas#94 |
Beta Was this translation helpful? Give feedback.
-
Hello!
Has anyone gotten the
read_time
modifier working with bard when you have sets?I found this post for awhile ago but its not working for me (post is from V2):
https://statamic.com/forum/2970-read-time-from-bard-fieldtype
When I try this in antlers:
I get this error:
Error: Array to string conversion
Inside the CMS, at the bottom of the bard field it does show the read time when Im using sets. Not sure if this is helpful but I looked at the source code and its using this package: https://www.npmjs.com/package/read-time-estimate
Beta Was this translation helpful? Give feedback.
All reactions