You are here

function social_group_views_query_alter in Open Social 8.8

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_group/social_group.module \social_group_views_query_alter()
  2. 8 modules/social_features/social_group/social_group.module \social_group_views_query_alter()
  3. 8.2 modules/social_features/social_group/social_group.module \social_group_views_query_alter()
  4. 8.3 modules/social_features/social_group/social_group.module \social_group_views_query_alter()
  5. 8.4 modules/social_features/social_group/social_group.module \social_group_views_query_alter()
  6. 8.5 modules/social_features/social_group/social_group.module \social_group_views_query_alter()
  7. 8.6 modules/social_features/social_group/social_group.module \social_group_views_query_alter()
  8. 8.7 modules/social_features/social_group/social_group.module \social_group_views_query_alter()
  9. 10.3.x modules/social_features/social_group/social_group.module \social_group_views_query_alter()
  10. 10.0.x modules/social_features/social_group/social_group.module \social_group_views_query_alter()
  11. 10.1.x modules/social_features/social_group/social_group.module \social_group_views_query_alter()
  12. 10.2.x modules/social_features/social_group/social_group.module \social_group_views_query_alter()

Implements hook_views_query_alter().

File

modules/social_features/social_group/social_group.module, line 1719
The Social group module.

Code

function social_group_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
  if (\Drupal::currentUser()
    ->isAuthenticated()) {
    return;
  }
  if (empty($view->rowPlugin) || !$view->rowPlugin instanceof EntityRow || $view->rowPlugin
    ->getEntityTypeId() != 'group') {
    return;
  }
  $found = FALSE;
  foreach ($query->where as &$conditions) {
    foreach ($conditions['conditions'] as &$condition) {
      if ($condition['field'] == 'groups_field_data.type') {
        $found = TRUE;
        break 2;
      }
    }
  }
  if (!$found) {
    unset($condition);
    $condition = [
      'field' => 'groups_field_data.type',
      'value' => [],
      'operator' => 'not in',
    ];
  }
  $hiddens = $condition['operator'] == 'not in';

  // Define the variable as array otherwise it can be interpreted as string
  // And breaking the code below.
  $condition['value'] = [];

  /** @var \Drupal\group\Entity\GroupTypeInterface $group_type */
  foreach (GroupType::loadMultiple() as $group_type) {
    $new = !in_array($group_type
      ->id(), $condition['value']);
    $permissions = $group_type
      ->getAnonymousRole()
      ->getPermissions();
    $hidden = !in_array('view group', $permissions);
    if ($new && $hiddens == $hidden) {
      $condition['value'][] = $group_type
        ->id();
    }
  }
  if (!$found) {
    if (isset($conditions)) {
      $conditions['conditions'][] = $condition;
    }
    else {
      $query->where[] = [
        'conditions' => [
          $condition,
        ],
        'type' => 'AND',
      ];
    }
  }
}