ExampleViewsEventSubscribers.php in Hook Event Dispatcher 3.x
File
examples/ExampleViewsEventSubscribers.php
View source
<?php
namespace Drupal\hook_event_dispatcher;
use Drupal\views_event_dispatcher\Event\Views\ViewsPostBuildEvent;
use Drupal\views_event_dispatcher\Event\Views\ViewsPostExecuteEvent;
use Drupal\views_event_dispatcher\Event\Views\ViewsPostRenderEvent;
use Drupal\views_event_dispatcher\Event\Views\ViewsPreBuildEvent;
use Drupal\views_event_dispatcher\Event\Views\ViewsPreExecuteEvent;
use Drupal\views_event_dispatcher\Event\Views\ViewsPreRenderEvent;
use Drupal\views_event_dispatcher\Event\Views\ViewsPreViewEvent;
use Drupal\views_event_dispatcher\Event\Views\ViewsQueryAlterEvent;
use Drupal\views_event_dispatcher\Event\Views\ViewsQuerySubstitutionsEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ExampleViewsEventSubscribers implements EventSubscriberInterface {
public function preView(ViewsPreViewEvent $event) : void {
$args =& $event
->getArgs();
$args[0] = 'custom value';
}
public function preBuild(ViewsPreBuildEvent $event) : void {
$view = $event
->getView();
$view
->setArguments([
'test',
]);
}
public function queryAlter(ViewsQueryAlterEvent $event) : void {
$query = $event
->getQuery();
$query
->setLimit(10);
}
public function querySubstitutions(ViewsQuerySubstitutionsEvent $event) : void {
$event
->addSubstitution('***CURRENT_TIME***', \Drupal::time()
->getRequestTime());
}
public function postBuild(ViewsPostBuildEvent $event) : void {
$view = $event
->getView();
$view->build_info;
}
public function preExecute(ViewsPreExecuteEvent $event) : void {
$view = $event
->getView();
$view
->setArguments([
'test',
]);
}
public function postExecute(ViewsPostExecuteEvent $event) : void {
$view = $event
->getView();
$view->build_info;
}
public function preRender(ViewsPreRenderEvent $event) : void {
$view = $event
->getView();
$view
->setArguments([
'test',
]);
}
public function postRender(ViewsPostRenderEvent $event) : void {
$cache = $event
->getCache();
$cache->options['results_lifespan'] = 0;
}
public static function getSubscribedEvents() : array {
return [
HookEventDispatcherInterface::VIEWS_PRE_VIEW => 'preView',
HookEventDispatcherInterface::VIEWS_PRE_BUILD => 'preBuild',
HookEventDispatcherInterface::VIEWS_QUERY_ALTER => 'queryAlter',
HookEventDispatcherInterface::VIEWS_QUERY_SUBSTITUTIONS => 'querySubstitutions',
HookEventDispatcherInterface::VIEWS_POST_BUILD => 'postBuild',
HookEventDispatcherInterface::VIEWS_PRE_EXECUTE => 'preExecute',
HookEventDispatcherInterface::VIEWS_POST_EXECUTE => 'postExecute',
HookEventDispatcherInterface::VIEWS_PRE_RENDER => 'preRender',
HookEventDispatcherInterface::VIEWS_POST_RENDER => 'postRender',
];
}
}