0

Is there a way to hide nodes or results if it has field "restricted" type bool with value 1 if the logged in user not in admin group without writing custom code. i could do that with views hooks like "hook_views_post_execute" with writing custom code but it will decrease the performance because i will load the node field in "foreach", so is there module can handle that from the back end !?

1
  • 1
    You can use hook_node_access. The node is already loaded for inspection there. Jul 15, 2014 at 23:34

2 Answers 2

0

The field permissions module lets you assign permissions to individual fields.

enter image description here

2
0

This helped me better as its global solution for the view i want to hook.

function rcreee_helper_views_post_execute(&$view){
$views_name = array('country_profile_item_energy_efficiency',
                    'country_profile_item_renewable_energy',
                    'country_profile_item_fuel_prices',
                    'country_profile_item_power_sector',
                    'country_profile_item_energy_system_overview'
                    );
$restricted_groups = array(3=>'administrator',9=>'Premium Registered');
global $user, $restricted_groups;
$has_access = count(array_intersect_key($restricted_groups , $user->roles));
if($has_access<=0){
    if(in_array($view->name,$views_name)){
        foreach ($view->result as $result_k => $result_v){

            if(isset($result_v->_field_data['nid']['entity']->field_visibility)
                && $result_v->_field_data['nid']['entity']->field_visibility['und'][0]['value']==1)
            {
                unset($view->result[$result_k]);
            }
        }
    }

}

}

1
  • Just a few words of warning about this approach: 1. it won't work properly with paging; 2. it might be neater to use a permission rather than a hard-coded list of roles, to make it easier to update; 3. the nodes are only hidden for these views, there may be plenty of other "security holes" on your site letting the user see premium content. A more complete but more complicated way to solve the problem is using Drupal's built-in node access system.
    – Andy
    Jul 16, 2014 at 11:34

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.