0

I have a video field that should be visible only to authenticated users on one content type (bundle), but unrestricted on another content type.

What's a clean way to hide this field on one content type only, for anonymous users?

Notes:

  1. I have the module field permissions installed, but it doesn't support per bundle field permissions.

  2. I'm using Display Suite, so I'd rather not use a custom template unless that's the cleanest way to handle this one field.

  3. I could create a new view mode that hides this field and and use HOOK_entity_view_mode_alter to switch to that view mode for anonymous users, but there are quite a lot fields to display so having 2 big view modes that are 95% the same seems kind of silly.

Edit: This is the code I used based on @wolverine's answer below:

function HOOK_utility_field_access($op, $field, $entity_type, $entity, $account) {

  //Check video field only when attached to 'lesson'.
  if ($field['field_name'] == 'field_video' && $entity_type == 'node' && $op == 'view' && $entity->type == 'lesson') {

    //Grant access only if user is logged in.
    if ($account->uid > 0) {
      return TRUE;
    }
    return FALSE;
  }
}

1 Answer 1

1

Since you want to do it for few fields, I would suggest to do it using custom code rather than any module.

You can implement hook_field_access($op, $field, $entity_type, $entity, $account) and check for the "field name, content type, user, operation" and then deny or allow access.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.