function social_group_views_query_alter in Open Social 8.7
Same name and namespace in other branches
- 8.9 modules/social_features/social_group/social_group.module \social_group_views_query_alter()
- 8 modules/social_features/social_group/social_group.module \social_group_views_query_alter()
- 8.2 modules/social_features/social_group/social_group.module \social_group_views_query_alter()
- 8.3 modules/social_features/social_group/social_group.module \social_group_views_query_alter()
- 8.4 modules/social_features/social_group/social_group.module \social_group_views_query_alter()
- 8.5 modules/social_features/social_group/social_group.module \social_group_views_query_alter()
- 8.6 modules/social_features/social_group/social_group.module \social_group_views_query_alter()
- 8.8 modules/social_features/social_group/social_group.module \social_group_views_query_alter()
- 10.3.x modules/social_features/social_group/social_group.module \social_group_views_query_alter()
- 10.0.x modules/social_features/social_group/social_group.module \social_group_views_query_alter()
- 10.1.x modules/social_features/social_group/social_group.module \social_group_views_query_alter()
- 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 1693 - 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',
];
}
}
}