0

What i would like to achieve is to be able to show the field's content only if another field of the node being displayed has a certain value. So I'd like to put an if condition at the beginning of the custom field.tpl.php that controls whether the content is public or restricted based on another field's value (whether the node is tagged as free or not).

Actually, what I am trying to achieve is to make a double control: if the node is tagged FREE, display it right away, if not, see the user's permissions and display it of he has "paid user" role (just to explain things).

1 Answer 1

1

I don't think that you need a custom field.tpl.php or custom module. You can just copy the field.tpl.php from a field module and modify it. Look at comments above the field.tpl.php:

  1. $items: An array of field values. Use render() to output them.
  2. ...
  3. $element['#object']
  4. $element['#field_name']
  5. you can get user info from global $user object;

i think these are key variables which you can use. E.g. (poorly modified version of field.tpl.php. But logic is like this):

<?php
  global $user;
?>

<div class="<?php print $classes; ?>"<?php print $attributes; ?>>
  <?php if (!$label_hidden): ?>
    <div class="field-label"<?php print $title_attributes; ?>><?php print $label ?>:&nbsp;</div>
  <?php endif; ?>
  <div class="field-items"<?php print $content_attributes; ?>>
    <?php foreach ($items as $delta => $item): ?>

      <?php
        if($element['#field_name'] == 'field_NAME') {
          if($item == 'MY_VALUE (e.g. free)') {
            $output = render($item);
          }
          elseif($item != 'MY_VALUE (e.g. free)' && isset($user->roles[MY_ROLE_KEY]) {
            $output = render($item);
          }
          else {
            $output = ''; // or whatever you want
          }
        }
      else {
        $output = render($item);
      }
      ?>

      <div class="field-item <?php print $delta % 2 ? 'odd' : 'even'; ?>"<?php print $item_attributes[$delta]; ?>><?php print $output; ?></div>
    <?php endforeach; ?>
  </div>
</div>

//P.S. Putting your logic into .tpl is not good..

2

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.