Contextually filtering a view using values from a completely separate table? I have a table "user_connections"

id | uid1 | uid2
1  | 1    | 2
2  | 3    | 1
etc


So, if the current user has uid = 1, then according to my table, he is "connected" to use 2 and 3. So I just want to be able to do something like:

select uid1 from user_connections where uid2 = :currentUserUid


Then pass these uids as a parameter to the view. So that it is ultimately used as a "where in" condition.

Any ideas how to do this? Or what is the best way?
Make sure you check mark allow multiple values on your contextual filter's &quot;More&quot; settings.

Use hook_views_pre_view to pass your uid's to the arguments (contextual filter).
/*
 * Implements hook_views_pre_view()
 */
function mymodule_views_pre_view(\Drupal\views\ViewExecutable $view, $display_id, array &amp;$args) {

  if ($view-&gt;id() == &quot;view_name&quot; &amp;&amp; $view-&gt;current_display == 'page_1') {
    $uids = [2, 19]; // hard coded example, you would query db here.
    if (is_array($uids)) {
      $uids = implode('+', $uids);
    } 
    $args[0] = $uids;
  }

}

You will also need to disable the view's caching or add a cache tag if you created one for your other table query.