Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
WCMS
uw_ct_contact
Commits
49a40f03
Commit
49a40f03
authored
Feb 05, 2021
by
Liam Morland
Browse files
ISTWCMS-4591: Auto-fill contact info from LDAP via Ajax
parent
b0447b0b
Changes
1
Hide whitespace changes
Inline
Side-by-side
uw_ct_contact.module
View file @
49a40f03
...
...
@@ -22,3 +22,72 @@ function uw_ct_contact_field_group_form_process_build_alter(array &$element, For
];
}
}
/**
* Implements hook_form_FORM_ID_alter().
*
* This is the contact creation form. Run Ajax callback when WatIAM ID changes.
*/
function
uw_ct_contact_form_node_uw_ct_contact_form_alter
(
array
&
$form
,
FormStateInterface
$form_state
,
string
$form_id
):
void
{
// Configure callback to load values from LDAP when WatIAM ID is changed.
$form
[
'field_uw_ct_contact_watiam_id'
][
'widget'
][
0
][
'value'
][
'#ajax'
]
=
[
'callback'
=>
'_uw_ct_contact_node_form_callback'
,
'event'
=>
'change'
,
// Replace entire form. Do not hard-code this value because the @id may
// change as the form is rebuilt during Ajax calls. The @id is also
// different between creation and editing.
'wrapper'
=>
$form
[
'#id'
],
];
}
/**
* Implements hook_form_FORM_ID_alter().
*
* This is the contact edit form. Do the same as for creation.
*/
function
uw_ct_contact_form_node_uw_ct_contact_edit_form_alter
(
array
&
$form
,
FormStateInterface
$form_state
,
string
$form_id
):
void
{
uw_ct_contact_form_node_uw_ct_contact_form_alter
(
$form
,
$form_state
,
$form_id
);
}
/**
* Ajax callback function. Auto-fill contact data from LDAP.
*/
function
_uw_ct_contact_node_form_callback
(
array
&
$form
,
FormStateInterface
$form_state
):
array
{
// Get the WatIAM ID.
$watiam_id
=
$form
[
'field_uw_ct_contact_watiam_id'
][
'widget'
][
0
][
'value'
][
'#value'
]
??
NULL
;
if
(
!
$watiam_id
)
{
return
$form
;
}
// Do the LDAP lookup.
$person
=
\
Drupal
::
service
(
'uw_cfg_common.uw_ldap'
)
->
lookupPerson
(
'uwldap'
,
$watiam_id
);
if
(
!
$person
)
{
$form
[
'field_uw_ct_contact_watiam_id'
][
'widget'
][
0
][
'value'
][
'#description'
]
=
t
(
'LDAP lookup failed.'
);
return
$form
;
}
// Mapping between form keys and LDAP fields.
$fields
=
[
'title'
=>
'displayname'
,
'field_uw_ct_contact_email'
=>
'mail'
,
'field_uw_ct_contact_phone'
=>
'telephonenumber'
,
'field_uw_ct_contact_location'
=>
'physicaldeliveryofficename'
,
];
// Set the desired fields. Each value must be unset first.
foreach
(
$fields
as
$form_key
=>
$ldap_key
)
{
if
(
!
empty
(
$person
[
$ldap_key
][
0
]))
{
$form_state
->
unsetValue
(
$form_key
);
$form
[
$form_key
][
'widget'
][
0
][
'value'
][
'#value'
]
=
$person
[
$ldap_key
][
0
];
}
elseif
(
$form_key
===
'title'
)
{
$form
[
'title'
][
'widget'
][
0
][
'value'
][
'#description'
]
=
t
(
'Found WatIAM ID but no name. User is likely hidden.'
);
}
}
// Field field_uw_ct_contact_sort_name is "Lastname, Firstname".
if
(
!
empty
(
$person
[
'sn'
][
0
])
&&
!
empty
(
$person
[
'givenname'
][
0
]))
{
$form_state
->
unsetValue
(
'field_uw_ct_contact_sort_name'
);
$form
[
'field_uw_ct_contact_sort_name'
][
'widget'
][
0
][
'value'
][
'#value'
]
=
$person
[
'sn'
][
0
]
.
', '
.
$person
[
'givenname'
][
0
];
}
return
$form
;
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment