ExampleViewsEventSubscribers.php in Hook Event Dispatcher 8
File
src/Example/ExampleViewsEventSubscribers.php
View source
<?php
namespace Drupal\hook_event_dispatcher\Example;
use Drupal\hook_event_dispatcher\Event\Views\ViewsPostBuildEvent;
use Drupal\hook_event_dispatcher\Event\Views\ViewsPostExecuteEvent;
use Drupal\hook_event_dispatcher\Event\Views\ViewsPostRenderEvent;
use Drupal\hook_event_dispatcher\Event\Views\ViewsPreBuildEvent;
use Drupal\hook_event_dispatcher\Event\Views\ViewsPreExecuteEvent;
use Drupal\hook_event_dispatcher\Event\Views\ViewsPreRenderEvent;
use Drupal\hook_event_dispatcher\Event\Views\ViewsPreViewEvent;
use Drupal\hook_event_dispatcher\Event\Views\ViewsQueryAlterEvent;
use Drupal\hook_event_dispatcher\Event\Views\ViewsQuerySubstitutionsEvent;
use Drupal\hook_event_dispatcher\HookEventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ExampleViewsEventSubscribers implements EventSubscriberInterface {
public function preView(ViewsPreViewEvent $event) {
$args =& $event
->getArgs();
$args[0] = 'custom value';
}
public function preBuild(ViewsPreBuildEvent $event) {
$view = $event
->getView();
$view
->setArguments([
'test',
]);
}
public function queryAlter(ViewsQueryAlterEvent $event) {
$query = $event
->getQuery();
$query
->setLimit(10);
}
public function querySubstitutions(ViewsQuerySubstitutionsEvent $event) {
$event
->setSubstitutions([
'***CURRENT_TIME***' => \Drupal::time()
->getRequestTime(),
]);
}
public function postBuild(ViewsPostBuildEvent $event) {
$view = $event
->getView();
$view->build_info;
}
public function preExecute(ViewsPreExecuteEvent $event) {
$view = $event
->getView();
$view
->setArguments([
'test',
]);
}
public function postExecute(ViewsPostExecuteEvent $event) {
$view = $event
->getView();
$view->build_info;
}
public function preRender(ViewsPreRenderEvent $event) {
$view = $event
->getView();
$view
->setArguments([
'test',
]);
}
public function postRender(ViewsPostRenderEvent $event) {
$cache = $event
->getCache();
$cache->options['results_lifespan'] = 0;
}
public static function getSubscribedEvents() {
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',
];
}
}