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!
| Comment | File | Size | Author |
|---|---|---|---|
| #20 | views-trim-fix.patch | 543 bytes | dawehner |
Comments
Comment #1
karens commentedYou are only seeing this on filefields and imagefields, so this is a filefield issue.
Comment #2
Anonymous (not verified) commentedThe 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
Comment #3
merlinofchaos commentedYou can only accomplish this through theming.
Comment #4
tenek commentedThanks 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:
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.
Comment #5
tenek commentedComment #6
jhedstromLabels can be removed at the preprocess level using something like this:
Comment #7
mariagwyn commentedI 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?
Comment #8
aren cambre commentedWould 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.
Comment #9
varal commentedI 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'.
Comment #10
wesjones commentedsubscribing
Comment #11
merlinofchaos commentedThe Fields row style now has an option for this in CVS, both 2.x and 3.x branches.
Comment #13
c-c-m commentedI do not see this option you mention, merlinofchaos. Where should it be? Is this issue really solved?
Comment #14
dawehnerIts in the dev/CVS Version.
As merlinofchaos said.
Comment #15
c-c-m commentedI 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
Comment #16
dawehnerWe 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.
Comment #17
cgjohnson commentedWill 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
Comment #18
jrabeemer commentedCan we get this feature to hide on whitespace and on
? Sometimes you get fields that just have whitespace after applying Strip HTML.Comment #19
jrabeemer commentedRebasing...
Comment #20
dawehnerShouldn't " " strings be always be trimmed?
Comment #21
merlinofchaos commentedI'm not sure about that. Spaces may be intentional, particularly if you used rewriting to add a space after an inline field...
Comment #22
Royce-1 commentedThis is very useful, Thanks!
Comment #23
merlinofchaos commentedCommitted to all 3 branches.
Comment #24
blueblade commentedsubscribing
Comment #26
andrenoronha commentedI 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.
Comment #27
merlinofchaos commentedThe 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.
Comment #28
drupal@guusvandewal.nl commented#4 Works like a charm! Thank you very much!
Gus
Comment #29
tarzadon commentedGreat! 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.
Comment #30
calefilm commentedI 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?
Comment #31
calefilm commentedOr 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:
Comment #32
calefilm commentedSolution for Tables:
I used muhleder's example at http://drupal.org/node/286580#comment-3005274