class ExampleViewsEventSubscribers in Hook Event Dispatcher 3.x
Same name and namespace in other branches
- 8.2 examples/ExampleViewsEventSubscribers.php \Drupal\hook_event_dispatcher\ExampleViewsEventSubscribers
Class ExampleViewsEventSubscribers.
Don't forget to define your class as a service and tag it as an "event_subscriber":
services: hook_event_dispatcher.example_views_subscribers: class: Drupal\hook_event_dispatcher\ExampleViewsEventSubscribers tags:
- { name: event_subscriber }
Hierarchy
- class \Drupal\hook_event_dispatcher\ExampleViewsEventSubscribers implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expanded class hierarchy of ExampleViewsEventSubscribers
File
- examples/
ExampleViewsEventSubscribers.php, line 28
Namespace
Drupal\hook_event_dispatcherView source
class ExampleViewsEventSubscribers implements EventSubscriberInterface {
/**
* Pre view event handler.
*
* @param \Drupal\views_event_dispatcher\Event\Views\ViewsPreViewEvent $event
* The event.
*/
public function preView(ViewsPreViewEvent $event) : void {
$args =& $event
->getArgs();
// Do something with the arguments.
$args[0] = 'custom value';
}
/**
* Pre build event handler.
*
* @param \Drupal\views_event_dispatcher\Event\Views\ViewsPreBuildEvent $event
* The event.
*/
public function preBuild(ViewsPreBuildEvent $event) : void {
$view = $event
->getView();
// Do something with the view.
$view
->setArguments([
'test',
]);
}
/**
* Query alter event handler.
*
* @param \Drupal\views_event_dispatcher\Event\Views\ViewsQueryAlterEvent $event
* The event.
*/
public function queryAlter(ViewsQueryAlterEvent $event) : void {
$query = $event
->getQuery();
// Do something with the query.
$query
->setLimit(10);
}
/**
* Query substitutions event handler.
*
* @param \Drupal\views_event_dispatcher\Event\Views\ViewsQuerySubstitutionsEvent $event
* The event.
*/
public function querySubstitutions(ViewsQuerySubstitutionsEvent $event) : void {
$event
->addSubstitution('***CURRENT_TIME***', \Drupal::time()
->getRequestTime());
}
/**
* Post build event handler.
*
* @param \Drupal\views_event_dispatcher\Event\Views\ViewsPostBuildEvent $event
* The event.
*/
public function postBuild(ViewsPostBuildEvent $event) : void {
$view = $event
->getView();
// Do something with the view.
$view->build_info;
}
/**
* Pre execute event handler.
*
* @param \Drupal\views_event_dispatcher\Event\Views\ViewsPreExecuteEvent $event
* The event.
*/
public function preExecute(ViewsPreExecuteEvent $event) : void {
$view = $event
->getView();
// Do something with the view.
$view
->setArguments([
'test',
]);
}
/**
* Post execute event handler.
*
* @param \Drupal\views_event_dispatcher\Event\Views\ViewsPostExecuteEvent $event
* The event.
*/
public function postExecute(ViewsPostExecuteEvent $event) : void {
$view = $event
->getView();
// Do something with the view.
$view->build_info;
}
/**
* Pre render event handler.
*
* @param \Drupal\views_event_dispatcher\Event\Views\ViewsPreRenderEvent $event
* The event.
*/
public function preRender(ViewsPreRenderEvent $event) : void {
$view = $event
->getView();
// Do something with the view.
$view
->setArguments([
'test',
]);
}
/**
* Post render event handler.
*
* @param \Drupal\views_event_dispatcher\Event\Views\ViewsPostRenderEvent $event
* The event.
*/
public function postRender(ViewsPostRenderEvent $event) : void {
$cache = $event
->getCache();
// Do something with the cache settings.
$cache->options['results_lifespan'] = 0;
}
/**
* {@inheritdoc}
*/
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',
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ExampleViewsEventSubscribers:: |
public static | function | ||
ExampleViewsEventSubscribers:: |
public | function | Post build event handler. | |
ExampleViewsEventSubscribers:: |
public | function | Post execute event handler. | |
ExampleViewsEventSubscribers:: |
public | function | Post render event handler. | |
ExampleViewsEventSubscribers:: |
public | function | Pre build event handler. | |
ExampleViewsEventSubscribers:: |
public | function | Pre execute event handler. | |
ExampleViewsEventSubscribers:: |
public | function | Pre render event handler. | |
ExampleViewsEventSubscribers:: |
public | function | Pre view event handler. | |
ExampleViewsEventSubscribers:: |
public | function | Query alter event handler. | |
ExampleViewsEventSubscribers:: |
public | function | Query substitutions event handler. |