1

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?

6
  • 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, 2019 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. May 22, 2019 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, 2019 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. May 23, 2019 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, 2019 at 22:27

1 Answer 1

1

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)) {
      $view1->setDisplay('page_1');
      $view2->setDisplay('attachment_1');
      $view3->setDisplay('attachment_2');
      $view4->setDisplay('attachment_3');

      $view1->execute();
      $view2->execute();
      $view3->execute();
      $view4->execute();

      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.";
      }
    }
  }
}
2
  • 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, 2019 at 6:01
  • Hmmm this doesn't seem to be working. $view->name doens't appear to return anything. May 29, 2019 at 0:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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