- Drupal 8 Development Cookbook(Second Edition)
- Matt Glaman
- 183字
- 2025-04-04 18:20:51
Validation
Contextual filters can have validation attached. Without specifying extra validation, Views will take the expected argument and try to make it just work. You can add validation to help limit this scope and filter out invalid route variables.
You can enable validation by checking Specify validation criteria from the When the filter value is present in the URL or a default is provided section. The default is set to Basic Validation, which allows you to specify how the view should react if the data is invalid; based on our recipe, this would be if the user is not found.
The list of Validator options is not filtered by the contextual filter item you selected, so some may not apply. For our recipe, one might want User ID and select the Validate user has access to the User. This validator would make sure that the current user is able to view the route's user's profile. Additionally, it can be restricted further based on its role:

This gives you more granular control over how the view operates when using contextual filters for route arguments.