How can I allow admins to choose anything from an Entity Reference field while restricting it for everyone else? I'm doing a job listings site. Users with privileges to list jobs have an Entity Reference field connecting them to a Company node. Job nodes have a similar field. So when an employer creates a Job, he should only be able to choose the Company in his own ER field.

This much I can do: I just created an Entity Reference View and limited it with a Referencing Entity Relationship and a Current User filter that uses that Relationship.



However, admins should be able to create Jobs for any Company. This is where I fall down. It seems like there's nothing I can do inside this View, since everything in this View has to do with the nodes being listed; there's no way to conditionally turn off a filter based on the current user.

So now I need other options. I see the obvious workaround: Make the User Company field accept multiple values, and give every single one to every admin user. That will be my last resort. Before I give up and go that route, does anyone have any ideas? Any answer will do, though if yours requires my admin users to grant themselves new permissions every time we create a company, I will probably disregard it. Thanks!
The solution was to use hook_views_pre_build. That hook lets you edit the view's criteria before it actually gets run. Here's my full code:

function mymodule_permission() { // Create the permission for privileged users
    return array(
        'create jobs for any company' =&gt; array(
            'title' =&gt; t('Create Jobs for any Company'),
            'description' =&gt; t("Don't limit the Company field in the Job node type to only those Companies assigned to the user."),
        ),
    );
}

function mymodule_views_pre_build(&amp;$view) {
    /* If it's the right view and the user has the above permission… */
    if($view-&gt;name === 'job_company_entity_reference' &amp;&amp; user_access('create jobs for any company')) {
        /* …then get rid of the view criteria that limit the choices for everyone else.
        (I found these by enabling the devel module and calling dpm($view) at the top of this function.) */
        unset($view-&gt;filter['uid_current']);
        unset($view-&gt;relationship['reverse_field_company_user']);
    }
}