0

I have three content types :
Content type A
Content type B
Content type C

B has a node reference to A.
C has a node reference to B.

I have also 2 roles :
Role 1
Role 2
Role 1 is belong to Content type B.
Role 2 is belong to Content type C.

I'm creating a view list in which

1) Content type A (nid) is passed from URL. Filter nodes of type B with nid of type A. ( done with contextual filter )
2) Criteria content type == B ( done with criteria )
3) Join content type C with content type B. ( done with relationship )

I have successfully got a list with above three.

But I have one more thing to do :
If logged-in user is Role 1, I want to filter nodes of type B of the logged-in user.
If logged-in user is administrator, show all nodes of type B which is filtered with node of type A.

How can I do that ? I'm using D7.7

[Edit]
I think filter criteria don't need for user role. It can be done with user filter for content type B. But I can only select all users associated the type B. I cannot select the current logged-in user id from views filter.
I may need to use this module : http://drupal.org/project/viewsphpfilter
Unfortunately, it has no update for D7.

1

I've done with hook_views_query_alter().

function mymodule_views_api() { // your module name into hook_views_api
    return array(
        'api' => 3,
        // might not need the line below, but in any case, the last arg is the name of your module
        'path' => drupal_get_path('module', 'mymodule'),
    );
}

function mymodule_views_query_alter(&$view, &$query){
    global $user;
    switch($view->name){
        case 'my_view_name' :   
            if(!in_array('administrator', $user->roles) || array_key_exists(4, $user->roles)){
                $result = db_query('my_query_here');
                $nodes = array();                   
                foreach($result as $row){
                    $nodes[] = $row->entity_id;
                }
                $query->where[0]['conditions'][] = array(
                    'field' => 'node.nid',
                    'value' => $nodes,
                    'operator' => 'in'
                );
            }
            break;
}
0

You can add a relationship in your views.

  • Edit your view
  • Open the Advanced tab
  • Add a relationship
  • Choose your relationship and name it

Now, go to the Filter Criteria and choose the user-filter. Now choose the User: Roles option, select your relationship and select your role.

And I think the best way to go it to make 2 blocks. One for Role 1 and one for administrator. But be aware, a Drupal admin had the bypass content access control-permission, so there is no use filtering the results.

  • I don't understand why I need to create 2 blocks. User filter is basically related with user role, but also need to filter with logged-in user. If logged-in user is administrator, how can I bypass that user filter. – Sithu Jan 18 '12 at 11:12
  • You can chain relationships together to make a multidimentional relationship. And for your second question, there is no way you can limit the permissions of an admin (why would you?) Best option is to create a new role calls (Administrator) and give him all but one or two permissions. It's bad practice to hand out the admin role of your website since they can do anything to it (add content, but also delete content or disable modules and by so destroying your entire website) – Michiel Jan 18 '12 at 11:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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