class ViewsUrlGenerator in Simple XML sitemap 8.3
Same name and namespace in other branches
- 4.x modules/simple_sitemap_views/src/Plugin/simple_sitemap/UrlGenerator/ViewsUrlGenerator.php \Drupal\simple_sitemap_views\Plugin\simple_sitemap\UrlGenerator\ViewsUrlGenerator
Views URL generator plugin.
Plugin annotation
@UrlGenerator(
id = "views",
label = @Translation("Views URL generator"),
description = @Translation("Generates URLs for views."),
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\simple_sitemap\Plugin\simple_sitemap\SimplesitemapPluginBase implements PluginInspectionInterface, ContainerFactoryPluginInterface
- class \Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator\UrlGeneratorBase implements UrlGeneratorInterface
- class \Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator\EntityUrlGeneratorBase
- class \Drupal\simple_sitemap_views\Plugin\simple_sitemap\UrlGenerator\ViewsUrlGenerator
- class \Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator\EntityUrlGeneratorBase
- class \Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator\UrlGeneratorBase implements UrlGeneratorInterface
- class \Drupal\simple_sitemap\Plugin\simple_sitemap\SimplesitemapPluginBase implements PluginInspectionInterface, ContainerFactoryPluginInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of ViewsUrlGenerator
File
- modules/
simple_sitemap_views/ src/ Plugin/ simple_sitemap/ UrlGenerator/ ViewsUrlGenerator.php, line 27
Namespace
Drupal\simple_sitemap_views\Plugin\simple_sitemap\UrlGeneratorView source
class ViewsUrlGenerator extends EntityUrlGeneratorBase {
/**
* Views sitemap data.
*
* @var \Drupal\simple_sitemap_views\SimpleSitemapViews
*/
protected $sitemapViews;
/**
* The route provider.
*
* @var \Drupal\Core\Routing\RouteProviderInterface
*/
protected $routeProvider;
/**
* ViewsUrlGenerator constructor.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\simple_sitemap\Simplesitemap $generator
* The simple_sitemap.generator service.
* @param \Drupal\simple_sitemap\Logger $logger
* The simple_sitemap.logger service.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\simple_sitemap\EntityHelper $entity_helper
* The simple_sitemap.entity_helper service.
* @param \Drupal\simple_sitemap_views\SimpleSitemapViews $sitemap_views
* Views sitemap data.
* @param \Drupal\Core\Routing\RouteProviderInterface $route_provider
* The route provider.
*/
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;
}
/**
* {@inheritdoc}
*/
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'));
}
/**
* {@inheritdoc}
*/
public function getDataSets() {
$data_sets = [];
// Get 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);
// View path without arguments.
if (!$extender
->hasRequiredArguments()) {
$data_sets[] = $base_data_set + [
'arguments' => NULL,
];
}
// Process indexed arguments.
if ($args_ids = $this->sitemapViews
->getIndexableArguments($view, $this->sitemapVariant)) {
$args_ids = $this->sitemapViews
->getArgumentsStringVariations($args_ids);
// Form the condition according to the variants of the
// indexable arguments.
$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');
// Get the arguments values from the index.
$max_links = is_numeric($settings['max_links']) ? $settings['max_links'] : NULL;
$indexed_arguments = $this->sitemapViews
->getArgumentsFromIndex($condition, $max_links, TRUE);
// Add the arguments values for processing.
foreach ($indexed_arguments as $index_id => $arguments_info) {
$data_sets[] = $base_data_set + [
'index_id' => $index_id,
'arguments' => $arguments_info['arguments'],
];
}
}
// Destroy a view instance.
$view
->destroy();
}
return $data_sets;
}
/**
* {@inheritdoc}
*/
protected function processDataSet($data_set) {
// Get information from data set.
$view_id = $data_set['view_id'];
$display_id = $data_set['display_id'];
$args = $data_set['arguments'];
try {
// Trying to get an instance of the view.
$view = Views::getView($view_id);
if (empty($view)) {
throw new \UnexpectedValueException('Failed to get an instance of the view.');
}
// Trying to set the view display.
$view
->initDisplay();
if (!$view->displayHandlers
->has($display_id) || !$view
->setDisplay($display_id)) {
throw new \UnexpectedValueException('Failed to set the view display.');
}
// Trying to get the sitemap settings.
$settings = $this->sitemapViews
->getSitemapSettings($view, $this->sitemapVariant);
if (empty($settings)) {
throw new \UnexpectedValueException('Failed to get the sitemap settings.');
}
// Trying to get the view URL.
$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);
// Do not include paths on which the view returns an empty result.
if (empty($view_result)) {
throw new \UnexpectedValueException('The view returned an empty result.');
}
// Remove empty arguments from URL.
$this
->cleanRouteParameters($url, $args);
}
$path = $url
->getInternalPath();
// Destroy a view instance.
$view
->destroy();
} catch (\Exception $e) {
// Delete records about arguments that are not added to the sitemap.
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' => [],
// Additional info useful in hooks.
'meta' => [
'path' => $path,
'view_info' => [
'view_id' => $view_id,
'display_id' => $display_id,
'arguments' => $args,
],
],
];
}
/**
* Clears the URL from parameters that are not present in the arguments.
*
* @param \Drupal\Core\Url $url
* The URL object.
* @param array $args
* Array of arguments.
*
* @throws \UnexpectedValueException.
* If this is a URI with no corresponding route.
*/
protected function cleanRouteParameters(Url $url, array $args) {
$parameters = $url
->getRouteParameters();
// Check that the number of params does not match the number of arguments.
if (count($parameters) != count($args)) {
$route_name = $url
->getRouteName();
$route = $this->routeProvider
->getRouteByName($route_name);
$variables = $route
->compile()
->getVariables();
// Remove params that are not present in the arguments.
foreach ($variables as $variable_name) {
if (empty($args)) {
unset($parameters[$variable_name]);
}
else {
array_shift($args);
}
}
// Set new route params.
$url
->setRouteParameters($parameters);
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
EntityUrlGeneratorBase:: |
protected | property | ||
EntityUrlGeneratorBase:: |
protected | property | ||
EntityUrlGeneratorBase:: |
protected | property | ||
EntityUrlGeneratorBase:: |
protected | property | ||
EntityUrlGeneratorBase:: |
protected | property | ||
EntityUrlGeneratorBase:: |
protected | property | ||
EntityUrlGeneratorBase:: |
public | function |
Overrides UrlGeneratorBase:: |
1 |
EntityUrlGeneratorBase:: |
protected | function | ||
EntityUrlGeneratorBase:: |
protected | function | ||
EntityUrlGeneratorBase:: |
protected | function | ||
EntityUrlGeneratorBase:: |
protected | function | ||
EntityUrlGeneratorBase:: |
protected | function | ||
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorBase:: |
protected | property | ||
UrlGeneratorBase:: |
protected | property | ||
UrlGeneratorBase:: |
protected | property | ||
UrlGeneratorBase:: |
protected | property | ||
UrlGeneratorBase:: |
protected | function | ||
UrlGeneratorBase:: |
public | function |
Overrides UrlGeneratorInterface:: |
|
UrlGeneratorBase:: |
public | function |
Overrides UrlGeneratorInterface:: |
|
ViewsUrlGenerator:: |
protected | property | The route provider. | |
ViewsUrlGenerator:: |
protected | property | Views sitemap data. | |
ViewsUrlGenerator:: |
protected | function | Clears the URL from parameters that are not present in the arguments. | |
ViewsUrlGenerator:: |
public static | function |
Creates an instance of the plugin. Overrides EntityUrlGeneratorBase:: |
|
ViewsUrlGenerator:: |
public | function |
Overrides UrlGeneratorBase:: |
|
ViewsUrlGenerator:: |
protected | function |
Overrides UrlGeneratorBase:: |
|
ViewsUrlGenerator:: |
public | function |
ViewsUrlGenerator constructor. Overrides EntityUrlGeneratorBase:: |