This seems like it should be very simple issue, but can someone explain how to stop a field title from showing up when the field on a certain node is empty?

Here is what happens: My view has a number of fields (e.g. title, body, content:document, content:image, etc.) and for many of them I have the title enabled. When the view displays a node is will display all of the field titles even if a field is empty. When a certain node on a certain field is empty I don't want to display the title. I can't use a filter b/c I still want to display the node if one of the fields is empty and I am hoping to avoid theming and php.

Here is an example. I get:
Test blog entry
Posted By: Admin
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Attached files:
Description:
Image:

And I want to display this:
Test blog entry
Posted By: Admin
Lorem ipsum dolor sit amet, consectetur adipiscing elit.

It seems like other people are having somewhat similar problems:
http://drupal.org/node/339075
http://drupal.org/node/426200
http://drupal.org/node/422840

Any advice?

Thanks!

CommentFileSizeAuthor
#20 views-trim-fix.patch543 bytesdawehner

Comments

karens’s picture

Project: Views (for Drupal 7) » FileField

You are only seeing this on filefields and imagefields, so this is a filefield issue.

Anonymous’s picture

Project: FileField » Views (for Drupal 7)

The way i did it

First you need to have Row Style = Fields not Node

Add all your fields, and then theme the view Row_style to not display a field and its label when empty.

To get the content in your template for a specific field of your view

$fields['YOUR_FIELD_NAME_value']->content;

To get your YOUR_FIELD_NAME cursor over the field in the view settings, and its the last bit in the status bar , or click the field and in the new section that opens it will be in the header less _value

merlinofchaos’s picture

You can only accomplish this through theming.

tenek’s picture

Thanks for the responses. I've fixed my issue but it would be GREAT if this feature could be added to views without the need to theme for noobs like me.

If anyone else is having troubles look here: http://drupal.org/node/286580#comment-958716 and here: http://www.group42.ca/theming_views_2_the_basics

Seeing as I found a couple errors in those explanations, here are the steps repeated:
1) Go to the views theme folder (sites\all\modules\views\theme) and copy the file called "views-view-fields.tpl.php"
2) Copy it into your theme's base folder (sites\all\themes\example)
3) Edit the copy of "views-view-fields.tpl.php" that is in your theme's base folder. It should end up looking like this:

<?php foreach ($fields as $id => $field): ?>
  <?php if (!empty($field->separator)): ?>
    <?php print $field->separator; ?>
  <?php endif; ?>
<?php if ($field->content): ?>
  <<?php print $field->inline_html;?> class="views-field-<?php print $field->class; ?>">
    <?php if ($field->label): ?>
      <label class="views-label-<?php print $field->class; ?>">
        <?php print $field->label; ?>:
      </label>
    <?php endif; ?>

      <?php
      // $field->element_type is either SPAN or DIV depending upon whether or not
      // the field is a 'block' element type or 'inline' element type.
      ?>
      <<?php print $field->element_type; ?> class="field-content"><?php print $field->content; ?></<?php print $field->element_type; ?>>
  </<?php print $field->inline_html;?>>
	<?php endif; ?>
  <?php endforeach; ?>

You can either just copy and paste that in or you can just add the relevant lines of code in the right place:
a) <?php if ($field->content): ?> (5 lines from the top)
b) <?php endif; ?> (3 lines from bottom)

4) Edit the view, click "Theme:Information" in the Basic Settings bar, click "rescan templates" button at the bottom.

tenek’s picture

Title: Hide Empty CCK Field When Field is Empty (but still show the rest of the node) » Hide Empty Field Label When Field is Empty (but still show the rest of the node)
jhedstrom’s picture

Labels can be removed at the preprocess level using something like this:

/**
 * Remove row if the field is empty.
 */
function MYMODULE_preprocess_views_view_fields(&$vars) {
  // Remove labels if value is empty.
  foreach ($vars['fields'] as $id => $field) {
    if (!$field->content) {
      unset($vars['fields'][$id]);
    }
  }
}
mariagwyn’s picture

I tried both of the given methods. They both work for 'standard' fields in Views. However, I have one field which is a relationship. The relationship links the node to its "Book Parent" which then outputs the title of the book in which the node is posted. If the node is not in a book, the field title is printed, as is the empty field markup (seen in source, not on the page itself). Is there any way to address this?

aren cambre’s picture

Title: Hide Empty Field Label When Field is Empty (but still show the rest of the node) » Hide labels for empty fields
Component: Miscellaneous » Code
Category: support » feature

Would also like to see this feature. This can be very useful in non-table styles.

Sure, may be accomplishable with custom coding, but would rather configure through application and not have to modify filesystem. It's a lot easier to deal with application settings than tracking file customizations.

varal’s picture

I second that, I need this function badly. It would be very welcome if there was the option 'Exclude from display if field is empty' right beneath the option 'Exclude from display'.

wesjones’s picture

subscribing

merlinofchaos’s picture

Status: Active » Fixed

The Fields row style now has an option for this in CVS, both 2.x and 3.x branches.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

c-c-m’s picture

I do not see this option you mention, merlinofchaos. Where should it be? Is this issue really solved?

dawehner’s picture

Its in the dev/CVS Version.

As merlinofchaos said.

c-c-m’s picture

I didn't understand. Is there any prevision to add this feature in a following stable release? I would like to implement it into a live site.

Thanks

dawehner’s picture

We have just to wait for 2.7. It will be there included. You can also read the cvs messages and apply the patch for it, for yourself.

cgjohnson’s picture

Will this work if the replacement pattern is empty? Any quick way to hide a field if the replacement pattern does not contain a value in a particular record?thanks

jrabeemer’s picture

Priority: Minor » Normal
Status: Active » Closed (fixed)

Can we get this feature to hide on whitespace and on &nbsp;? Sometimes you get fields that just have whitespace after applying Strip HTML.

jrabeemer’s picture

Priority: Normal » Minor
Status: Closed (fixed) » Active

Rebasing...

dawehner’s picture

Priority: Normal » Minor
Status: Closed (fixed) » Needs review
StatusFileSize
new543 bytes

Shouldn't " " strings be always be trimmed?

merlinofchaos’s picture

I'm not sure about that. Spaces may be intentional, particularly if you used rewriting to add a space after an inline field...

Royce-1’s picture

This is very useful, Thanks!

merlinofchaos’s picture

Status: Needs review » Fixed

Committed to all 3 branches.

blueblade’s picture

subscribing

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

andrenoronha’s picture

I have a block with two fields. how can I hide this block when none of the fields were filled by the user?

I used the solution in #6 but it only hides the fields.

merlinofchaos’s picture

The only way to hide the block is to ensure that no records were returned. The only way to ensure that no records are returned is to use filters. However, the filter you need is probably an OR, since you want to return a record if either field has data. Views doesn't currently let you do that, but the views_or module might be able to help there.

drupal@guusvandewal.nl’s picture

#4 Works like a charm! Thank you very much!

Gus

tarzadon’s picture

Great! The Fields row style option works for most things, but I just wanted to mention that this didn't work for textarea b/c CKEditor (in Firefox) was adding <br />'s into the field causing it not to be empty. However, the new CKEditor has fixed the issue.

In views-view-fields.tpl.php, I added:
if ($field->content != "<p></p>" ):
...
endif;

to take care of all the nodes that already have "<br />" in them.

Alternatively, you can update the database field and replace the <br />'s with NULL's.

calefilm’s picture

I see the "Hide empty fields" in Row Style: Fields...

However, when I change the Style to Table, the "Row Style: Fields" disappears. Thus, I am unable to "Hide empty fields". Can I not select this option if I am using Table Style?

calefilm’s picture

Or can someone tell me what I would replace in my "views-view-table.tpl.php" as was similarly suggested in #4?

Thank you for any help!

views-view-table.tpl.php:


<table class="<?php print $class; ?>">
  <?php if (!empty($title)) : ?>
    <caption><?php print $title; ?></caption>
  <?php endif; ?>
  <thead>
    <tr>
      <?php foreach ($header as $field => $label): ?>
        <th class="views-field views-field-<?php print $fields[$field]; ?>">
          <?php print $label; ?>
        </th>
      <?php endforeach; ?>
    </tr>
  </thead>
  <tbody>
    <?php foreach ($rows as $count => $row): ?>
      <tr class="<?php print implode(' ', $row_classes[$count]); ?>">
        <?php foreach ($row as $field => $content): ?>
          <td class="views-field views-field-<?php print $fields[$field]; ?>">
            <?php print $content; ?>
          </td>
        <?php endforeach; ?>
      </tr>
    <?php endforeach; ?>
  </tbody>
</table>

calefilm’s picture

Solution for Tables:

I used muhleder's example at http://drupal.org/node/286580#comment-3005274