View source
<?php
namespace Drupal\simple_sitemap_views\Plugin\QueueWorker;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\simple_sitemap_views\SimpleSitemapViews;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Database\Database;
use Drupal\Core\Queue\QueueWorkerBase;
class GarbageCollector extends QueueWorkerBase implements ContainerFactoryPluginInterface {
protected $viewStorage;
protected $sitemapViews;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, SimpleSitemapViews $sitemap_views) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->viewStorage = $entity_type_manager
->getStorage('view');
$this->sitemapViews = $sitemap_views;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('simple_sitemap.views'));
}
public function processItem($data) {
$view_id = $data['view_id'];
$view_entity = $this->viewStorage
->load($view_id);
$display_ids = [];
if ($view_entity && $view_entity
->status()) {
$view = $view_entity
->getExecutable();
foreach ($this->sitemapViews
->getRouterDisplayIds($view_entity) as $display_id) {
if (!$view
->setDisplay($display_id) || !$view->display_handler
->isEnabled()) {
continue;
}
$variants = $this->sitemapViews
->getIndexableVariants($view);
$variants = array_keys($variants);
$args_ids = [];
foreach ($variants as $variant) {
$variant_args_ids = $this->sitemapViews
->getIndexableArguments($view, $variant);
if (count($variant_args_ids) > count($args_ids)) {
$args_ids = $variant_args_ids;
}
}
if (empty($args_ids)) {
continue;
}
$display_ids[] = $display_id;
$args_ids = $this->sitemapViews
->getArgumentsStringVariations($args_ids);
$condition = Database::getConnection()
->condition('AND');
$condition
->condition('view_id', $view_id);
$condition
->condition('display_id', $display_id);
$condition
->condition('arguments_ids', $args_ids, 'NOT IN');
$this->sitemapViews
->removeArgumentsFromIndex($condition);
$max_links = 0;
foreach ($variants as $variant) {
$settings = $this->sitemapViews
->getSitemapSettings($view, $variant);
$variant_max_links = is_numeric($settings['max_links']) ? $settings['max_links'] : 0;
if ($variant_max_links == 0) {
$max_links = 0;
break;
}
if ($variant_max_links > $max_links) {
$max_links = $variant_max_links;
}
}
if ($max_links > 0) {
$condition = Database::getConnection()
->condition('AND');
$condition
->condition('view_id', $view_id);
$condition
->condition('display_id', $display_id);
if ($index_id = $this->sitemapViews
->getIndexIdByPosition($max_links, $condition)) {
$condition
->condition('id', $index_id, '>');
$this->sitemapViews
->removeArgumentsFromIndex($condition);
}
}
}
if (!empty($display_ids)) {
$condition = Database::getConnection()
->condition('AND');
$condition
->condition('view_id', $view_id);
$condition
->condition('display_id', $display_ids, 'NOT IN');
$this->sitemapViews
->removeArgumentsFromIndex($condition);
}
$view
->destroy();
}
if (empty($display_ids)) {
$condition = Database::getConnection()
->condition('AND');
$condition
->condition('view_id', $view_id);
$this->sitemapViews
->removeArgumentsFromIndex($condition);
}
}
}