I have a requirement to list events via view, but these events are categorized as past, ongoing and upcoming events. View results should show ongoing first, then upcoming and past events in the same view. I have used a computed(integer) field to set the value for each category. While creating a node based on start/end date I am setting this field value to 1(ongoing),2(upcoming), and 3(past). In the views, I am sorting the events based on this numeric field Ascending. It works fine, but issues arise when upcoming events become ongoing, then this computed field value is not updated to show as ongoing. How do I update this field value before this view is rendered?
Are you asking how to update a field on multiple nodes programmatically? There exist on this site answers for that. Or, are you asking if there is a better way to implement this feature?– cilefenAug 17, 2022 at 11:21
Computed fields are usually updated on node save. Does an upcoming event become ongoing without the relevant node being saved?– Patrick KennyAug 17, 2022 at 11:40
Hi @PatrickKenny upcoming will become ongoing based on the start date provided in the node. but when that node is shown in the events list from view then that computed flag should be updated.– SurajAug 17, 2022 at 12:26
Hi @cilefen I'm asking if is there any way to update the node field while processing the view and rendering but it should be before sorting. Because sorting will be based on that dynamic field. Or if saving node while processing view will put more load on views then is there any way to update just query result which will feed to views– SurajAug 17, 2022 at 12:29
You cannot (or at least, really should not) force nodes to be re-saved before accessing a view. Such an approach is very inefficient.
The issue here is that you have a computed field that does a calculation based on a date and the current time. If the date is in the future, the field has one value, and if the date is in the past, the field has another value. Using a computed field here is not a good approach because as time passes, you have to re-save the nodes to ensure they have correct values.
One (inefficient) way to update the computed fields would be to batch re-save your nodes in cron.
A similar approach would be to use the Job Scheduler module to set up jobs to periodically resave the nodes.
However, I don't think saving a computed value is the right approach if it may have to be recomputed when a user accesses a node or a view. If you need to compute a value on the fly, then don't store it in a computed field.
Instead, do the computing when you process the view. To do this, you can write a custom Views sort plugin. You can move the computed field logic to this plugin and then sort based on the date field (instead of the computed field).