1

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:

  1. The results of the query in the Computed PHP setting of the field is being cached somehow. Find a way to stop this.

  2. Use 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?

  • 2
    "Updated on view" really means "updated on cache generation", as cache happens after view hooks and handlers. What you need is to force Drupal to clear appropriate cache entries on comment save. How? I have no idea. – Mołot Jan 30 '14 at 11:08
  • Related: drupal.stackexchange.com/q/62639/16495 – Mołot Jan 30 '14 at 11:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.