You are here

function heartbeat_views_query_alter in Heartbeat 8

Implements hook_views_query_alter().

Parameters

ViewExecutable $view:

QueryPluginBase $query:

File

./heartbeat.views.inc, line 174
Contains heartbeat\heartbeat.views.inc.. Provide a custom views field data that isn't tied to any other module.

Code

function heartbeat_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
  switch ($view
    ->id()) {
    case 'heartbeat_friendship':
      $configuration = array(
        'table' => 'users_field_data',
        'field' => 'uid',
        'left_table' => 'heartbeat_friendship',
        'left_field' => 'uid_target',
        'operator' => '=',
      );
      $join = Views::pluginManager('join')
        ->createInstance('standard', $configuration);
      $query
        ->addRelationship('users_target', $join);
      break;
    case 'user_friends':
      $friendData = \json_decode(\Drupal::config('heartbeat_friendship.settings')
        ->get('data'));
      $friendUids = [];
      foreach ($friendData as $data) {
        $friendUids[] = $data->uid;
        $friendUids[] = $data->uid_target;
      }
      $query
        ->addWhere('AND', 'users_field_data.uid', \Drupal::currentUser()
        ->id(), '!=');
      $query
        ->addWhere('AND', 'users_field_data.uid', array_unique($friendUids), 'IN');
      break;
  }
}