Adding a static text on top of view when it is not filtered

Steve S. Source

I want to add a static text (some sort of explanation/welcome text) on the very top of a view (over the filter) in Drupal 6.x with Views "2". I want this text appear only when the view is not filtered (i.e. on initial load of the page).

My problem is that the only place I figure out to make it work partially is in the views-exposed-form--MYVIEW.tpl.php. The problem is that when I place the code in this template, I don't know if the view is filtered or not, so the text appear on every single page! I don't have access to this info in that template so the only place this is available ($rows or $empty variables for example) is in views-view--MYVIEW.tpl.php.

But there I got an another problem. The order in witch it seams the variables are output are not the same as the order in witch they appeared in the file. For example, the $exposed variable content is render always on top, then $admin_links, $header and so forth.

 <?php if ($header): ?>
    <div class="view-header">
      <?php print $header; ?>
  <?php endif; ?>

  <?php if (!$rows): ?>
  <h3>This static text appear AFTER $exposed !!!</h3>  
  <?php endif; ?>

  <?php if ($exposed): ?>
    <div class="view-filters">
      <?php print $exposed; ?>
  <?php endif; ?>

  <?php if ($attachment_before): ?>
    <div class="attachment attachment-before">
      <?php print $attachment_before; ?>
  <?php endif; ?>

So even if I place my static content before this code, the filter form always appear on top!

I found the reason why is doing this: the exposed filter form is rendered as a part of the content-top <div></div>, but not the result (and $header, $footer, etc).

So is this by design? Do I miss something? How can I get my static text on the very top of the content-top!?



answered 7 years ago Steve S. #1

Well, after some tweaking and lecture on preprocess function in the theming system, I found a solution to my problem. I share it with you and if you find a more elegante approach; let me know!

What I did is this...

1) In the template.php file of the theme, I add two fonctions:

function YOURTHEME_preprocess_views_view__MYVIEW(&$variables) {
     if ($variables['rows'] || $variables['empty']) {
        $GLOBALS['dont_show_static_text'] = TRUE;

function YOURTHEME_preprocess_views_exposed_form__MYVIEW(&$variables) {
     if ($GLOBALS['dont_show_static_text']) {
        $variables['custom_flag1'] = TRUE;

So, if my views is showing some results ($row) or a blank result ($empty), then I set a flag to be use in the template file....

2) In my views-exposed-form--MYVIEW.tpl.php

<?php if (!$custom_flag1): ?> 
  <h2>Some static text here</h2>
<?php endif; ?> 

And voilĂ ! My static text is showing up only on the initial load of the view and it shows on TOP of the filter (not under!).

Hope this help someone else!

comments powered by Disqus