From 36828fd6c556992bfb42e4ac94c83b633de06dea Mon Sep 17 00:00:00 2001 From: Ludovic Ramarojaona Date: Fri, 1 Mar 2024 16:00:22 +0000 Subject: [PATCH] EICNET-2729: Create a default custom ordering for Organisations members. --- ...ontext.context.group_members_overviews.yml | 6 +- .../DocumentProcessor/ProcessorUser.php | 58 +++++++++++++++++++ .../Search/Sources/UserGallerySourceType.php | 11 +++- .../src/Service/SolrSearchManager.php | 4 ++ 4 files changed, 76 insertions(+), 3 deletions(-) diff --git a/config/sync/context.context.group_members_overviews.yml b/config/sync/context.context.group_members_overviews.yml index caba651384..571a78d0d2 100644 --- a/config/sync/context.context.group_members_overviews.yml +++ b/config/sync/context.context.group_members_overviews.yml @@ -23,13 +23,14 @@ reactions: id: blocks uuid: 52968c87-7333-4284-a1e0-68f19e8ed28d blocks: - 6b8e8018-5a77-4a11-a3cc-3df82e28a314: - uuid: 6b8e8018-5a77-4a11-a3cc-3df82e28a314 + 89627531-455e-4e3c-87c1-09ee56341cb4: + uuid: 89627531-455e-4e3c-87c1-09ee56341cb4 id: eic_search_overview label: '' provider: eic_search label_display: visible region: content + weight: '0' custom_id: eic_search_overview_group_members theme: eic_community css_class: '' @@ -49,6 +50,7 @@ reactions: sm_roles_group: sm_roles_group sm_user_profile_job_string: 0 sort_options: + team_default_sort: team_default_sort its_activity_score_group: its_activity_score_group ds_user_access: ds_user_access score: score diff --git a/lib/modules/eic_search/src/Search/DocumentProcessor/ProcessorUser.php b/lib/modules/eic_search/src/Search/DocumentProcessor/ProcessorUser.php index 0d07b5771c..9bc1f6b74c 100644 --- a/lib/modules/eic_search/src/Search/DocumentProcessor/ProcessorUser.php +++ b/lib/modules/eic_search/src/Search/DocumentProcessor/ProcessorUser.php @@ -10,6 +10,8 @@ use Drupal\eic_comments\Constants\Comments; use Drupal\eic_private_message\PrivateMessageHelper; use Drupal\eic_user\UserHelper; +use Drupal\group\Entity\GroupContentInterface; +use Drupal\group\Entity\GroupInterface; use Drupal\group\Entity\GroupRole; use Drupal\group\GroupMembership; use Drupal\group\GroupMembershipLoaderInterface; @@ -161,6 +163,18 @@ public function process( $fields['ss_user_profile_image_uri'] : NULL; + // Add user memberships to organisations. + /** @var \Drupal\group\Entity\GroupContentInterface[] $user_memberships */ + $user_memberships = $this->entityTypeManager->getStorage('group_content') + ->loadByProperties([ + 'type' => 'organisation-group_membership', + 'entity_id' => $user->id(), + ]); + foreach ($user_memberships as $user_membership) { + $document->addField($this->getMembershipSortFieldName($user_membership->getGroup()), + $this->getOrganisationMembershipSortOrder($user_membership)); + } + $teaser_relative = ''; // Generates image style for the user picture. @@ -181,6 +195,50 @@ public function process( ); } + /** + * Returns the field name of membership sort for the given group. + * + * @param \Drupal\group\Entity\GroupInterface $group + * The group entity. + * + * @return string + * The field name. + */ + protected function getMembershipSortFieldName(GroupInterface $group) { + return 'its_team_default_sort_' . $group->id(); + } + + /** + * Returns the sort order for the membership. + * + * @param \Drupal\group\Entity\GroupContentInterface $user_membership + * The user membership object. + * + * @return int + * The sort order. + */ + protected function getOrganisationMembershipSortOrder(GroupContentInterface $user_membership) { + // We want CEO and CFO to appear first, then members by order of + // membership date. + switch ($user_membership->field_vocab_job_title->target_id) { + // General management / CEO. + case 20: + $membership_sort = -10; + break; + + // Accounting / Finance / CFO. + case 21: + $membership_sort = -5; + break; + + default: + $membership_sort = $user_membership->getCreatedTime(); + break; + } + + return $membership_sort; + } + /** * {@inheritdoc} */ diff --git a/lib/modules/eic_search/src/Search/Sources/UserGallerySourceType.php b/lib/modules/eic_search/src/Search/Sources/UserGallerySourceType.php index f98f922d16..0b92f48acf 100644 --- a/lib/modules/eic_search/src/Search/Sources/UserGallerySourceType.php +++ b/lib/modules/eic_search/src/Search/Sources/UserGallerySourceType.php @@ -14,6 +14,11 @@ class UserGallerySourceType extends SourceType { use StringTranslationTrait; + /** + * This is used as a token for the team sort machine name. + */ + const TEAM_DEFAULT_SORT = 'team_default_sort'; + /** * @inheritDoc */ @@ -55,6 +60,10 @@ public function getAvailableFacets(): array { */ public function getAvailableSortOptions(): array { return [ + self::TEAM_DEFAULT_SORT => [ + 'label' => $this->t('Organisation team default sort', [], ['context' => 'eic_search']), + 'ASC' => $this->t('Default', [], ['context' => 'eic_search']), + ], DocumentProcessorInterface::SOLR_MOST_ACTIVE_ID => [ 'label' => $this->t('Most active', [], ['context' => 'eic_search']), 'DESC' => $this->t('Most active', [], ['context' => 'eic_search']), @@ -83,7 +92,7 @@ public function getAvailableSortOptions(): array { * @inheritDoc */ public function getDefaultSort(): array { - return [DocumentProcessorInterface::SOLR_MOST_ACTIVE_ID, 'DESC']; + return ['team_default_sort', 'ASC']; } /** diff --git a/lib/modules/eic_search/src/Service/SolrSearchManager.php b/lib/modules/eic_search/src/Service/SolrSearchManager.php index 6bfaef7b4d..b2dcfbaa16 100644 --- a/lib/modules/eic_search/src/Service/SolrSearchManager.php +++ b/lib/modules/eic_search/src/Service/SolrSearchManager.php @@ -242,6 +242,10 @@ public function buildSortFacets(?array $facets_value, ?string $sort_value) { if ($sort_value) { $sorts = explode('__', $sort_value); + if ($this->source instanceof UserGallerySourceType && $sorts[0] == $this->source::TEAM_DEFAULT_SORT) { + $sorts[0] = 'its_team_default_sort_' . $this->currentGroup; + } + //Normally sort key have this structure 'FIELD__ASC' but add double check if (2 === count($sorts)) { // Check if sort needs a group injection before sending to solr.