Hide labels for empty fields
| Project: | Views |
| Version: | 6.x-3.x-dev |
| Component: | Code |
| Category: | feature request |
| Priority: | minor |
| Assigned: | Unassigned |
| Status: | closed |
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
You are only seeing this on filefields and imagefields, so this is a filefield issue.
#2
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
You can only accomplish this through theming.
#4
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
#6
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
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
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
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
subscribing
#11
The Fields row style now has an option for this in CVS, both 2.x and 3.x branches.
#12
Automatically closed -- issue fixed for 2 weeks with no activity.
#13
I do not see this option you mention, merlinofchaos. Where should it be? Is this issue really solved?
#14
Its in the dev/CVS Version.
As merlinofchaos said.
#15
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
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
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
Can we get this feature to hide on whitespace and on
? Sometimes you get fields that just have whitespace after applying Strip HTML.#19
Rebasing...
#20
Shouldn't " " strings be always be trimmed?
#21
I'm not sure about that. Spaces may be intentional, particularly if you used rewriting to add a space after an inline field...
#22
This is very useful, Thanks!
#23
Committed to all 3 branches.
#24
subscribing
#25
Automatically closed -- issue fixed for 2 weeks with no activity.
#26
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
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.