0

Let's say a profile2 has 3 fields.

Weight: 
Height:
Length: 

The user only inputs 45 kg and 160 cm, BUT for length he inputs nothing.

Now the display is like this:

Weight:45kg
Height:160cm
Length:Select

How can I hide the field that had no input like this:

Weight:45kg
Height:160cm

Since the user didn't select any value for length, and it's not a required field. I don't want it displayed on the user profile.

I know this can be done with Views and no results behavior, but I need this for user profile display.

Here is a live example:

http://vuxi.com/users/nitap

enter image description here

1
  • 1
    Can you provide more information about the modules and field types you are using, as well as how these are being rendered / printed on the theme layer? Drupal by default will not show empty fields, so something else must be printing these values.
    – marcoscano
    Oct 28, 2015 at 13:19

3 Answers 3

0

In custom module or template.php add:

function THEMENAME_preprocess_user_profile(&$variables){
  if(!isset($variables['user_profile']['field_drinking']['und'][0]['value'])){
  unset($variables['user_profile']['field_drinking']);
}

Replace THEMENAME with your themename or modulename if defined in module.

The condition may not be the right one bicause I don't know if the value is not set as Select. But I am sure that you get the Idea.

0

One way to look at this use node.tpl for this node type and add a php check like the following:

 <?php if(!empty($content['your_field_name'])): ?>
    <div class="field-label"><strong><?php echo t('Your Label'); ?></strong></div>
    <?php print render($content['Your field Name']); ?>
 <?php endif; ?>
0

The reason this didn't work was because I added the "Select" field to replace the default "-None-". The default must be kept to "none" and changed via hook_form_alter instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.