You are here

public function XmlSitemapGenerator::rebuildBatchFetch in XML sitemap 2.x

Same name and namespace in other branches
  1. 8 src/XmlSitemapGenerator.php \Drupal\xmlsitemap\XmlSitemapGenerator::rebuildBatchFetch()

Batch callback; fetch and add the sitemap links for a specific entity.

Parameters

string $entity_type_id: Entity type to be rebuilt.

array|\ArrayAccess $context: Context to be rebuilt.

Overrides XmlSitemapGeneratorInterface::rebuildBatchFetch

File

src/XmlSitemapGenerator.php, line 516

Class

XmlSitemapGenerator
XmlSitemap generator service class.

Namespace

Drupal\xmlsitemap

Code

public function rebuildBatchFetch($entity_type_id, &$context) {
  if (!isset($context['sandbox']['info'])) {
    $context['sandbox']['info'] = xmlsitemap_get_link_info($entity_type_id);
    $context['sandbox']['bundles'] = xmlsitemap_get_link_type_enabled_bundles($entity_type_id);
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['last_id'] = 0;
  }
  if (empty($context['sandbox']['bundles'])) {
    return;
  }
  $info = $context['sandbox']['info'];
  $query = $this->entityTypeManager
    ->getStorage($entity_type_id)
    ->getQuery();
  $query
    ->condition($info['entity keys']['id'], $context['sandbox']['last_id'], '>');
  if (!empty($info['entity keys']['bundle'])) {
    $query
      ->condition($info['entity keys']['bundle'], $context['sandbox']['bundles'], 'IN');
  }

  // Access for entities is checked individually for the anonymous user
  // when each item is processed. We can skip the access check for the
  // query.
  $query
    ->accessCheck(FALSE);
  $query
    ->addTag('xmlsitemap_rebuild');
  if (!isset($context['sandbox']['max'])) {
    $count_query = clone $query;
    $count_query
      ->count();
    $context['sandbox']['max'] = $count_query
      ->execute();
    if (!$context['sandbox']['max']) {

      // If there are no items to process, skip everything else.
      return;
    }
  }

  // PostgreSQL cannot have the ORDERED BY in the count query.
  $query
    ->sort($info['entity keys']['id']);

  // Get batch limit.
  $limit = $this->config
    ->get('batch_limit');
  $query
    ->range(0, $limit);
  $result = $query
    ->execute();
  $info['xmlsitemap']['process callback']($entity_type_id, $result);
  $context['sandbox']['last_id'] = end($result);
  $context['sandbox']['progress'] += count($result);
  $context['message'] = $this
    ->t('Processed %entity_type_id @last_id (@progress of @count).', [
    '%entity_type_id' => $entity_type_id,
    '@last_id' => $context['sandbox']['last_id'],
    '@progress' => $context['sandbox']['progress'],
    '@count' => $context['sandbox']['max'],
  ]);
  if ($context['sandbox']['progress'] >= $context['sandbox']['max']) {
    $context['finished'] = 1;
  }
  else {
    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  }
}