Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Hi,
let's say I have a field called web_url :
foreach ((array)$field_web_url as $item) {
print $item['view']
}
How should I code conditional so the field and its label will not be displayed if field is empty ?
Cordially,
Brakkar
Comments
Comment #1
jjeff CreditAttribution: jjeff commentedWell this code doesn't output a field label, so you'll probably only get
<p></p>
as the output if there's nothing there.But the thing to do is to look at the field's "value" attribute rather than the "view" attribute and see if it's empty. So your code would look something like this:
That is untested, but you should get the idea
Comment #2
jjeff CreditAttribution: jjeff commentedComment #3
(not verified) CreditAttribution: commentedComment #4
Robardi56 CreditAttribution: Robardi56 commentedOk,
what I want to do is include the label of a field (simple text) in a conditional so if field is empty, label will not display neither.
I tried your code:
with a ticker_id field, but got this error message:
Parse error: syntax error, unexpected T_STRING, expecting T_VARIABLE or '$' in /big/dom/xmysite/www/drupal/sites/www.mysite.com/modules/contemplate/contemplate.module(455) : eval()'d code on line 3
Any clue why ?
Cordially,
Brakkar
Comment #5
Robardi56 CreditAttribution: Robardi56 commentedOk,
found the solution.... if someone thinks the code is bad, please reopen the issue and explain why.
Meanwhile, here is what I use: it will print the field only if it has a value:
This is an example for a cck field called "ticker_id".
Brakkar
Comment #6
jjeff CreditAttribution: jjeff commentedLooks good to me!
-j
Comment #7
(not verified) CreditAttribution: commentedComment #8
nathanraft CreditAttribution: nathanraft commentedI am still getting the h4 label showing even if there is no value in the field. Here is my contemplate code.
Any ideas? I must be missing something simple here but can't figure it out.
Thanks!!
Comment #9
gemini CreditAttribution: gemini commentedThis is how the regular field output looks like:
if we create condition mentioned above - that means that the whole layer will be output with the lable anyways. When I edit contemplates I have to add this condition on the outside of each layer like this:
I use [0]['value'] only in the case when I output a single node since there are no other values in the same array (in my cases).
I think this type of condition needs to be pre-set in the contemplates, otherwise there is so much extra html code in the output in case of blank fields.
Comment #10
gemini CreditAttribution: gemini commentedJust realised that using [0]['value'] will work in any case - of single or multiple values in the array. There is one problem thought.. with fieldimage/imagecache ['value'] doesn't work - you have to use ['view'].
I think this is an issue where templates are incomplete because they don't have the condition not to output all that extra HTML code in case of blank fields.
Also, I wanted to note that the HTML structure in contemplates is not the same as default structure in regular CCK node output. CCK has "Display Fields" tab where you can set lables to show inline, above the fields or hide - default HTML code of contemplates is not the same and doesn't reflect "Display Fields" settings which I think it supposed to.
Comment #11
gemini CreditAttribution: gemini commentedActually, your HTML structure is much better, but I don't like that it doesn't follow the "Display Fields" rules. May be there should be a CSS added.
Comment #12
jrglasgow CreditAttribution: jrglasgow commenteddoes this still need to be open?
Comment #13
YesCT CreditAttribution: YesCT commentedThis is very helpful!
Comment #14
jrglasgow CreditAttribution: jrglasgow commentedComment #16
nirvanajyothi CreditAttribution: nirvanajyothi commentedAm adding the code that worked for me. Hope the template is alright. It also allows to add a background color in a table the dimensions of which can be changed.
Thanks for all the hints.
Comment #17
dsweeney0126 CreditAttribution: dsweeney0126 commentedReally helpful discussion. I'm using the code below to conditionally display the field label, but when the field has multiple entries, I get multiple labels. Any thoughts?
foreach ((array)$field_phases[0] as $item) {
if(!empty($item['view'])){
echo "Phases:";
}
}
Comment #18
dsweeney0126 CreditAttribution: dsweeney0126 commentedNevermind. Using the code in comment #9 solved the problem.
Comment #19
wildchief CreditAttribution: wildchief commentedI had something similar where one of my fields I allowed users to add 10 fields. If they only filled in 5 of them I found that it still out put empty fields in my case as blank list items. This is how i solved it
Reasons Why
Comment #20
citronica CreditAttribution: citronica commented#16 worked great for me, thanks!
Comment #21
Rosamunda CreditAttribution: Rosamunda commentedI´ve tried #16, but my matrix field won´t appear.
I´m printing it like this:
Any ideas? Maybe it doesn´t work because it is a matrix field and it should be printed a different way?
Thanks for your help!!!
Rosamunda
Comment #22
Prodigy CreditAttribution: Prodigy commented@ Rosamunda
Try adjusting line 2 to read the following:
Comment #23
youssefr CreditAttribution: youssefr commentedHi everyone,
I tried #21 and #22 and they are both working great for me and I am getting the result I am looking for.
print $node->field_curso_snippets[0]['value']
orprint $node->field_curso_snippets[0]['view']
did not change anything for me. The purpose here is to show and style only the added values for my multi-value CCK field "field_snippets". In my case, I have 4 values for this CCK field (I am letting the users publish a maximum of 4 snippets/values).Question: how do you hide the style of background, border...from displaying when the user has not added any values?
Thank you.
Comment #24
evinweissenberg CreditAttribution: evinweissenberg commentedThe solution is very simple and it goes like so...
<table>
</table>