2

I have a field age in profile of my Drupal Instant which computes age from the D.O.B of the user. How do I make sure the age is updated along with cron job. Is there any way to trigger.

Right now I believe it will only update when the profile is saved.

2 Answers 2

2

From your question, by hook_cron() this can be done. You can use a watchdog() function to make sure that the cron was run. The computed field has a table entry in the database, so you just need to find out that one and check each user's birthday from a DOB field stored in another table based on UID. Then if the user has updated the DOB then check for their ages and insert/update the value in the "field_age" computed field. All of the operation can take place in hook_cron() which will be invoked by cron.php each time the page is fired in crontab.

2
  • User won't update D.O.B. But as the days passes, aleast every year the age should change right? so I will have to update each user in loop by checking it against date?
    – esafwan
    Commented Jan 1, 2013 at 8:32
  • About the DOB, once a user creates a profile, that will be inserted. So suppose it has a table field_dob in drupal 7 db. No further updates are possible I think. So for field_age, yes you need to have to loop via each user in users table fetch the UID and then update each user's age in the field_age table rows with the key uid stored in the table as entity_id I suppose. If you want to check this in each year then create a separate php file and include bootstrap as BOOTSTRAP_FULL and run that file each year in cron via crontab. Commented Jan 1, 2013 at 8:43
1

I would rather implement hook_node_load(), and check if the age field needs updating.

If it needs updating, save the node and reload the node. (I believe that calculated field would be updated when saving)

If you update the age field with cron, then a wrong value could be displayed, if cron hasn't run yet and it's user's birthday.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.