I have created a view, displayed as a block. My goal is to simply display some text if the view returns results, and hide it if the view is empty. However, I can't save the view as a block if I don't set any fields for display. Is there a way to make it save anyway, or another way to get this result without creating a module or hacking the theme?

Comments

nevets’s picture

What about adding "Node: nid" as a field and check "Exclude from display"?

narayanis’s picture

At least one field must be displayed, or it treats it just as if I had no fields to begin with. I'm using a workaround of rewriting the output of nid as a single space, but that feels hacky and difficult to explain to any future project maintainers.

nevets’s picture

You could make a view that uses Node: nid and skip the hidden single space with theming.

Edit the view and click 'Information' next to theme.

The highest level template will read something like (my view is called colors)

Display output: views-view.tpl.php, views-view--colors.tpl.php, views-view--.tpl.php, views-view--default.tpl.php, views-view--colors--default.tpl.php

Click on 'Display output', it will show you the default template code. Copy the code, in your theme directory make a file using one of the template names and paste the code in there. In this case you want a template that applies only to this view (views-view--colors.tpl.php) or possibly only a particular display of the view (in my case views-view--colors--default.tpl.php), either way the template file should have the name of your view in it.

Now edit the template file, look for the part

  <?php if ($rows): ?>
    <div class="view-content">
      <?php print $rows; ?>
    </div>
  <?php elseif ($empty): ?>
    <div class="view-empty">
      <?php print $empty; ?>
    </div>
  <?php endif; ?>

  <?php if ($pager): ?>
    <?php print $pager; ?>
  <?php endif; ?>

and change to

  <?php if ( empty($rows) && $empty): ?>
    <div class="view-empty">
      <?php print $empty; ?>
    </div>
  <?php endif; ?>

Save the changes and click the 'Rescan template files' button (under Theme: Information) and your file should show in bold.

narayanis’s picture

I'll give this a shot. Thanks so much for the detailed advice!

EDIT: I wasn't using the empty text field (just Header), so I commented out the view-content div altogether and it worked perfectly. Thanks again!