Problem: modules using hook_entity_insert() might call profile2_load_by_user(), which has an outdated cache. The cache is updated too late, so we need to clear it before invoking the insert hook.

My use case is Rules, where I wanted to sync profile fields to mailchimp, but the fields where always empty because the new profile was not returned.

Patch attached.

TODO: this should come with a test case.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

fago’s picture

Status: Needs review » Needs work

ok, that's reasonable (although the hook might be used for storage only, ouch). But why does it change the cache from being updated to being cleared?

scottsawyer’s picture

Issue summary: View changes

I just spent two days creating a rule action that runs on "After saving a new profile" and couldn't figure out why I wasn't getting any profile2 data back from profile2_load_by_user(). Applied this patch and now works perfectly.

@fago I would be happy to test updating the cache instead of clearing it if you think that is a better way. What would that code look like? I am not sure that clearing the cache would be the best idea on a site with busy registrations.

jantoine’s picture

Status: Needs work » Needs review
FileSize
1.71 KB

@fago,

The cache must be cleared because if a new profile is being saved, the profile doesn't yet have a pid and so the cache would be updated to NULL for the profile type being created. The cache should be updated if and when the profile2_load_by_user() function is called.

Also, you can't unset the cache for only the profile type being saved due to how the caching mechanism of profile2_load_by_user() works. If any profile ids are cached for the user, it loads directly from the cache and does not check individual profiles types. This actually led to the discovery of another bug with caching where a profile type is passed into the profile2_load_by_user() function the first time it's called via the $type_name parameter. This causes only that profile type to be cached for the user, so any subsequent calls to profile2_load_by_user() for other profile type fail.

The attached patch has been updated against HEAD and also contains a fix for the secondary caching issue discovered.

rp7’s picture

I can confirm this issue & the patch in #3 solves the problem.

Thank you @jantoine.

balintcsaba’s picture

Status: Needs review » Reviewed & tested by the community

+ the patch in #3 solves the problem.

This is a real problem with tokens etc, so I think it's time for this to go on dev branch.

  • Rick J committed fa8504f on 7.x-1.x authored by jantoine
    Issue #1860094 by jantoine, klausi: profile2_load_by_user() cache needs...
RickJ’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.