Skip to content

Instantly share code, notes, and snippets.

@bserem
Created May 10, 2022 11:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bserem/a3c13e2efa07cf0492c0c2a55c29959c to your computer and use it in GitHub Desktop.
Save bserem/a3c13e2efa07cf0492c0c2a55c29959c to your computer and use it in GitHub Desktop.
Conditional no results text based on exposed filter values (bypass drupal core bug related to https://www.drupal.org/project/drupal/issues/3024467)
<?php
/**
* Implements hook_views_pre_execute().
*/
function HOOK_views_pre_execute(ViewExecutable $view) {
if (empty($view->exposed_raw_input['title']) && ($view->query->getPluginId() === "search_api_query")) {
$view->query->abort();
}
}
/**
* Implements hook_views_pre_render().
*/
function HOOK_views_pre_render(ViewExecutable $view) {
if (empty($view->exposed_raw_input['title']) && ($view->query->getPluginId() === "search_api_query")) {
$view->attachment_after = array('#markup' => t('Select any filter and click on Apply to see results'));
}
else if (empty($view->result)) {
$view->attachment_after = array('#markup' => t('Your search has no results'));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment