Hide labels for empty fields

tenek - June 2, 2009 - 21:26
Project:Views
Version:6.x-3.x-dev
Component:Code
Category:feature request
Priority:minor
Assigned:Unassigned
Status:closed
Description

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!

#1

KarenS - June 2, 2009 - 21:51
Project:Views» FileField
Version:6.x-3.x-dev» 6.x-3.x-dev

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

#2

midkemia - June 2, 2009 - 21:53
Project:FileField» Views
Version:6.x-3.x-dev» 6.x-3.x-dev

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

#3

merlinofchaos - June 2, 2009 - 21:55

You can only accomplish this through theming.

#4

tenek - June 3, 2009 - 14:06

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.

#5

tenek - June 3, 2009 - 14:10
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)

#6

jhedstrom - June 5, 2009 - 00:46

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

<?php
/**
* 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]);
    }
  }
}
?>

#7

mariagwyn - June 6, 2009 - 21:59

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?

#8

Aren Cambre - June 21, 2009 - 04:23
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 request» feature request

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.

#9

varal - June 29, 2009 - 23:03

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'.

#10

wesoccer2003 - June 30, 2009 - 22:04

subscribing

#11

merlinofchaos - July 1, 2009 - 22:53
Status:active» fixed

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

#12

System Message - July 15, 2009 - 23:00
Status:fixed» closed

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

#13

c-c-m - August 17, 2009 - 16:33

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

#14

dereine - August 17, 2009 - 17:02

Its in the dev/CVS Version.

As merlinofchaos said.

#15

c-c-m - August 18, 2009 - 20:22

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

#16

dereine - August 18, 2009 - 20:40

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.

#17

cgjohnson - August 28, 2009 - 19:43

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

#18

momendo - September 11, 2009 - 14:08

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

#19

momendo - September 11, 2009 - 14:07
Priority:normal» minor
Status:closed» active

Rebasing...

#20

dereine - September 11, 2009 - 19:03
Status:active» needs review

Shouldn't " " strings be always be trimmed?

AttachmentSize
views-trim-fix.patch 543 bytes

#21

merlinofchaos - September 25, 2009 - 00:17

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

#22

divinevette - September 28, 2009 - 19:39

This is very useful, Thanks!

#23

merlinofchaos - November 2, 2009 - 23:31
Status:needs review» fixed

Committed to all 3 branches.

#24

blueblade - November 7, 2009 - 01:48

subscribing

#25

System Message - November 21, 2009 - 01:50
Status:fixed» closed

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

#26

a.luiz.n - December 3, 2009 - 18:46

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.

#27

merlinofchaos - December 3, 2009 - 18:47

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 is a registered trademark of Dries Buytaert.