Hi everyone,

Basically I want to hide the cck field label when no data has been entered.

Here is my current code for displaying a mobile phone number. It works ok but if no number is entered it still displays the field label.

 <?php foreach ((array)$field_mobile as $item) { ?>
   <?php print $node->content['group_contact_details']['group']['field_mobile']['field']['#title'] ?>
   <?php } ?></p>

Any suggestions?

Comments

Boogle’s picture

Missed a bit.

<?php foreach ((array)$field_mobile as $item) { ?>
   <?php print $node->content['group_contact_details']['group']['field_mobile']['field']['#title'] ?>
   <?php print $item['view'] ?>
   <?php } ?></p>

Thanks

nevets’s picture

Sounds like you want

foreach ((array)$field_mobile as $item) {
  if ( !empty($item['view']) ) {
    print $node->content['group_contact_details']['group']['field_mobile']['field']['#title'];
    print $item['view'];
  }
}
Boogle’s picture

Thank you very much, works perfectly.

Boogle’s picture

Ok, this is working nicely but I want a bit more control over the label. What do I need to do to display the label above and also put a class on it so I can add some CSS.

<?php foreach ((array)$field_open_hours as $item) {
  	if ( !empty($item['view']) ) {
    print $node->content['group_contact_details']['group']['field_open_hours']['field']['#title'];
	print $item['view'];
  	} } ?>

Thanks

nevets’s picture

Not sure what you mean by "display the label above" but if you mean only once for the set of values how about

<?php
$mobile = '';
foreach ((array)$field_mobile as $item) {
  if ( !empty($item['view']) ) {
    $mobile .= $item['view'];
  }
}
if ( !empty($mobile) ) {
  print '<div class="some-class">' . $node->content['group_contact_details']['group']['field_mobile']['field']['#title'] . '</div>';
  print $mobile;
}

?>
Boogle’s picture

I meant I want the field label to be above the content rather than inline, for example:

Mobile:
08777 888 888

The code you’ve written does that, thank you very much.

nravens’s picture

I have a similar issue. I've a custom content type and used contemplate to create a custom node tpl file but want to hide non-required fields that are empty. The problem I guess is that I have some CSS that handles headings for these non-required fields so when the field is empty, the Headings still appear.
The node tpl file has -

class="specials-heading" Honeymoon Packages and class="specials" that provides a border and other text styling for the following php snippet
print $node->field_honeymoon_package[0] ['view']

Is it possible to hide the "Honeymoon Packages" heading and the border when no text is entered into the field?

nravens’s picture

ok, thanks to the code suggested above, this worked for me :

$honeymoon_package = '';
foreach ((array)$field_honeymoon_package as $item) {
  if ( !empty($item['view']) ) {
    $honeymoon_package .= $item['view'];
  }
}
if ( !empty($honeymoon_package) ) {
  print '<div class="specials-heading">Honeymoon Packages:</div><div class="specials">' . $node->field_honeymoon_package[0]['view'] . '</div>';

}
nevets’s picture

You can simplify this to

<?php
if ( !empty($node->field_honeymoon_package[0]['view']) ) {
  print '<div class="specials-heading">Honeymoon Packages:</div><div class="specials">' . $node->field_honeymoon_package[0]['view'] . '</div>';

}
?>

or if there really can be more than one value you would want to change $node->field_honeymoon_package[0]['view'] in the print statement to $honeymoon_package.