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!