Skip to main content
drupalreleases

Use direct release data with your AI assistant.

Learn more

Views Hooks Extras

Security covered Under active development 6 sites

Part of the Views ecosystem · 280 projects

This module provides more specific ways to hook into Views functionality, allowing developers to easily target individual views and their displays. This helps to reduce the need for lengthy conditional logic within existing hooks.

This module extends Views' native hooks to more specifically target particular views and their displays -- basically hook_form_FORM_ID_alter() for views hooks. This can help to avoid function calls with tons of conditional logic just to delineate which views are being targeted.

TL;DR:

Before: (╯°□°)╯︵ ┻━┻

function mymodule_views_pre_render(ViewExecutable $view) {
  switch ($view->id()) {
    case 'product_index':
      do_thing_1();
      if ($view->current_display === 'page_1') {
        do_thing_2();    
      }
       break;
    case 'users_list':
      do_thing_3();
      break;
  }
}

After: ┬─┬ノ( º _ ºノ)

function mymodule_extra_views_product_index_pre_render(ViewExecutable $view) {
  do_thing_1();
}

function mymodule_extra_views_product_index__page_1_pre_render(ViewExecutable $view) {
  do_thing_2();
}

function mymodule_extra_views_users_list_pre_render(ViewExecutable $view) {
  do_thing_3();
}

=======

Hooks take the format of:

  • hook_extra_viewsPrefix_VIEW_ID_viewsSuffix
  • hook_extra_viewsPrefix_VIEW_ID__DISPLAY_ID_viewsSuffix

Note that the DISPLAY_ID portion takes on the snake case version of the actual display ID. That is to say, if you happened to name your view display to be page-1, the hook would need to use page_1.

At time of writing, the following views hooks are currently supported:

  • hook_views_query_substitutions
  • hook_views_pre_view
  • hook_views_pre_build
  • hook_views_post_build
  • hook_views_pre_execute
  • hook_views_pre_render
  • hook_views_post_render
  • hook_views_query_alter
  • hook_views_preview_info_alter

Logic already exists to support the field_views and plugin hooks, but we aren't sure whether there's a way to parse a particular view or display ID from the available parameters. Feel free to open an issue if you have a solution for any of these hooks.

Shameless Self Promotion

A list of modules that I maintain:

  • Cache Register: A module aimed at improving developer quality of life surrounding caching.
  • Enum Generator: A developer utility that generates enums and class constants from a site's entities.
  • Views Hooks Extras: Extends Views' native hooks to more specifically target particular views and their displays

Image Credit

Cute fishing lure photo by Karolina Grabowska from Pexels.

Drupal 8–11

Ask your assistant about Views Hooks Extras

Check compatibility with your Drupal and PHP version, maintenance and security coverage, from current release data. How it works

Depends on

Dependencies of the latest stable release

Required by

Tracked projects that depend on this one

No tracked projects depend on this one yet.

Activity

Tracked releases
2
Tracked since
Sep 2023
Latest release
8 months ago
Releases (12 mo)
1 ▲ from 0
Maintenance
Active

Releases

Version Type Core PHP Release date
1.0.0 Stable 8–11 Jan 26, 2026
1.0.0-alpha3 Pre-release 8–10 Sep 7, 2023