Skip to content

Commit c6749e2

Browse files
authored
Merge pull request #306 from necyberteam/d8-2236
Use qaBot in access-ci-ui
2 parents e395f63 + 91b2dcf commit c6749e2

File tree

6 files changed

+128
-16
lines changed

6 files changed

+128
-16
lines changed

access.module

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,27 @@ function access_page_attachments(array &$attachments) {
7575

7676
$current_uri = \Drupal::request()->getRequestUri();
7777

78-
// Get current user information
78+
// Get current user data for JavaScript.
7979
$current_user = \Drupal::currentUser();
8080
$user_data = [];
81-
8281
if ($current_user->isAuthenticated()) {
83-
$user = \Drupal\user\Entity\User::load($current_user->id());
84-
$user_data['username'] = $current_user->getAccountName();
85-
$user_data['email'] = $user->getEmail();
86-
$user_data['firstName'] = $user->get('field_user_first_name')->value ?? '';
87-
$user_data['lastName'] = $user->get('field_user_last_name')->value ?? '';
88-
$user_data['accessId'] = $current_user->id();
82+
$user_entity = \Drupal\user\Entity\User::load($current_user->id());
83+
$first_name = $user_entity->get('field_user_first_name')->value ?? '';
84+
$last_name = $user_entity->get('field_user_last_name')->value ?? '';
85+
$full_name = trim($first_name . ' ' . $last_name);
86+
$account_name = $user_entity->getAccountName();
87+
88+
// Derive accessId from username by removing @access-ci.org
89+
$access_id = '';
90+
if (str_ends_with($account_name, '@access-ci.org')) {
91+
$access_id = str_replace('@access-ci.org', '', $account_name);
92+
}
93+
94+
$user_data = [
95+
'email' => $user_entity->getEmail(),
96+
'name' => $full_name,
97+
'accessId' => $access_id,
98+
];
8999
}
90100

91101
// Adding global library to all pages.

js/headerfooter.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,23 @@ import {
77
footer,
88
footerMenus,
99
header,
10+
qaBot,
1011
siteMenus,
1112
universalMenus,
12-
} from "https://esm.sh/@access-ci/[email protected]";
13+
} from "/libraries/access-ci-ui/access-ci-ui.js";
1314

1415
(function (Drupal, drupalSettings) {
1516

1617
'use strict';
1718

19+
let currentUri = drupalSettings.access.current_uri;
20+
1821
/**
1922
* Attaches the JS test behavior to weight div.
2023
*/
2124
Drupal.behaviors.accessMenuData = {
2225
attach: function (context, settings) {
23-
var currentMenu = drupalSettings.access.current_menu;
24-
var currentUri = drupalSettings.access.current_uri;
26+
let currentMenu = drupalSettings.access.current_menu;
2527
try {
2628
currentMenu = JSON.parse(currentMenu);
2729
} catch (e) {
@@ -90,4 +92,28 @@ async function setMenu(menu, currentUri) {
9092
target: document.getElementById("footer")
9193
});
9294

95+
const { email = '', name = '', accessId = '' } = drupalSettings.access.user || {};
96+
const apiKey = "4nn5l4T4TnkMdzsK0AwAtnGRcheBDnjawuAT42LaOLc";
97+
98+
qaBot({
99+
target: document.getElementById("qa-bot"),
100+
apiKey: apiKey,
101+
isLoggedIn: isLoggedIn,
102+
userEmail: email,
103+
userName: name,
104+
accessId: accessId,
105+
loginUrl: "/login?redirect=" + currentUri,
106+
});
107+
108+
qaBot({
109+
target: document.querySelector(".embedded-qa-bot"),
110+
embedded: true,
111+
apiKey: apiKey,
112+
isLoggedIn: isLoggedIn,
113+
userEmail: email,
114+
userName: name,
115+
accessId: accessId,
116+
loginUrl: "/login?redirect=" + currentUri,
117+
});
118+
93119
};

modules/cssn/src/Plugin/Block/PersonaBlock.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,23 @@ public function build() {
8282
$last_name = $user_entity->get('field_user_last_name')->value;
8383
$pronouns = $user_entity->get('field_user_preferred_pronouns')->value;
8484

85-
// Show access organization if set; otherwise, use institution field.
85+
// Show access organization if set; use institution field if organization is "Other" (3695) or not set.
8686
$orgArray = $user_entity->get('field_access_organization')->getValue();
87+
$institution = $user_entity->get('field_institution')->value;
88+
8789
if (!empty($orgArray) && !empty($orgArray[0])) {
8890
$nodeId = $orgArray[0]['target_id'];
89-
if (!empty($nodeId)) {
91+
// If organization is "Other" (node ID 3695), use institution field instead
92+
if ($nodeId == 3695) {
93+
$institution = $user_entity->get('field_institution')->value;
94+
} else if (!empty($nodeId)) {
9095
$orgNode = \Drupal::entityTypeManager()->getStorage('node')->load($nodeId);
96+
if ($orgNode) {
97+
$institution = $orgNode->getTitle();
98+
}
9199
}
92100
}
93-
$institution = isset($orgNode) ? $orgNode->getTitle() : $user_entity->get('field_institution')->value;
101+
// If no organization is set, use institution field (this is already handled above)
94102

95103
$roles = $user_entity->getRoles();
96104
$is_student = array_search('student', $roles) !== FALSE;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
(function ($, Drupal, once) {
2+
'use strict';
3+
4+
Drupal.behaviors.organizationToggle = {
5+
attach: function (context, settings) {
6+
once('organization-toggle', '[name="field_access_organization[0][target_id]"]', context).forEach(function (element) {
7+
var $orgField = $(element);
8+
var $institutionField = $('[name="field_institution[0][value]"]').closest('.field--name-field-institution');
9+
10+
function toggleInstitutionField() {
11+
var selectedValue = $orgField.val();
12+
13+
// Check if the value contains "Other" and node ID 3695
14+
// Autocomplete fields might have format like "Other (3695)"
15+
if (selectedValue === '3695' ||
16+
(selectedValue && selectedValue.includes('3695')) ||
17+
(selectedValue && selectedValue.toLowerCase().includes('other'))) {
18+
$institutionField.show();
19+
$institutionField.find('input').prop('required', true);
20+
} else {
21+
$institutionField.hide();
22+
$institutionField.find('input').prop('required', false);
23+
}
24+
}
25+
26+
// Initial state
27+
toggleInstitutionField();
28+
29+
// Listen for changes - multiple events to catch all scenarios
30+
$orgField.on('change keyup input', toggleInstitutionField);
31+
32+
// Handle autocomplete selections
33+
$orgField.on('autocompleteselect autocompletechange', function(event, ui) {
34+
setTimeout(toggleInstitutionField, 100);
35+
});
36+
37+
// Also check periodically in case value changes via other means
38+
setInterval(function() {
39+
var currentValue = $orgField.val();
40+
if (currentValue !== $orgField.data('lastValue')) {
41+
$orgField.data('lastValue', currentValue);
42+
toggleInstitutionField();
43+
}
44+
}, 500);
45+
});
46+
}
47+
};
48+
49+
})(jQuery, Drupal, once);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
organization-toggle:
2+
js:
3+
js/organization-toggle.js: {}
4+
dependencies:
5+
- core/jquery
6+
- core/once

modules/user_profiles/user_profiles.module

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,26 @@ function user_profiles_form_alter(&$form, &$form_state, $form_id) {
8888
}
8989

9090
// Check here for id because case fall-through from user_form case.
91-
// Hide some fields.
91+
// Hide some fields only on registration forms.
9292
if ($form_id == 'user_register_form') {
9393
$form['field_user_bio']['#access'] = FALSE;
94-
$form['field_institution']['#access'] = FALSE;
9594
$form['field_citizenships']['#access'] = FALSE;
9695
$form['field_user_preferred_pronouns']['#access'] = FALSE;
9796
}
97+
98+
// Apply institution field visibility logic to both registration and edit forms
99+
// This must come after ASP access restrictions to avoid being overridden
100+
if (isset($form['field_access_organization']) && isset($form['field_institution'])) {
101+
// Initially hide the institution field, show it via JS when "Other" is selected
102+
$form['field_institution']['#states'] = [
103+
'visible' => [
104+
':input[name="field_access_organization[0][target_id]"]' => ['value' => '3695'],
105+
],
106+
];
107+
108+
// Add JS library for organization field behavior
109+
$form['#attached']['library'][] = 'user_profiles/organization-toggle';
110+
}
98111
break;
99112
}
100113
}

0 commit comments

Comments
 (0)