You are here

function xmlsitemap_xmlsitemap_index_links in XML sitemap 8

Same name and namespace in other branches
  1. 2.x xmlsitemap.module \xmlsitemap_xmlsitemap_index_links()

Implements hook_xmlsitemap_index_links().

1 call to xmlsitemap_xmlsitemap_index_links()
xmlsitemap_cron in ./xmlsitemap.module
Implements hook_cron().

File

./xmlsitemap.module, line 1590
xmlsitemap XML sitemap

Code

function xmlsitemap_xmlsitemap_index_links($limit) {
  $entity_type_manager = \Drupal::entityTypeManager();
  $entity_types = $entity_type_manager
    ->getDefinitions();
  foreach ($entity_types as $entity_type_id => $entity_type) {

    // If an entity type is not supported it will not have link info.
    $info = xmlsitemap_get_link_info($entity_type_id);
    if (empty($info)) {
      continue;
    }
    $bundles = xmlsitemap_get_link_type_enabled_bundles($entity_type_id);
    if (empty($bundles)) {
      continue;
    }
    try {
      $query = $entity_type_manager
        ->getStorage($entity_type_id)
        ->getQuery();
      $query
        ->range(0, $limit);
      if (!empty($info['entity keys']['bundle'])) {
        $query
          ->condition($info['entity keys']['bundle'], $bundles, 'IN');
      }

      // Perform a subquery against the xmlsitemap table to ensure that we are
      // only looking for items that we have not already indexed.
      $subquery = \Drupal::database()
        ->select('xmlsitemap', 'x');
      $subquery
        ->addField('x', 'id');
      $subquery
        ->condition('type', $entity_type_id);

      // If the storage for this entity type is against a SQL backend, perform
      // a direct subquery condition to avoid needing to load all the IDs.
      if ($query instanceof \Drupal\Core\Entity\Query\Sql\Query) {
        $query
          ->condition($info['entity keys']['id'], $subquery, 'NOT IN');
      }
      else {
        $query
          ->condition($info['entity keys']['id'], $subquery
          ->execute()
          ->fetchCol(), 'NOT 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_index_links');
      if ($ids = $query
        ->execute()) {

        // Chunk the array into batch sizes.
        $chunks = array_chunk($ids, \Drupal::config('xmlsitemap.settings')
          ->get('batch_limit'));
        foreach ($chunks as $chunk) {
          $info['xmlsitemap']['process callback']($entity_type_id, $chunk);
        }
        \Drupal::logger('xmlsitemap')
          ->info('Indexed @count new @type items.', [
          '@count' => count($ids),
          '@type' => $entity_type_id,
        ]);
      }
    } catch (\Exception $e) {
      watchdog_exception('xmlsitemap', $e);
    }
  }
}