You are here

class UserSyncBatch in CRM Core 8

Same name and namespace in other branches
  1. 8.3 modules/crm_core_user_sync/src/UserSyncBatch.php \Drupal\crm_core_user_sync\UserSyncBatch

Methods for running the ConfigImporter in a batch.

@package Drupal\crm_core_user_sync

Hierarchy

Expanded class hierarchy of UserSyncBatch

1 file declares its use of UserSyncBatch
SettingsForm.php in modules/crm_core_user_sync/src/Form/SettingsForm.php

File

modules/crm_core_user_sync/src/UserSyncBatch.php, line 10

Namespace

Drupal\crm_core_user_sync
View source
class UserSyncBatch {

  /**
   * Batch operation callback.
   */
  public static function progress(&$context) {
    $userStorage = \Drupal::entityTypeManager()
      ->getStorage('user');
    if (empty($context['sandbox'])) {
      $max = $userStorage
        ->getQuery()
        ->condition('uid', 1, '>')
        ->count()
        ->execute();
      $context['sandbox']['max'] = $max;
      $context['sandbox']['progress'] = 0;
      $context['results']['synced'] = 0;
      $context['sandbox']['last_uid'] = 1;
    }
    $limit = 20;
    $uids = $userStorage
      ->getQuery()
      ->sort('uid')
      ->condition('uid', $context['sandbox']['last_uid'], '>')
      ->range(0, $limit)
      ->execute();
    $accounts = $userStorage
      ->loadMultiple($uids);
    foreach ($accounts as $account) {
      if (\Drupal::service('crm_core_user_sync.relation')
        ->relate($account)) {
        $context['results']['synced']++;
      }
      $context['sandbox']['last_uid'] = $account
        ->id();
      $context['sandbox']['progress']++;
    }
    if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
      $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
    }
  }

  /**
   * Batch finished callback.
   */
  public static function finished($success, $results, $operations) {
    \Drupal::messenger()
      ->addMessage(t('@count users have been associated with contacts.', [
      '@count' => $results['synced'],
    ]));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
UserSyncBatch::finished public static function Batch finished callback.
UserSyncBatch::progress public static function Batch operation callback.