3. Formatting, Hiding, and Showing Fields

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.

 
 

Drupal is a registered trademark of Dries Buytaert.