Note that in the current versions of CCK the default field theming function displays nothing (no label, no div) if the field is empty so it is not necessary to do this manually.

You now have a basic display of your fields, but you might want to clean it up a bit. For instance, you may not want to see the 'Phone:' label if there is no value in the phone fields. The following code would print out the label and values only if there are values in the fields:

<?php if (content_format('field_phone', $field_phone[0]) != '') : ?>
  <dt><label>Phone:</label></dt>
  <?php foreach ($field_phone as $phone) { ?>
    <dd> <?php print content_format('field_phone', $phone) ?> </dd>
  <?php } ?>
<?php endif; ?>

An email address presents obvious security concerns. You may not want everyone to see it. In particular, you probably don't want it to be available to spam bots. That means you may want to be sure it is hidden from anonymous users (which includes spam bots). You could accomplish that this way:

<?php $user = $GLOBALS['user']; ?>
<?php if (in_array('authenticated user', $user->roles) && content_format('field_email', $field_email[0]) != '') : ?>
  <dt><label>Email:</label></dt>
  <?php foreach ($field_email as $email) { ?>
    <dd><?php print content_format('field_email', $email) ?></dd>
  <?php }?>
<?php endif; ?>

The above code would display the email label and field only to authenticated users by checking whether the current user has that role. You can use the 'authenticated user' role, which would be any logged in user, or you can create custom roles and assign users to them, then limit your fields to users who have been assigned to those custom roles.

The long term plan is for cck to include a more sophisticated method of selectively controlling access to specific fields, but this method will work in the meantime.

Comments

justclint’s picture

Due to needing to build a large form with 160 questions I need to be able to print the form title/label. I need to do this because the titles/labels of my fields are 1 to 2 sentence questions. They all only have a YES or NO value. So I created the field names as field_q001, field_q002, and so on so that I could reference the title to the value later when I go to display the fields.

For example, in theory I want to be able to do something like:

print $node->field_q001['#title'] . $node->field_q001['0']['value'];
print "<br>"
print $node->field_q002['#title'] . $node->field_q002['0']['value'];

I know print $node->field_q001['#title'] is bad syntax but Id like to know what the right syntax is to pull the #title data.

Ive been searching, posting in forums and just cant seem to find an answer to displaying field titles. Since all the field titles/labels get created and put into the database when I create the form I dont see why I would want to hand type all those same titles/labels again when I go to display the fields; especially when those titles/labels are 1 - 2 sentences long.

Can someone please help me out and show me how to print the field title?

Thanks so much!

Scott J’s picture

justclint,
The answer is:
print $node->content['field_q001']['field']['#title']
which I found at http://www.davidnewkerk.com/book/30 , which is referenced at #209229: Field label not presented for themes in $node object.