drupalfun.com
Hide HTML for an empty CCK field on a custom profile node page.
Hi everyone!
I have a custom theme page for my profile node. What is the best way to hide HTML associated with a CCK field (any field type) if the field is empty?
P.S.
I was able use this "if" statement format for most CCK fields:
<?php if(content_format('field_randomname', $field_randomname[0]) != ''): ?>
HMTL (hides if the field is empty)
<?php endif ?>
I have the locations module installed and want to hide HTML associated with it. Unfortunately, the above code doesn't hide HTML for any location field that is empty. What should I use?
Cheers!
Related Questions
Responses
2. Location is necessary
Thanks for the fast response! I have 2 locations in my profile node. One is home location and the other is job location. The code above hides the locations if there is nothing in the locations field, but if I put HTML inside the php "if"statement it still appears even if the location information doesn't. The html I want to put in the locations display is something like "Home: (location stuff)" . I keep getting "Home:" to appear whether or not any info is in the locations field. Should locations be treated different from other text fields in that the field is saved as an array? Is there a way to tell drupal to check to make sure the city and state variable is empty in the array? Or is something else going on when checking the field is empty specific to the locations module? Merry Christmas!
3. Mmm... I usually use
Mmm... I usually use Contemplate to override node pages, this way, I never got this problem.
You could edit content type to not display the field's label Home (edit -> display fields). Next, add the Home label inside the PHP if routine. That way, you are sure only appears when needed.
If you enable the Contemplate module, you get an overview of all the different fields that you can display in the theme (under teaser variables when you create a new template).
If you want to check if the location is empty, you can check to see if the LID is empty: $node->locations[0]['lid']
4. Our journey began with what
Our journey began with what seemed to be a simple request: How do you remove the colons from the end of field labels on a Drupal 6 site isp?
Should be simple, right? A CSS tweak, a function override in the theme, maybe a hook_form_alter -- standard stuff. No big whoop. Well, it was not as straightforward as we would have hoped. But backup software, after some code snippet borrowing, a foray into regex-land, many theme function override attempts and one major "aha" moment, we've finally arrived at a solution we can all get behind (and understand).
Not all fields are created equally
I'll skip right to the punchline. The main thing to realize is that the HTML output for various fields is not all created equally or in the same place file recovery. Fields provided by Drupal core have their own theme function-- theme_form_element-- an explanation of which can be found on the Drupal API site
Take a look at the theme_form_element code on http://api.drupal.org (every Drupal themer's best friend). This code, if copied into your theme's template.php file can be manipulated to produce the output you desire search engine optimization:

1. Hi tommyc2c, I think the code
Hi tommyc2c,
I think the code you suggested is indeed the best solution if you are theming the profile anyway.
Could you set the minimum number of locations to 0 on the content type. That way there might not be a location attached to a node and thus the html might not display. Not sure if this is what you are looking for...