Skip to main content

Preventing Empty CCK-based Blocks from Printing

File under

Today I learned that if you have a Views-powered block that uses CCK fields; and the CCK fields are empty for all the returned rows; then Views doesn’t realize you’ve got an empty set and it still prints all the scaffolding for your block – the admin links, the container divs, and so on. The content of the block is empty, but the HTML still takes up space and throws off your layout.

Fixing this is easy. To prevent an empty CCK field-based Views-powered block from being output, add a filter on the CCK field and set the filter to Is not empty (NOT NULL). That way, the empty rows are filtered out up front, so Views correctly interprets an empty set as an empty set and nothing is printed. No fuss, no muss!

The following screen shot shows an example of how you could use this technique.

Views configuration screen showing how to filter on a CCK field

In the example, we are displaying the contents of one CCK field, “Campaign” (which happens to be a node reference). To make sure the block isn’t displayed when there are no referenced nodes in the target node, we’ve added a filter on the Campaign field and set it to Is not empty (NOT NULL). Voila!

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <img> <code> <ul> <ol> <li> <dl> <dt> <dd> <pre> <h1> <h2> <h3> <h4> <h5> <h6>
  • Lines and paragraphs break automatically.
  • Adds typographic refinements.

More information about formatting options