0

How can I put a condition on the Header in a View to not display the Header if the View has content?

There's an option to display a Header/Footer absent any content, but I'm looking for the opposite effect.

screen capture

3 Answers 3

1

To do this without using preprocess functions in template.php as @ Andrew said, Click on the add in header section and select Global: PHP and use this code

<?php
      if(count($results) > 0)
           return;
       else
           print "THIS IS YOUR HEADER";
?>

I mean you must do the following

enter image description here

Then in the Global: PHP

enter image description here

You can do the same for footer and ...

1

You can user template_preprocess_views_view function to do it

function mymodule_preprocess_views_view(&$variables) {
  if ($variables['view']->name == 'my_view') {
    if ($variables['view']->current_display == 'my_display' && !empty($variables['rows'])) {
      $variables['header'] = '';
      $variables['footer'] = '';
    }
  }
}
-1

I like Drupal(8), but it encourages some bad practices. Giving more than one way of doing something is good, but some of those should not be there. Namely, Header and Footer in the Views. It may seem like an easy solution. But there's a catch! Where is this information stored? In the database. Can you easily import/export/reuse these "settings"? Nope. So hope that'll save some one hours of confusion and wasted time, as personally my first Drupal-based site used a lot of those as I thought, well since they're there it must be good idea to use them.

Also avoid using preprocess functions, unless you absolutely must. Also source of wasted time later on.

So where can you put the header/footer? In your overwritten View twig file. For example: views-view.html.twig will cover all your views but you can put it in a specific one if you're overwriting it differently depending on a view.

Variable you can use there to give total results: {{ view.total_rows }}

4
  • The question is "How can I put a condition on the Header in a View to not display the Header if the View has content?", but I don't understand how this answer addresses that question ... Mar 21, 2017 at 17:29
  • @Pierre.Vriens My answer above has 2 parts. 1) Where not to put the header and specifically why 2) Where to put the header and how. In the "views-view.html.twig" (as one possibility). If the view has content (say 3 results), view.total_rows will be that number (non zero that is). You can use that variable inside the twig. This should give enough information to write any conditional logic. I assume OP is working on a theme, but can't assume extent of background knowledge. But this is a community right? If you require clarification on any part just ask.
    – Emil
    Mar 23, 2017 at 7:38
  • This additional comment clarifies things a bit. But even better would be to actually integrate (move) it into your actual answer. Thereby also using some header (starting with '###' to indicate the start of each of the 2 parts. Good luck! Mar 23, 2017 at 7:43
  • It's just two connected paragraphs, and seeing the path the OP is taking, both should be useful to him or others taking that route. My answer does require basic understanding of what TWIG files are, but not too much. Yet, elaborating on that could mean going in great detail on any number of subjects, which I think would be off-topic. (Unless asked, then I don't mind) But I think the goal is to be as descriptive as possible while keeping in the scope of original question. Thus, I've given exact file and exact variable for conditional test. Just paste your HTML there, and it'll work - simple.
    – Emil
    Mar 23, 2017 at 9:01

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.