How do I programmatically set contextual filters?


Contextual filters use query string params. You could hard code these into the URL. If using a Url object, you can pass a third argument ['query' => ['param1 => 'param1_value',...]]. Try setting a filter and seeing what the URL param name & value is after doing so.

In hook_views_pre_build(), you can also set the filter value directly:

$view->filter['filter_name']->value = 'filter_value';

It's helpful to have Xdebug so you can see the names of existing filters. If you don't have xdebug, try finding the correct filter name in the ->filter property, then find the desired filter value, and put 2 and 2 together.

Tags: Views / Drupal 8

Similar questions

How do I programmatically render a view with contextual filters?
How do I programmatically render a view with contextual filters in Drupal 8? In Drupal 7, I generally use the following code. Is there any alternative way to achieve this in Drupal 8?
Views contextual filters programmatically alter add to or
I have 2 fields in my contextual filters. Obviously, the default join is AND but I'd like to filter on one or the other field depending on which one of two arguments is provided. I tried the following approach: However, using the devel module to inspect the Views data, the type field in the where section of my View still outputs AND instead of OR. ...
How do I programmatically change contextual filters?
Basing on a blog post, I wrote the following code. I cleared the cache, but the code I wrote doesn't have any effect. The SQL query is not altered; only single taxonomy ID is returned. I have selected Allow multiple values for the filter. What is wrong with the code? How can I fix it?
How do I set my views to return a 404 using Global: Null under contextual filters?
Standard Drupal behavior is to pass all arguments through, whether or not they requested. Sometimes this behavior is desired. However, I need to return a 404 for anything but a query string. Example of the problem <- This should return a 404; 200 given. Duplicate cont...
Redirect to main view URL when no contextual filters are set
I have a view in drupal 7 with no contextual filters at all set. Let's say it is a simple list of nodes of the type "item" and that the url is I also have pathauto patterns set for the individual nodes. So if an item titled "my item" is created the url would be This is important ...
unable to set CONTEXTUAL FILTERS according to Raw Value from Url
In my project having two type of Content Type 1) Artist and 2) Work I have added the content for the Artist and now also i have added the content for the Work according to Artist Name having field name (Artist field_artist Entity reference) On view Page i want to show the result according to the artist According to name robert i have set the CONTEX...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.