Jump to content

Calculate estimated reading time of content inside matrix field


Zeka
 Share

Recommended Posts

Hi!

I need to calculate estimated reading time for all of content inside all text extended fields inside repeater matrix field.

That's how I do it now, but sometime I forgot to update selector when fields are changed for some of matrix types. 

/**
* Calculate and save field value of estimated reading time
*/
public function calculateReadingTime($page)
{

  $sanitizer = $this->wire('sanitizer');
  $content = '';
  $page->builder->find('body|title|otherfields!=""')->each(function ($item) use (&$content, $sanitizer) {
  $content .= $item->get('body|title|otherfields');
  });

  $page->readtime = $this->calculateEstimatedReadingTime($content);
}

That's why I was trying to use selector by Fieldtypes that was introduced in 3.0.91 with no luck

$page->builder->find('FieldtypeText.extends!=""')->each(function ($item) use (&$content, $sanitizer) {
	$content .= $item->get(...);
});

So the question is there more optimal way of doing it where I don't need to enter names of fields manualy? 

Link to comment
Share on other sites

What about finding the fields first?

$textFields = $fields->find('type=FieldtypeTextarea|FieldtypeText')

$page->builder->find("$textFields!=''")->each(function ($item) use (&$content, $sanitizer) {
	$content .= $item->get(...);
});

EDIT: Oh just read again about "text extended fields". This obviously won't work :/

Maybe:

$textExtendedFields = [];
$fields->each(function($f){
   if(wireInstanceOf($f->type, "FieldtypeText") === true) {
	$textExtendedFields[] = $f;
   };
})

 

  • Like 1
Link to comment
Share on other sites

@elabx Thanks for idea.

$repeater_fields = $this->wire('fields')->get('builder')->repeaterFields;
$text_based_fields = $this->wire('fields')->find("id=" . implode($repeater_fields, '|') . ', type=FieldtypeTextarea|FieldtypeText|FieldtypeTextareaLanguage|FieldtypeTextareaLanguage');
$search_fields = $text_based_fields->implode('|', 'name');


$page->builder->find("{$search_fields}!=''")->each(function ($item) use (&$content, $search_fields) {
	$content .= $item->get($search_fields);
});

Now I need to get it work with different langauges. 

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...