1

In user account (/admin/config/people/accounts/fields) I have a field that references to an entity: company.

I have a node-type, dossier, with a field that references the same entity as in user account: company.

How can I make a view where the logged in user only sees the user's own dossiers?

I tried a lot of things but nothing works. Sometimes the view is empty, sometimes the logged in user sees all the dossiers of the website.

3
  • To the best of my knowledge, you can't do it through the AI. I struggled with it over a year ago and never got it right. It's easy with a small custom module though which I can quickly type up. How many companies can the user's field reference? Commented Dec 13, 2016 at 14:50
  • Only 1 company. It is the company where the user is working for.
    – user24924
    Commented Dec 13, 2016 at 15:32
  • I think my answer should take care of most of it. Commented Dec 13, 2016 at 16:02

2 Answers 2

1

I added an extra reference field to dossiers which refers to an user. In the view: add relationship to the referenced field and make it mandatory. Now it works. The logged in user sees only his files.

1

You will need to create a view to display dossiers when the company's NID is the contextual filter.

The below is for a module called "custom_views_args" and it's the custom_views_args.module file. You need to change the field name, view name and display name. It should work fine.

<?php

function custom_views_args_views_pre_view(&$view, &$display_id, &$args){ 

// Load User - This is done and held in memory on every page load so don't worry about performance.
    global $user;
    $user_company = user_load($user->uid);

// Get the Company's NID    
    $company_nid = 0; // Might stop errors
    if (!empty($user_company->field_COMPANY['und'][0]['value']) or isset($user_company->field_COMPANY['und'][0]['value'])) { 
    $company_nid = $user_company->field_COMPANY['und'][0]['value'];
}   

// Hook Views Pre View 
    if($view->name == 'VIEW_NAME' and $display_id == 'DISPLAY_NAME') {      
        $args[0] = $company_nid; 
    }   
} 

Works for me.

By the way, this question title isn't relevant to the question really.

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.