I have a blog post content type, with a Computed Field that shows the number of comments on the post.
The field's Computed PHP setting makes a database query to count the number of comments attached to the node. The computed value is not set to be stored in the database, so should be calculated on the fly.
When a new comment is submitted on a blog post, the comment count field does not update automatically. Now, since I'm not storing the value in the database, I'm expecting it to be updated on node view, as per documentation:
You can also choose whether to store your computed field values in the database with other content fields, or have them "calculated" on the fly during node views.
The only way I can get the comment count to update is by flushing the cache from the admin menu. So this leaves me with two potential options:
The results of the query in the Computed PHP setting of the field is being cached somehow. Find a way to stop this.
hook_comment_insert()to clear the cache when a comment is saved. The problem with this is that the whole cache is cleared which is expensive. I actually tried this and couldn't get
cache_clear_all()to trigger for some reason, so I don't feel great about this method.
Any pointers? If indeed Computed Fields are meant to be calculated on node view, what is preventing this from happening?