You are here

public function QueueWorker::queue in Simple XML sitemap 4.x

Same name and namespace in other branches
  1. 8.3 src/Queue/QueueWorker.php \Drupal\simple_sitemap\Queue\QueueWorker::queue()

Parameters

string[]|string|null $variants:

Return value

$this

Throws

\Drupal\Component\Plugin\Exception\PluginException

1 call to QueueWorker::queue()
QueueWorker::rebuildQueue in src/Queue/QueueWorker.php

File

src/Queue/QueueWorker.php, line 134

Class

QueueWorker

Namespace

Drupal\simple_sitemap\Queue

Code

public function queue($variants = NULL) : QueueWorker {
  $all_data_sets = [];
  $empty_variants = [];
  $variants = $variants !== NULL ? (array) $variants : NULL;

  /** @var \Drupal\simple_sitemap\Entity\SimpleSitemap[] $variants */
  $variants = $this->entityTypeManager
    ->getStorage('simple_sitemap')
    ->loadMultiple($variants);
  foreach ($variants as $variant_id => $variant) {
    $data_sets = [];
    foreach ($variant
      ->getType()
      ->getUrlGenerators() as $url_generator_id => $url_generator) {
      $data_sets = $url_generator
        ->setSitemapVariant($variant)
        ->getDataSets();

      //todo automatically set variant
      foreach ($data_sets as $data_set) {
        $all_data_sets[] = [
          'data' => $data_set,
          'sitemap' => $variant_id,
          'url_generator' => $url_generator_id,
        ];
        if (count($all_data_sets) === self::REBUILD_QUEUE_CHUNK_ITEM_SIZE) {
          $this
            ->queueElements($all_data_sets);
          $all_data_sets = [];
        }
      }
    }
    if (empty($data_sets)) {
      $empty_variants[] = $variant_id;
    }
  }
  if (!empty($all_data_sets)) {
    $this
      ->queueElements($all_data_sets);
  }
  $this
    ->getQueuedElementCount(TRUE);

  // Remove all sitemap content of variants which did not yield any queue elements.
  foreach ($empty_variants as $empty_variant) {
    $variants[$empty_variant]
      ->deleteContent();
  }
  return $this;
}