Hey, first of all thanks for the module.

How would one go about adding this field as a filter in a view? Let's say: I want to filter nodes for which the reading time is inferior to X.

Comments

Nelson Alves created an issue. See original summary.

yivanov’s picture

Hi @Nelson, thanks for using the module :) Your idea is great and I would definitely be happy to implement this when I have more free time.

nsalves’s picture

StatusFileSize
new6.33 KB

Hey yivanov, thanks for the quick reply.
I went a little deeper in the module and noticed the current implementation makes use of a pseudo-field. I believe this means there's no way (at least that I know) to add a filter plugin for views since the field is not stored in the database. I think a good solution would be to create a new field and store there the calculated read time the modules provides. This way the users could use this new field to filter or sort in views.
The attached patch does just that. The idea is to have a checkbox in the configuration form where the user can choose if he wants to create this new field (this way there's no impact in the current functionality). If the checkbox is selected, a new field (and field storage) is created programmatically in the selected bundles. Additionally, the patch implements an hook_entity_save to calculate the reading time and store it in the newly created fields. If by any chance the user decides that he doesn't want a certain bundle to keep displaying the reading time ( as provided by this module) and uncheck the box then the created fields are automatically deleted.

Edit: I believe the patch only works if the unit time is "minutes". I'll have to check but I'm pretty sure I didn't account for the other cases

nsalves’s picture

StatusFileSize
new7.23 KB

Attaching new patch that fixes an error if the user selected the unit of time "Default (only minute) without text" and a validation in the configuration form to not allow the user to select the create field option with the unit of time "Minute and Second" selected (the field created is of the type integer). Hope all its good now :)

nsalves’s picture

StatusFileSize
new7.26 KB

Attaching new patch that fixes some php notices