View source
<?php
namespace Drupal\simple_sitemap_views\Plugin\simple_sitemap\UrlGenerator;
use Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator\EntityUrlGeneratorBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\simple_sitemap_views\SimpleSitemapViews;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\RouteProviderInterface;
use Drupal\Core\Database\Database;
use Drupal\simple_sitemap\Simplesitemap;
use Drupal\simple_sitemap\EntityHelper;
use Drupal\simple_sitemap\Logger;
use Drupal\views\Views;
use Drupal\Core\Url;
class ViewsUrlGenerator extends EntityUrlGeneratorBase {
protected $sitemapViews;
protected $routeProvider;
public function __construct(array $configuration, $plugin_id, $plugin_definition, Simplesitemap $generator, Logger $logger, LanguageManagerInterface $language_manager, EntityTypeManagerInterface $entity_type_manager, EntityHelper $entity_helper, SimpleSitemapViews $sitemap_views, RouteProviderInterface $route_provider) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $generator, $logger, $language_manager, $entity_type_manager, $entity_helper);
$this->sitemapViews = $sitemap_views;
$this->routeProvider = $route_provider;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('simple_sitemap.generator'), $container
->get('simple_sitemap.logger'), $container
->get('language_manager'), $container
->get('entity_type.manager'), $container
->get('simple_sitemap.entity_helper'), $container
->get('simple_sitemap.views'), $container
->get('router.route_provider'));
}
public function getDataSets() {
$data_sets = [];
foreach ($this->sitemapViews
->getIndexableViews() as $view) {
$settings = $this->sitemapViews
->getSitemapSettings($view, $this->sitemapVariant);
if (empty($settings)) {
$view
->destroy();
continue;
}
$base_data_set = [
'view_id' => $view
->id(),
'display_id' => $view->current_display,
];
$extender = $this->sitemapViews
->getDisplayExtender($view);
if (!$extender
->hasRequiredArguments()) {
$data_sets[] = $base_data_set + [
'arguments' => NULL,
];
}
if ($args_ids = $this->sitemapViews
->getIndexableArguments($view, $this->sitemapVariant)) {
$args_ids = $this->sitemapViews
->getArgumentsStringVariations($args_ids);
$condition = Database::getConnection()
->condition('AND');
$condition
->condition('view_id', $view
->id());
$condition
->condition('display_id', $view->current_display);
$condition
->condition('arguments_ids', $args_ids, 'IN');
$max_links = is_numeric($settings['max_links']) ? $settings['max_links'] : NULL;
$indexed_arguments = $this->sitemapViews
->getArgumentsFromIndex($condition, $max_links, TRUE);
foreach ($indexed_arguments as $index_id => $arguments_info) {
$data_sets[] = $base_data_set + [
'index_id' => $index_id,
'arguments' => $arguments_info['arguments'],
];
}
}
$view
->destroy();
}
return $data_sets;
}
protected function processDataSet($data_set) {
$view_id = $data_set['view_id'];
$display_id = $data_set['display_id'];
$args = $data_set['arguments'];
try {
$view = Views::getView($view_id);
if (empty($view)) {
throw new \UnexpectedValueException('Failed to get an instance of the view.');
}
$view
->initDisplay();
if (!$view->displayHandlers
->has($display_id) || !$view
->setDisplay($display_id)) {
throw new \UnexpectedValueException('Failed to set the view display.');
}
$settings = $this->sitemapViews
->getSitemapSettings($view, $this->sitemapVariant);
if (empty($settings)) {
throw new \UnexpectedValueException('Failed to get the sitemap settings.');
}
$url = $view
->getUrl($args);
$url
->setAbsolute();
if (is_array($args)) {
$params = array_merge([
$view_id,
$display_id,
], $args);
$view_result = call_user_func_array('views_get_view_result', $params);
if (empty($view_result)) {
throw new \UnexpectedValueException('The view returned an empty result.');
}
$this
->cleanRouteParameters($url, $args);
}
$path = $url
->getInternalPath();
$view
->destroy();
} catch (\Exception $e) {
if (!empty($data_set['index_id'])) {
$condition = Database::getConnection()
->condition('AND');
$condition
->condition('id', $data_set['index_id']);
$this->sitemapViews
->removeArgumentsFromIndex($condition);
}
return FALSE;
}
return [
'url' => $url,
'lastmod' => NULL,
'priority' => isset($settings['priority']) ? $settings['priority'] : NULL,
'changefreq' => !empty($settings['changefreq']) ? $settings['changefreq'] : NULL,
'images' => [],
'meta' => [
'path' => $path,
'view_info' => [
'view_id' => $view_id,
'display_id' => $display_id,
'arguments' => $args,
],
],
];
}
protected function cleanRouteParameters(Url $url, array $args) {
$parameters = $url
->getRouteParameters();
if (count($parameters) != count($args)) {
$route_name = $url
->getRouteName();
$route = $this->routeProvider
->getRouteByName($route_name);
$variables = $route
->compile()
->getVariables();
foreach ($variables as $variable_name) {
if (empty($args)) {
unset($parameters[$variable_name]);
}
else {
array_shift($args);
}
}
$url
->setRouteParameters($parameters);
}
}
}