0

I have a problem with caching. I've set up a menu hook for an URL like /user/USER_ID/some_info, which automatically receives the entire user object because of the way I set up my menu hook (see below).

The problem I'm facing is that I have all caching modules enabled in Drupal (including EntityCache) and the $usr variable gets passed from the cache. So naturally if I update a field and save it using field_attach_update, the data is saved into the DB, but the old data is shown on the form, confusing the admins (the checkbox is checked, even though I just unchecked it, and it's value in the DB is in fact 0).

Is there a way I can disable caching for this particular object ($usr) in this particular function?

Note: on my local machine where caching is pretty much disabled, it works fine, both the UI and DB get updated.

My menu hook:

$items['user/%user/some_info'] = array(
    'title' => 'Some Info',
    'description' => 'Edit',
    'page callback' => 'mymodule_edit_user',
    'page arguments' => array(1),
    'access arguments' => array('administer users'),
    'file' => 'mymodule.pages.inc',
    'type' => MENU_LOCAL_TASK,
    'weight' => 8
);

The function that gets executed:

function mymodule_edit_user($usr){
    // check if POST and validation here
    if(isset($_POST['somefield'])) //checkbox
        $usr->field_somefield[LANGUAGE_NONE][0]['value'] = 1;
    else
        $usr->field_somefield[LANGUAGE_NONE][0]['value'] = 0;

    field_attach_update('user', $usr);
}
2

Sorry I didn't do more digging before asking this, but going to answer my own question in case someone else finds this.

I've taken a look inside the user_save function, and seen that it does some invoking, so I did it too. Surprise: it worked (EntityCache was the bad guy who was caching everything BTW).

Here's my code for reference:

$usr->field_somefield[LANGUAGE_NONE][0]['value'] = 0;
field_attach_presave('user', $usr);
module_invoke_all('entity_presave', $usr, 'user');
module_invoke_all('entity_update', $usr, 'user');
field_attach_update('user', $usr);

Your Answer

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

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