Drupal 7: hide field content from anonymous users

Two approaches to hiding the content of a field to non-members of your website.

Approach #1: Do it in a module.
Approach #2: Use field templates in your theme.

Approach #2 is more sophisticated and elegant and gives you the ability to change the html at field level. It’s also new in Drupal 7. However approach #1 is great just because it’s in a module so everything is there just in one place.

In my particular case I did both: I used the module to provide the drupal_set_message() and the field templates to show the field-level messages. Sweet!

Approach #1: Do it in a module (hook_node_load)

Something like this:

<?php
/**
 * Implements hook_node_load().
 *
 * Prevents the following fields from being rendered to anonymous users:
 *
 * - field_file
 * - field_video
 * - field_link
 *
 * Sets a message that is shown to the user if a field has been hidden.
 *
 */
function mymodule_node_load($nodes, $types) {
  global $user;
  $hidden = FALSE;
  // user is anonymous
  if ($user->uid == 0) {
    // apply to single node view only
    // remove this condition to apply to views with multiple resources
    if (count($nodes) == 1) {
      foreach ($nodes as $node) {
        if ($node->type == 'resource') {
          if (count($node->field_file)) {
            unset($node->field_file);
            $hidden = TRUE;
          }
          if (count($node->field_video)) {
            unset($node->field_video);
            $hidden = TRUE;
          }
          if (count($node->field_link)) {
            unset($node->field_link);
            $hidden = TRUE;
          }
        }
      }
    }
  }       
  if ($hidden) {
    $link = l('members','membership');
    drupal_set_message("This resource contains some content available only to $link.");
  }
}

Approach #2: Use field templates in your theme.

Create a field template within your theme
Firstly, copy:
drupalroot/modules/field/themes/field.tpl.php
… to your custom theme folder.

Next, copy the field.tpl.php to field–field_name.tpl.php. Note the two dashes in the name. field_name is the machine name of the field, as you see it in Content Types > Manage Fields. So it could be something like field–field_video.tpl.php.

Note I said copy the field.tpl.php to field–field_name.tpl.php rather than simply renaming it. That’s because the Drupal documentation tells us that the overridden template must be in the same directory as the base template.

Copy it into your custom theme folder, so that it becomes something like:
drupalroot/sites/all/themes/mytheme/field–field_name.tpl.php

Make sure that the permissions on the file is correct, moving the file may have messed up your permissions so make sure www-user can read it. Also, of course, clear the cache!

Now you can edit the field template to show an access denied message to non-signed-up users:

<?php global $user; ?>
<div class="<?php print $classes; ?> clearfix"<?php print $attributes; ?>>
  <?php if (!$label_hidden) : ?>
    <div class="field-label"<?php print $title_attributes; ?>><?php print $label ?>:&nbsp;</div>
  <?php endif; ?>

  <?php if ($user->uid > 0) : ?>
    <div class="field-items"<?php print $content_attributes; ?>>
      <?php foreach ($items as $delta => $item) : ?>
        <div class="field-item <?php print $delta % 2 ? 'odd' : 'even'; ?>"<?php print $item_attributes[$delta]; ?>><?php print render($item); ?></div>
      <?php endforeach; ?>
    </div>
  <?php else : ?>
      <?php foreach ($items as $delta => $item) : ?>
        <div class="field-item <?php print $delta % 2 ? 'odd' : 'even'; ?>"<?php print $item_attributes[$delta]; ?>><a href='/membership'>RESOURCE AVAILABLE ONLY TO MEMBERS</a></div>
      <?php endforeach; ?>
    </div>
  <?php endif; ?>
</div>

Viel Spass Leute! Bis nächstes Mal!

About these ads
Posted in drupal

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: