function social_group_flexible_group_views_query_alter in Open Social 8.7
Same name and namespace in other branches
- 8.9 modules/social_features/social_group/modules/social_group_flexible_group/social_group_flexible_group.module \social_group_flexible_group_views_query_alter()
- 8.6 modules/social_features/social_group/modules/social_group_flexible_group/social_group_flexible_group.module \social_group_flexible_group_views_query_alter()
- 8.8 modules/social_features/social_group/modules/social_group_flexible_group/social_group_flexible_group.module \social_group_flexible_group_views_query_alter()
- 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()
- 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()
- 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()
- 10.2.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 271 - 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_group_allowed_visibility',
'field' => 'entity_id',
'left_table' => 'groups_field_data',
'left_field' => 'id',
'operator' => '=',
];
$alias = 'groups_field_data_allowed_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_group_allowed_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');
// AN users can only see flexible groups that are public.
if ($account
->isAnonymous()) {
// Add context so for AN it will have a different cache.
$view->element['#cache']['contexts'][] = 'user.roles:anonymous';
$query
->setGroupOperator('OR');
// Secret group also alters the query, so lets do it better.
if (\Drupal::moduleHandler()
->moduleExists('social_group_secret')) {
$query
->addWhere(1, 'groups_field_data.type', [
'flexible_group',
], 'NOT IN');
$query
->addWhere(2, 'groups_field_data.type', [
'flexible_group',
'secret_group',
'closed_group',
'open_group',
], 'NOT IN');
$query
->addWhere($new_where, 'field_group_allowed_visibility_value', [
'public',
], 'IN');
$query
->addWhere($new_where, 'groups_field_data.type', [
'flexible_group',
], 'IN');
return;
}
// Make sure we remove flexible group as an option so only public groups
// are part of this clause.
$query
->addWhere($current_where, 'groups_field_data.type', [
'flexible_group',
], 'NOT IN');
// OR it is a flexible group but than we only want groups
// that have the public content visibility for AN users.
$query
->setWhereGroup('AND', $new_where);
$query
->addWhere($new_where, 'field_group_allowed_visibility_value', [
'public',
], 'IN');
$query
->addWhere($new_where, 'groups_field_data.type', [
'flexible_group',
], 'IN');
return;
}
}