hook_views_pre_view

hook_views_pre_view(ViewExecutable $view, $display_id, array &$args)

Alter a view at the very beginning of Views processing.

Output can be added to the view by setting $view->attachment_before and $view->attachment_after.

Parameters

\Drupal\views\ViewExecutable $view: The view object about to be processed.

string $display_id: The machine name of the active display.

array $args: An array of arguments passed into the view.

See also

\Drupal\views\ViewExecutable

Related topics

Hooks
Define functions that alter the behavior of Drupal core.

File

core/modules/views/views.api.php, line 699
Describes hooks and plugins provided by the Views module.

Code

1
2
3
4
5
6
7
8
9
function hook_views_pre_view(ViewExecutable $view, $display_id, array &$args) {
 
  // Modify contextual filters for my_special_view if user has 'my special permission'.
  $account = \Drupal::currentUser();
 
  if ($view->id() == 'my_special_view' && $account->hasPermission('my special permission') && $display_id == 'public_display') {
    $args[0] = 'custom value';
  }
}
doc_Drupal
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.