0

I have users that can be associated with a single node of type foo. The node has a list of users that are associated with it. I populate the entity reference fields in the content type with a view, which filters users that aren't associated with foo (user has no foo).

What I need to happen is that I need this view to also grab users that are associated with the specific node that I'm looking at, so when I try to add new users to the foo content type, the users that were associated with it beforehand remain associated with it (user has no foo OR user's foo id equals current foo id). The problem is that the first filter is a normal filter, the second is a contextual filter. How can I, programmatically or otherwise, get the views to grab the users I need with those conditions?

Edit: Unfortunately, the views-related hooks aren't called when populating an Entity Reference, which makes things like hook_views_query_alter not a solution. So a more accurate question would be: how can I get the entity reference to grab the users I need with the given conditions?

1 Answer 1

0

I guess you need to use hook_views_query_alter and then you could add your OR condition.

function hook_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
  if ($view->id() == 'machine_name_of_your_view' ) {
   //$query->where contains the filter conditions 
   //i.e. 'Where' part of the query where you could add yours.
   var_dump($query->where);die;
  }
}

more from drupal.org

1
  • It would seem that using a view as an entity reference source doesn't cause hook_views_query_alter to be called. I've edited the question to reflect this.
    – Greenstack
    Commented Sep 1, 2017 at 16:48

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.