You are here

public static function SocialProfilePrivacyBatchHelper::updateProcess in Open Social 10.2.x

Same name and namespace in other branches
  1. 10.3.x modules/social_features/social_profile/modules/social_profile_privacy/src/Service/SocialProfilePrivacyBatchHelper.php \Drupal\social_profile_privacy\Service\SocialProfilePrivacyBatchHelper::updateProcess()

Process operation to update content retrieved from init operation.

Parameters

array $items: Items.

array $context: An array that may or may not contain placeholder variables.

File

modules/social_features/social_profile/modules/social_profile_privacy/src/Service/SocialProfilePrivacyBatchHelper.php, line 48

Class

SocialProfilePrivacyBatchHelper
Class SocialProfilePrivacyBatchHelper.

Namespace

Drupal\social_profile_privacy\Service

Code

public static function updateProcess(array $items, array &$context) {

  // Elements per operation.
  $limit = 50;

  // Set default progress values.
  if (empty($context['sandbox']['progress'])) {
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['max'] = count($items);
  }

  // Save items to array which will be changed during processing.
  if (empty($context['sandbox']['items'])) {
    $context['sandbox']['items'] = $items;
  }
  if (!empty($context['sandbox']['items'])) {

    /** @var \Drupal\profile\ProfileStorageInterface $profile_storage */
    $profile_storage = \Drupal::entityTypeManager()
      ->getStorage('profile');

    // Get items for processing.
    $current_pids = array_splice($context['sandbox']['items'], 0, $limit);

    // Load profiles by profiles IDs.
    $profiles = $profile_storage
      ->loadMultiple($current_pids);
    foreach ($profiles as $profile) {
      if ($profile instanceof ProfileInterface) {
        SocialProfilePrivacyBatchHelper::updateProfileName($profile);
      }
      $context['sandbox']['progress']++;
      $context['message'] = t('Now processing profile :progress of :count', [
        ':progress' => $context['sandbox']['progress'],
        ':count' => $context['sandbox']['max'],
      ]);

      // Increment total processed item values. Will be used in finished
      // callback.
      $context['results']['processed'] = $context['sandbox']['progress'];
    }
  }

  // If not finished all tasks, we count percentage of process. 1 = 100%.
  if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  }
}