I have a Search API view that has 3 attachments. This is so I can display different content type results with unique layouts.

If the user searches something too specific, they'll see the No Results Behavior up to 4 times in a row. This looks bad.

How can I only display the No Results Behavior (custom textarea) when there are no results in the view AND its 3 attachments?

  • In edit View, under "Advanced" there is an option to customize the no results behaviour. Do this for each of your views. As each view should be treated separately evethough they are attached. – No Sssweat May 22 at 5:25
  • This isn't the issue. I'm already using the "No Results Behavior" feature. But since I have 4 attachments, the user will see up to 4 "NO RESULTS FOUND" messages. I only want them to see AT MOST one no results - even if all 4 views fail to return anything. – user1566957 May 22 at 18:24
  • Oh right sorry. You'll need to use hook_views_pre_render and perform a check if they all say no results and if yes then you override the output of the attached views and set them to null/nothing/empty. – No Sssweat May 22 at 21:19
  • Thanks for the tip. Can you give me a rough outline of what to do with that hook? I'm not sure how I can check it a view is empty with PHP. I also don't know how to take content from the hook and have it change the output of the view. – user1566957 May 23 at 2:13
  • There doesn't appear to be way, ran into dead ends. Not sure if it's a Views bug or just how it's suppose to be. I guess another way would be to hide the duplicates via custom JS/jQuery. – No Sssweat May 23 at 22:27

I solved it myself (that's a first and probably a last)!

I'm sure this isn't the best code but it works and I don't know enough PHP to clean it up.

I disabled all "No Results Behaviors" in the view settings. I'm using the hook_preprocess_views_view hook to count the number of results for each of my attachments. Only if all 4 === 0 will it print the correct empty message.

function MYTHEMENAME_preprocess_views_view(&$variables) {
  $view = $variables['view'];
  if ($view->name == 'search_results') {
    $view1 = $view2 = $view3 = $view4 = $view;

    if (is_object($view1) && is_object($view2) && is_object($view3) && is_object($view4)) {


      if (count($view1->result) === 0 && count($view2->result) === 0 && count($view3->result === 0 && count($view4->result) === 0)) {
        $variables['empty'] = "Sorry, but we couldn't find any results for your query. Try broadening your search terms.";
  • Improved your answer. Added a conditional, as you don't want this code to run on every single view that your site has. – No Sssweat May 24 at 6:01
  • Hmmm this doesn't seem to be working. $view->name doens't appear to return anything. – user1566957 May 29 at 0:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.