- π©π°Denmark Steven Snedker
Well, SomebodySysop. I'm sorry that noone gave you a good answer in time and that the documentation for Drupal 9 is so awful. I try do document my own modules well, at least.
Anyway, here how I solved programmatically adding a filter to a view in Drupal 9.5. It may solve a problem for you in the future or bring some help to all the other lost developers looking for useful documentation and working examples.
I made a view β (the format is Full Calender Display, but that doesn't matter) showing the jobs assigned to a specific car on a specific day.My wish was to programmatically load and show a view block 10 times - once for each car on that day.
So in the views UI I added a filter for field_car_term set to 11.
Then I made this code:
function home() { $terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree('cars'); // Get all terms from vocabulary foreach ($terms as $term) { // Loop through them $view = Views::getView('job_calendar'); $view->setDisplay('block_1'); unset($view->display_handler->options['filters']['field_car_term_target_id']['value']['11']); // Unset the hardcoded value $view->display_handler->options['filters']['field_car_term_target_id']['value'][$term->tid] = $term->tid; // Set the dynamic value $ret[$term->tid]['cal'] = $view->buildRenderable(); // Add the render array of the view to an array } return $ret; // Return render array }
The filter has to exist in the view. Otherwise the above code will not work.
The filter should be hardcoded to something that you later unset. Here it's term id 11.
Setting the filter, you have a surprising amount of freedom
$view->display_handler->options['filters']['field_car_term_target_id']['value'][$term->tid] = $term->tid; $view->display_handler->options['filters']['field_car_term_target_id']['value'][3] = $term->tid; $view->display_handler->options['filters']['field_car_term_target_id']['value']['drupalisawfullydocmented'] = $term->tid;
all work well.
The saner looking
$view->display_handler->options['filters']['field_car_term_target_id']['value'] = $term->tid;
does not work whatsoever. So avoid that.
I'd be thrilled if there was a better place to read about how to programatically add filters to a view. A place with more and better examples. But alas. Looking for hours I did not find such a place.