You are here

function opigno_catalog_views_query_alter in Opigno training catalog 8

Same name and namespace in other branches
  1. 3.x opigno_catalog.module \opigno_catalog_views_query_alter()

Implements hook_views_query_alter().

File

./opigno_catalog.module, line 41
Contains opigno_catalog.module.

Code

function opigno_catalog_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
  if ($view
    ->id() !== 'opigno_training_catalog') {
    return;
  }

  // If the user is admin, bypass the additional checks.
  $user = \Drupal::currentUser();
  if ($user
    ->hasPermission('manage group members in any group') || $user
    ->hasPermission('manage group content in any group')) {

    // Allow platform-level managers to access any training.
    return;
  }

  /** @var \Drupal\views\Plugin\views\query\Sql $query */
  $query
    ->addField('group_content_field_data_groups_field_data', 'entity_id', '', [
    'function' => 'group',
    'aggregate' => TRUE,
  ]);
  $query
    ->addField('group__field_learning_path_visibility', 'field_learning_path_visibility_value', '', [
    'function' => 'group',
    'aggregate' => TRUE,
  ]);
  $group_or = $query
    ->setWhereGroup('OR');

  // Show the group if the user is a member.
  $query
    ->addWhere($group_or, 'group_content_field_data_groups_field_data.entity_id', $user
    ->id());

  // Filter by type.
  $group_and = $query
    ->setWhereGroup('AND');
  $query
    ->addWhere($group_and, 'group_content_field_data_groups_field_data.type', 'learning_path-group_membership');

  // Or if the group is not private.
  $query
    ->addWhere($group_or, 'group__field_learning_path_visibility.field_learning_path_visibility_value', [
    'public',
    'semiprivate',
  ], 'IN');
}