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_viewsSuffixhook_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.