How do I set the default value for results? I'm on Drupal 9 and I have an AJAX view with a page display and an exposed filter block and a single filter exposed, &quot;Search: Fulltext search&quot;.
The Search API module is configured to search inside 3 different content types: products, articles and authors.
I've also configured a facet to filter on the content type, using a widget type &quot;links&quot;, that let filter one content type at time.
So, initially, the search page shows the results of all the three content types.
After users filter a content type, they can switch only to another one and never return to the no content type chosen situation.
What I need to do is that the results are listed by default with one specific content type. If that content type yield no results, the following content type should be used as filter.
If there are no result, just no result should be shown.
In other words, the no content type chosen situation should be never available.
I've tried to force the filter on content type, if missing, using hook_views_pre_view(), but it doesn't seem to work.
Actually, I've no idea if it is the correct approach or I have to work on different hook\somewhere else.
How do I solve this issue?
This is the hook code.
function my_module_views_pre_view(\Drupal\views\ViewExecutable $view, $display_id, array &amp;$args) {
  if ($view-&gt;id() === 'search' &amp;&amp; $display_id === 'page_1') {
    $active_filters = $view-&gt;getExposedInput();
    if (isset($active_filters[&quot;search_api_fulltext&quot;]) &amp;&amp; !empty($active_filters[&quot;search_api_fulltext&quot;])) {
      if (!isset($active_filters[&quot;f&quot;])) {
        $active_filters[&quot;f&quot;] = ['content_type:product'];
      }
      else {
        $content_type_filter_exists = FALSE;
        foreach ($active_filters[&quot;f&quot;] as $facet_filter) {
          $filter_array = explode(':', $facet_filter);
          if ($filter_array[0] === 'content_type') {
            $content_type_filter_exists = TRUE;
            break;
          }
        }
        if (!$content_type_filter_exists) {
          $active_filters[&quot;f&quot;] = array_merge($active_filters[&quot;f&quot;], ['content_type:product']);
        }
      }
      $view-&gt;setExposedInput($active_filters);
    }
  }
  //$view-&gt;exposed_raw_input = array_merge($active_filters, (array) $view-&gt;exposed_raw_input);
}
Initially I tried some different approach:

extending SearchApiString
an event subscriber of KernelEvents::REQUEST altering the incoming url

because by default facets\search api rely on the query_params of the url and the &quot;values&quot; of the relative facets various objects of the modules.
Both of those those approaches kinda failed, because the first didn't update the query params of the request, while the second caused some bugs that I couldn't manage to solve.
The working approach was to extending the default url_processor QueryString - which both initialises the active facets filters and build the url after a search.
Injecting the logic when the class is created allowed to do a minor modification without breaking all the standard functionality.
&lt;?php

namespace Drupal\my_module\Plugin\facets\url_processor;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\facets\Plugin\facets\url_processor\QueryString;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;

/**
 * Query string URL processor.
 *
 * @FacetsUrlProcessor(
 *   id = &quot;my_query_string&quot;,
 *   label = @Translation(&quot;My Query string&quot;),
 *   description = @Translation(&quot;Add description&quot;)
 * )
 */
class MyQueryString extends QueryString {

  /**
   * A string of how to represent the facet in the url.
   *
   * @var string
   */
  protected $urlAlias;

  /**
   * The event dispatcher.
   *
   * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
   */
  protected $eventDispatcher;

  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, Request $request, EntityTypeManagerInterface $entity_type_manager, EventDispatcherInterface $eventDispatcher) {
    //this is where I'm adding a default value
    $this-&gt;alterRequest($request);
    parent::__construct($configuration, $plugin_id, $plugin_definition, $request, $entity_type_manager, $eventDispatcher);
  }

  /**
   * @param \Symfony\Component\HttpFoundation\Request $request
   */
  private function alterRequest(Request $request): void {
    $query = $request-&gt;query;
    $facet_new_parameters = [];

    if ($facet_parameters = $query-&gt;get('f')) {
      $content_type_filter_exists = FALSE;

      foreach ($facet_parameters as $parameter) {
        $sepator = ':';
        $explosion = explode($sepator, $parameter);
        $facet_type = array_shift($explosion);
        if ($facet_type === 'content_type') {
          $content_type_filter_exists = TRUE;
        }
      }

      if (!$content_type_filter_exists) {
        $facet_new_parameters = array_merge($facet_parameters, ['content_type:prodotto']);
      }
    }
    else {
      $facet_new_parameters = ['content_type:prodotto'];
    }

    if (!empty($facet_new_parameters)) {
      $query-&gt;set('f', $facet_new_parameters);
    }
  }
}