4

Very similar to this question except I need to save users, not nodes.

Use case: I added a field (single boolean) to a feature so that the field is added to user profiles. I then pushed the feature to my staging server. However, existing user accounts do not have the default field value for this field assigned. Re-saving each account fixes the problem, but there are 1000 users.

How can I quickly re-save all users on my site?

  • 2
    Users are entities too, you can use exactly the same logic from the other question (VBO with PHP, or a manual update) and just replace node_save() with user_save() – Clive Apr 20 '13 at 14:35
  • @Clive @rooby Thanks for the tips here. I just tried user_save($entity); this causes the user to be saved (I confirmed by adding a rule that shows a message on the site). However, no value is assigned to the new field, even if I run the save action multiple times. If I do it manually, though, from the People page, the default value is set. – Patrick Kenny Apr 20 '13 at 15:17
  • 2
    How about forcing it through...$items = field_get_items('user', $entity, 'field_name'); if (empty($items)) { $entity->field_name[LANGUAGE_NONE][0]['value'] = 1; } user_save($entity); That should do the trick – Clive Apr 20 '13 at 15:20
7

You can use the "Execute arbitrary PHP script" bulk operation in VBO and when you run it enter:

user_save($entity);

You could also run this PHP (you can do so by enabling the devel module and going to devel/php on your site):

set_time_limit(0);
$uids = db_query('SELECT uid FROM {users} WHERE uid != 0')->fetchCol();
$users = user_load_multiple($uids);
foreach ($users as $user) {
  user_save($user);
}

If you don't have many users you shouldn't need the time limit line.

Also, Clive mentions that field_attach_update is faster. To use that replace the user_save line with field_attach_update('user', $user)

  • What is the VBO save action for users? I looked for it but I couldn't find it. – Patrick Kenny Apr 20 '13 at 14:42
  • @Patrick is there not a generic save action for entities? (I haven't checked I just assumed there would be) – Clive Apr 20 '13 at 14:43
  • 1
    My mistake, there is a "Save content" one for nodes but no equivalent user one. I have removed that part of my answer. – rooby Apr 20 '13 at 14:47
  • 1
    Thanks, I put a note about field_attach_update(). I also added an even nicer solution using VBO execute PHP code (thanks Bojan) – rooby Apr 20 '13 at 14:57
  • 1
    The PHP code via Devel worked great. I had to add 'WHERE uid > 0' to avoid an integrity constraint error in the logs. – Ben Jan 13 '15 at 1:01
5

rooby's answer is probably the easiest/quickest method but just to offer a code solution that can be used with VBO...

You can define your own action to save the user like this:

function MYMODULE_rules_action_info() {
  return array(
   'MYMODULE_update_user_account' => array(
     'label' => t('Save a user account'),
     'parameter' => array(
       'account' => array(
         'type' => 'user',
         'label' => t('User'),
       ),
     ),
     'group' => t('MODULENAME'),
   ),
 );
}

function MYMODULE_update_user_account($account) {
  user_save($account);
}

Once you've clerared the caches you should see your new action appear under VBO/rules and anywhere else actions are used.

Edit: It seems this might re-inventing the wheel somewhat if VBO will already let you do it natively, but it's a nice simple example of a rules action so I'll leave it here in case anyone needs it. Plus it removes the need for eval() to be used which is never a bad thing.

  • Seeing as this is now the leading answer, if anyone actually does end up doing it this way, please submit it back as a patch. The rightful module is the user module because the node module provides the save content action, however there might be more chance of it being committed to the views_bulk_operations module. - Maybe a drupal 8 user module patch that might have a chance at back port to 7. – rooby Apr 21 '13 at 0:26

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.