You are here

function social_group_flexible_group_views_query_alter in Open Social 10.2.x

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_group/modules/social_group_flexible_group/social_group_flexible_group.module \social_group_flexible_group_views_query_alter()
  2. 8.6 modules/social_features/social_group/modules/social_group_flexible_group/social_group_flexible_group.module \social_group_flexible_group_views_query_alter()
  3. 8.7 modules/social_features/social_group/modules/social_group_flexible_group/social_group_flexible_group.module \social_group_flexible_group_views_query_alter()
  4. 8.8 modules/social_features/social_group/modules/social_group_flexible_group/social_group_flexible_group.module \social_group_flexible_group_views_query_alter()
  5. 10.3.x modules/social_features/social_group/modules/social_group_flexible_group/social_group_flexible_group.module \social_group_flexible_group_views_query_alter()
  6. 10.0.x modules/social_features/social_group/modules/social_group_flexible_group/social_group_flexible_group.module \social_group_flexible_group_views_query_alter()
  7. 10.1.x modules/social_features/social_group/modules/social_group_flexible_group/social_group_flexible_group.module \social_group_flexible_group_views_query_alter()

Implements hook_views_query_alter().

Hide flexible groups everywhere when the current user cant see it.

File

modules/social_features/social_group/modules/social_group_flexible_group/social_group_flexible_group.module, line 651
The Social Group Flexible Group module.

Code

function social_group_flexible_group_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
  if (empty($view->rowPlugin) || !$view->rowPlugin instanceof EntityRow || $view->rowPlugin
    ->getEntityTypeId() !== 'group') {
    return;
  }
  $account = \Drupal::currentUser();
  if (!$account
    ->isAnonymous()) {

    // Don't trigger page cache, this will cache it for AN
    // but with LU data.
    // Dynamic page cache handles this.
    \Drupal::service('page_cache_kill_switch')
      ->trigger();
  }

  // Don't check, they can see it all.
  if ($account
    ->hasPermission('manage all groups')) {
    return;
  }

  // Let's build our join with the allowed visibility data.
  $configuration = [
    'type' => 'LEFT',
    'table' => 'group__field_flexible_group_visibility',
    'field' => 'entity_id',
    'left_table' => 'groups_field_data',
    'left_field' => 'id',
    'operator' => '=',
  ];
  $alias = 'groups_field_flexible_group_visibility';

  /** @var \Drupal\views\Plugin\views\query\Sql $query */
  $join = Views::pluginManager('join')
    ->createInstance('standard', $configuration);
  $rel = $query
    ->addRelationship($alias, $join, 'groups_field_data');
  $query
    ->addTable('group__field_flexible_group_visibility', $rel, $join, $alias);

  /** @var \Drupal\views\Plugin\views\query\Sql $query */
  $current_where = count($query->where);

  // Make sure we add one new group with a where clause.
  $new_where = $current_where + 1;

  // We need to add our group by using a query tag.
  // Otherwise views doesn't accept it.
  $query
    ->addTag('flexible_group_by');

  // Get all LU groups.
  $my_groups = \Drupal::service('social_group.helper_service')
    ->getAllGroupsForUser($account
    ->id());

  // Get all hidden groups.
  $hidden_groups = \Drupal::entityTypeManager()
    ->getStorage('group')
    ->loadByProperties([
    'field_flexible_group_visibility' => 'members',
  ]);

  // Get all hidden groups that the current user is not a member of
  // and remove them from showing in the view.
  $ids = array_diff(array_keys($hidden_groups), $my_groups);
  if ($ids) {

    // Anonymous user should only see 'public' groups.
    if ($account
      ->isAnonymous()) {
      $community_groups = \Drupal::entityTypeManager()
        ->getStorage('group')
        ->loadByProperties([
        'field_flexible_group_visibility' => 'community',
      ]);
      $ids = array_merge($ids, array_keys($community_groups));

      // Add context so for AN it will have a different cache.
      $view->element['#cache']['contexts'][] = 'user.roles:anonymous';
    }
    $query
      ->addWhere($new_where, 'groups_field_data.id', $ids, 'NOT IN');
  }

  // Make sure this block gets refreshed for a user if the group membership
  // changed for this user.
  $view->element['#cache']['contexts'][] = 'user';
  $view->element['#cache']['tags'][] = 'group_content_list:plugin:group_membership:entity:' . $account
    ->id();
}