class ArgumentCollector in Simple XML sitemap 4.x
Same name and namespace in other branches
- 8.3 modules/simple_sitemap_views/src/EventSubscriber/ArgumentCollector.php \Drupal\simple_sitemap_views\EventSubscriber\ArgumentCollector
Collect information about views arguments.
Hierarchy
- class \Drupal\simple_sitemap_views\EventSubscriber\ArgumentCollector implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expanded class hierarchy of ArgumentCollector
1 string reference to 'ArgumentCollector'
- simple_sitemap_views.services.yml in modules/
simple_sitemap_views/ simple_sitemap_views.services.yml - modules/simple_sitemap_views/simple_sitemap_views.services.yml
1 service uses ArgumentCollector
- simple_sitemap.views.argument_collector in modules/
simple_sitemap_views/ simple_sitemap_views.services.yml - Drupal\simple_sitemap_views\EventSubscriber\ArgumentCollector
File
- modules/
simple_sitemap_views/ src/ EventSubscriber/ ArgumentCollector.php, line 15
Namespace
Drupal\simple_sitemap_views\EventSubscriberView source
class ArgumentCollector implements EventSubscriberInterface {
/**
* View entities storage.
*
* @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface
*/
protected $viewStorage;
/**
* Views sitemap data.
*
* @var \Drupal\simple_sitemap_views\SimpleSitemapViews
*/
protected $sitemapViews;
/**
* The currently active route match object.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $routeMatch;
/**
* ArgumentCollector constructor.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* Entity type manager.
* @param \Drupal\simple_sitemap_views\SimpleSitemapViews $sitemap_views
* Views sitemap data.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The currently active route match object.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, SimpleSitemapViews $sitemap_views, RouteMatchInterface $route_match) {
$this->viewStorage = $entity_type_manager
->getStorage('view');
$this->sitemapViews = $sitemap_views;
$this->routeMatch = $route_match;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[KernelEvents::TERMINATE] = 'onTerminate';
return $events;
}
/**
* Collect information about views arguments.
*
* @param \Symfony\Component\HttpKernel\Event\PostResponseEvent $event
* Object of event after a response was sent.
*/
public function onTerminate(PostResponseEvent $event) {
// Only successful requests are interesting.
// Collect information about arguments only if views support is enabled.
if (!$event
->getResponse()
->isSuccessful() || !$this->sitemapViews
->isEnabled()) {
return;
}
$view_id = $this->routeMatch
->getParameter('view_id');
/** @var \Drupal\views\ViewEntityInterface $view_entity */
if ($view_id && ($view_entity = $this->viewStorage
->load($view_id))) {
$display_id = $this->routeMatch
->getParameter('display_id');
// Get a set of view arguments and try to add them to the index.
$view = $view_entity
->getExecutable();
$args = $this
->getViewArgumentsFromRoute();
$this->sitemapViews
->addArgumentsToIndex($view, $args, $display_id);
// Destroy a view instance.
$view
->destroy();
}
}
/**
* Get view arguments from current route.
*
* @return array
* View arguments array.
*/
protected function getViewArgumentsFromRoute() {
// The code of this function is taken in part from the view page controller
// method (Drupal\views\Routing\ViewPageController::handle()).
$route = $this->routeMatch
->getRouteObject();
$map = $route
->hasOption('_view_argument_map') ? $route
->getOption('_view_argument_map') : [];
$args = [];
foreach ($map as $attribute => $parameter_name) {
$parameter_name = $parameter_name ?? $attribute;
$arg = $this->routeMatch
->getRawParameter($parameter_name);
if ($arg !== NULL) {
$args[] = $arg;
}
}
return $args;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ArgumentCollector:: |
protected | property | The currently active route match object. | |
ArgumentCollector:: |
protected | property | Views sitemap data. | |
ArgumentCollector:: |
protected | property | View entities storage. | |
ArgumentCollector:: |
public static | function | ||
ArgumentCollector:: |
protected | function | Get view arguments from current route. | |
ArgumentCollector:: |
public | function | Collect information about views arguments. | |
ArgumentCollector:: |
public | function | ArgumentCollector constructor. |