Skip to content

Commit

Permalink
EICNET-2729: Create a default custom ordering for Organisations members.
Browse files Browse the repository at this point in the history
  • Loading branch information
lramarojaona committed Mar 1, 2024
1 parent 4afdc42 commit 36828fd
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 3 deletions.
6 changes: 4 additions & 2 deletions config/sync/context.context.group_members_overviews.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: ''
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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']),
Expand Down Expand Up @@ -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'];
}

/**
Expand Down
4 changes: 4 additions & 0 deletions lib/modules/eic_search/src/Service/SolrSearchManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 36828fd

Please sign in to comment.