I have a table "user_connections"

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

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?

  • Updated my answer. – No Sssweat Apr 25 at 10:18

Make sure you check mark allow multiple values on your contextual filter's "More" settings.

enter image description here

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 &$args) {

  if ($view->id() == "view_name" && $view->current_display = 'page') {
    $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.

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.