0

I have a website for ads. In addition the user will have opportunity to make his ad as a VIP ad.

When the user creates a new ad, down bellow will be displayed a field "Make your ad VIP" and after clicking on that button it will popup a description. This field is created as custom field with Display suite, but I want to make visible only on node author.

The idea is only user that is author of the node can see that field "Make your ad VIP".

The module "Field permissions" cannot control this custom field.

EDIT:

The field name is "adding_make_vip", the custom module is "custom_alter" and this is the code that I've tried:

<?php

function custom_alter_field_access($op, $field, $entity_type, $entity, $account) {
  global $user;
  if ($op == 'view' && !empty($entity) && $field['field_name'] == 'adding_make_vip') {

   // $entity will be the node,so check node author with current user.
   if($entity->uid == $user->uid) {
      return TRUE;
    } 
   else {
    return FALSE;
    }
  }
}

Unfortunately it didn't hide the field for anonymous users and users that aren't authors of the node.

1 Answer 1

0

How about implementing hook_field_access() in your custom module.

This hook takes operation edit/view as parameters and returns access to the user for the operations.As a sample code

function MYMODULE_field_access($op, $field, $entity_type, $entity, $account) {
  global $user;
  if ($op == 'view' && !empty($entity) && $field['field_name'] == 'your_field_name') {

   // $entity will be the node,so check node author with current user.
   if($entity->uid == $user->uid) {
      return TRUE;
    } 
   else {
    return FALSE;
    }
  }
}
7
  • I've edited my thread. Please check it out. Commented Oct 15, 2013 at 13:56
  • codes are not test did you check what's their inside $entity is the cndition check if($entity->uid == $user->uid) appropriate ?also did you cleared your caches
    – arpitr
    Commented Oct 15, 2013 at 15:12
  • Yes, I've already cleared my caches. I'm not sure how to check inside $entity with the condition check if($entity->uid == $user->uid) Commented Oct 16, 2013 at 10:21
  • @user2519032 if you have devel installed you can check with this code dpm($entity); inside MYMODULE_field_access()
    – arpitr
    Commented Oct 16, 2013 at 10:23
  • I've tried with krumo($entity), but there are bunch of (Object) stdClasses and I didn't find theme_breadcrumb() there. Commented Oct 16, 2013 at 10:40

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.