You are here

function mailchimp_lists_populate_member_batch in Mailchimp 8

Same name and namespace in other branches
  1. 7.3 modules/mailchimp_lists/mailchimp_lists.module \mailchimp_lists_populate_member_batch()
  2. 2.x modules/mailchimp_lists/mailchimp_lists.module \mailchimp_lists_populate_member_batch()

Batch processor for member mergevar updates to built the mergevar arrays.

1 string reference to 'mailchimp_lists_populate_member_batch'
mailchimp_lists_update_member_merge_values in modules/mailchimp_lists/mailchimp_lists.module
Triggers an update of all merge field values for appropriate entities.

File

modules/mailchimp_lists/mailchimp_lists.module, line 425
Mailchimp lists/audiences module.

Code

function mailchimp_lists_populate_member_batch($entity_type, $bundle_name, $field, $mergefields, &$context) {
  if (!isset($context['sandbox']['progress'])) {

    // Load up all our eligible entities.
    $query = \Drupal::entityQuery($entity_type);
    $definition = \Drupal::entityTypeManager()
      ->getDefinition($entity_type);
    if ($definition
      ->hasKey('bundle')) {
      $query
        ->condition($definition
        ->getKey('bundle'), $bundle_name);
    }
    $query_results = $query
      ->execute();
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['max'] = isset($query_results) ? count($query_results) : 0;
    if ($context['sandbox']['max']) {
      $context['sandbox']['entity_ids'] = array_keys($query_results);
      $context['results']['update_queue'] = [];
    }
  }
  if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
    $batch_size = 50;
    $item_ids = array_slice($context['sandbox']['entity_ids'], $context['sandbox']['progress'], $batch_size);
    $entities = \Drupal::entityTypeManager()
      ->getStorage($entity_type)
      ->loadMultiple($item_ids);
    foreach ($entities as $entity) {
      $merge_vars = _mailchimp_lists_mergevars_populate($mergefields, $entity);
      if ($merge_vars['EMAIL'] && isset($context['results']['subscribers'][strtolower($merge_vars['EMAIL'])])) {
        $context['results']['update_queue'][] = [
          'email' => $merge_vars['EMAIL'],
          // Preserve subscribers's email type selection:
          'email_type' => $context['results']['subscribers'][strtolower($merge_vars['EMAIL'])]->email_type,
          'merge_vars' => $merge_vars,
        ];
      }
      $context['sandbox']['progress']++;
    }
    $context['message'] = t('Checking for changes on items %count - %next.', [
      '%count' => $context['sandbox']['progress'],
      '%next' => $context['sandbox']['progress'] + $batch_size,
    ]);
    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  }
}