1

I need to re-save about 300 user to initialize geo-coding on their address fields. On /admin/people, I don't see any bulk action that would fit my needs. I have tested the update path-alias but with no luck.

Also, the VBO module for Drupal 8 doesn't offer a solution for users, AFAIK.

Any help is appreciated.

1 Answer 1

2

I don't know how comfortable you are with code. I would suggest writing an update hook in a custom module. Something like:

MYMODULE_update_8100(&$sandbox) {
  // On the initial run, set up the update.
  if (empty($sandbox)) {
    // Get all user ids.
    $sandbox['items'] = \Drupal::entityQuery('user')
      ->accessCheck(FALSE)
      ->execute();
    $sandbox['max'] = count($sandbox['items']);

    if (!$sandbox['max']) {
      // Prevent division by zero if there are no users.
      $sandbox['#finished'] = 1;
      return;
    }
  }

  $iteration = 0;
  while ($iteration < 25 && $sandbox['items']) {
    $iteration ++;

    // Resave each user.
    \Drupal::entityTypeManager()
      ->getStorage('user')
      ->load(end($sandbox['items']))
      ->save();

    // Remove the end of the items list when the item has finished 
    // processing.
    array_pop($sandbox['items']);
  }

  // Update the completion status as a number between 0 and 1.
  $sandbox['#finished'] = ($sandbox['max'] - count($sandbox['items']) / $sandbox['max'];
}

See hook_update_N for more information.

11
  • Thanks @Charles Bamford, i'll give it a try. Time to write my first d8 module... :)
    – Volker
    Jan 22, 2020 at 18:19
  • Good luck have fun! Jan 22, 2020 at 18:22
  • 2
    The timeout thing looks a bit odd. Actually when you batch process users the right way you don't need to worry about timeouts. That's what the sandbox is for. You need to make your code look like in the following sample which processes users in chunks of 25 users each. gorannikolovski.com/blog/….
    – leymannx
    Jan 22, 2020 at 21:55
  • 3
    I'm with @leymannx on this one. The standard pattern in core update hooks is to do a COUNT() query (or similar) to initialize, and then load the items in each batch with a ->range() query. Personally, I think the timeout approach is fragile.
    – mpdonadio
    Jan 23, 2020 at 0:46
  • 1
    When drupal installs a module, it checks for hook update N and assumes that the installation schema is the same as the schema version of the update and doesn't process any previous updates. The module needs to be enabled first, then the update can be added. It is usually added into a .install file. Jan 27, 2020 at 16:40

Your Answer

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

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