From 83be2f4aa23d8c298a6f304b08e3da909dfb157f Mon Sep 17 00:00:00 2001 From: Kyle Huynh Date: Wed, 10 May 2023 13:09:26 +0000 Subject: [PATCH] Added a custom filter and move the query altering to the filter instead of a hook --- config/schema/group_solr.schema.yml | 10 +++ group_solr.module | 48 ++++-------- .../filter/AccessControlWithGroupFilter.php | 74 +++++++++++++++++++ 3 files changed, 97 insertions(+), 35 deletions(-) create mode 100644 config/schema/group_solr.schema.yml create mode 100644 src/Plugin/views/filter/AccessControlWithGroupFilter.php diff --git a/config/schema/group_solr.schema.yml b/config/schema/group_solr.schema.yml new file mode 100644 index 0000000..25a9560 --- /dev/null +++ b/config/schema/group_solr.schema.yml @@ -0,0 +1,10 @@ +views.filter.access_control_with_group_filter: + type: views.filter.in_operator + label: 'Select access control by Group' + mapping: + value: + type: sequence + label: 'Select access control by Group' + sequence: + type: string + label: 'Select access control by Group' \ No newline at end of file diff --git a/group_solr.module b/group_solr.module index a463dc0..73daad8 100644 --- a/group_solr.module +++ b/group_solr.module @@ -37,40 +37,18 @@ function group_solr_theme() { } /** - * Implements hook_views_query_alter - * @param $view - * @param $query - * @return void + * Implements hook_views_data(). */ -function group_solr_views_query_alter($view, $query) { - if ($view->id() == 'solr_search_content') { - // Get current user - $uid = \Drupal::currentUser()->id(); - $current_user = \Drupal\user\Entity\User::load($uid); - - // Get groups which this user is belonged to - $groups = array(); - $grp_membership_service = \Drupal::service('group.membership_loader'); - $grps = $grp_membership_service->loadByUser($current_user); - foreach ($grps as $grp) { - $groups[$grp->getGroup()->id()] = $grp->getGroup()->label(); - } +function group_solr_views_data_alter(array &$data) { + + $data['views']['access_control_with_group_filter'] = [ + 'title' => t('Access Control Filter - Configurable with Group'), + 'group' => t('Access control'), + 'filter' => [ + 'title' => t('Access Control Filter - Configurable with Group'), + 'field' => 'group_access_control', + 'id' => 'access_control_with_group_filter', + ], + ]; - if (count($groups) > 0) { - // - from those group, get the taxonomy term in field_access_terms - // - from those terms, get term ids - $terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree("islandora_access"); - $conditions = $query->createConditionGroup('OR'); - $conditions->addCondition("group_access_control", '200', "="); - foreach ($terms as $term) { - if (in_array($term->name, $groups)) { - $conditions->addCondition('group_access_control', $term->name, "IN"); - } - } - $query->addConditionGroup($conditions); - } - else { - $query->addCondition('group_access_control', "200", '='); - } - } -} +} \ No newline at end of file diff --git a/src/Plugin/views/filter/AccessControlWithGroupFilter.php b/src/Plugin/views/filter/AccessControlWithGroupFilter.php new file mode 100644 index 0000000..ac0ac4c --- /dev/null +++ b/src/Plugin/views/filter/AccessControlWithGroupFilter.php @@ -0,0 +1,74 @@ +query; + + // Get current user + $uid = \Drupal::currentUser()->id(); + $current_user = \Drupal\user\Entity\User::load($uid); + + // Get groups which this user is belonged to + $groups = array(); + $grp_membership_service = \Drupal::service('group.membership_loader'); + $grps = $grp_membership_service->loadByUser($current_user); + foreach ($grps as $grp) { + $groups[$grp->getGroup()->id()] = $grp->getGroup()->label(); + } + + if (count($groups) > 0) { + // - from those group, get the taxonomy term in field_access_terms + // - from those terms, get term ids + $terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree("islandora_access"); + $conditions = $query->createConditionGroup('OR'); + $conditions->addCondition("group_access_control", '200', "="); + foreach ($terms as $term) { + if (in_array($term->name, $groups)) { + $conditions->addCondition('group_access_control', $term->name, "IN"); + } + } + $query->addConditionGroup($conditions); + } + else { + $query->addCondition('group_access_control', "200", '='); + } + } + + public function buildOptionsForm(&$form, FormStateInterface $form_state) { + parent::buildOptionsForm($form, $form_state); + $form["filter-descritpion"] = [ + "#markup" => $this->t("Adding this fitler, it will check current user + with the existing access control with Groups and filter the results.") + ]; + unset($form['expose_button']); + } +} \ No newline at end of file