You are here

class ExampleViewsEventSubscribers in Hook Event Dispatcher 8

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\Example\ExampleViewsEventSubscribers' tags:

  • { name: 'event_subscriber' }

Hierarchy

  • class \Drupal\hook_event_dispatcher\Example\ExampleViewsEventSubscribers implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of ExampleViewsEventSubscribers

File

src/Example/ExampleViewsEventSubscribers.php, line 29

Namespace

Drupal\hook_event_dispatcher\Example
View source
class ExampleViewsEventSubscribers implements EventSubscriberInterface {

  /**
   * Pre view event handler.
   *
   * @param \Drupal\hook_event_dispatcher\Event\Views\ViewsPreViewEvent $event
   *   The event.
   */
  public function preView(ViewsPreViewEvent $event) {
    $args =& $event
      ->getArgs();

    // Do something with the arguments.
    $args[0] = 'custom value';
  }

  /**
   * Pre build event handler.
   *
   * @param \Drupal\hook_event_dispatcher\Event\Views\ViewsPreBuildEvent $event
   *   The event.
   */
  public function preBuild(ViewsPreBuildEvent $event) {
    $view = $event
      ->getView();

    // Do something with the view.
    $view
      ->setArguments([
      'test',
    ]);
  }

  /**
   * Query alter event handler.
   *
   * @param \Drupal\hook_event_dispatcher\Event\Views\ViewsQueryAlterEvent $event
   *   The event.
   */
  public function queryAlter(ViewsQueryAlterEvent $event) {
    $query = $event
      ->getQuery();

    // Do something with the query.
    $query
      ->setLimit(10);
  }

  /**
   * Query substitutions event handler.
   *
   * @param \Drupal\hook_event_dispatcher\Event\Views\ViewsQuerySubstitutionsEvent $event
   *   The event.
   */
  public function querySubstitutions(ViewsQuerySubstitutionsEvent $event) {
    $event
      ->setSubstitutions([
      '***CURRENT_TIME***' => \Drupal::time()
        ->getRequestTime(),
    ]);
  }

  /**
   * Post build event handler.
   *
   * @param \Drupal\hook_event_dispatcher\Event\Views\ViewsPostBuildEvent $event
   *   The event.
   */
  public function postBuild(ViewsPostBuildEvent $event) {
    $view = $event
      ->getView();

    // Do something with the view.
    $view->build_info;
  }

  /**
   * Pre execute event handler.
   *
   * @param \Drupal\hook_event_dispatcher\Event\Views\ViewsPreExecuteEvent $event
   *   The event.
   */
  public function preExecute(ViewsPreExecuteEvent $event) {
    $view = $event
      ->getView();

    // Do something with the view.
    $view
      ->setArguments([
      'test',
    ]);
  }

  /**
   * Post execute event handler.
   *
   * @param \Drupal\hook_event_dispatcher\Event\Views\ViewsPostExecuteEvent $event
   *   The event.
   */
  public function postExecute(ViewsPostExecuteEvent $event) {
    $view = $event
      ->getView();

    // Do something with the view.
    $view->build_info;
  }

  /**
   * Pre render event handler.
   *
   * @param \Drupal\hook_event_dispatcher\Event\Views\ViewsPreRenderEvent $event
   *   The event.
   */
  public function preRender(ViewsPreRenderEvent $event) {
    $view = $event
      ->getView();

    // Do something with the view.
    $view
      ->setArguments([
      'test',
    ]);
  }

  /**
   * Post render event handler.
   *
   * @param \Drupal\hook_event_dispatcher\Event\Views\ViewsPostRenderEvent $event
   *   The event.
   */
  public function postRender(ViewsPostRenderEvent $event) {
    $cache = $event
      ->getCache();

    // Do something with the cache settings.
    $cache->options['results_lifespan'] = 0;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {

    // In order of execution.
    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

Namesort descending Modifiers Type Description Overrides
ExampleViewsEventSubscribers::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
ExampleViewsEventSubscribers::postBuild public function Post build event handler.
ExampleViewsEventSubscribers::postExecute public function Post execute event handler.
ExampleViewsEventSubscribers::postRender public function Post render event handler.
ExampleViewsEventSubscribers::preBuild public function Pre build event handler.
ExampleViewsEventSubscribers::preExecute public function Pre execute event handler.
ExampleViewsEventSubscribers::preRender public function Pre render event handler.
ExampleViewsEventSubscribers::preView public function Pre view event handler.
ExampleViewsEventSubscribers::queryAlter public function Query alter event handler.
ExampleViewsEventSubscribers::querySubstitutions public function Query substitutions event handler.