function legal_form_user_form_alter in Legal 8
Same name and namespace in other branches
- 2.0.x legal.module \legal_form_user_form_alter()
Implements hook_form_FORM_ID_alter().
File
- ./
legal.module, line 274 - Module file for Legal.
Code
function legal_form_user_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Deal with Registration form in legal_form_user_register_form_alter().
if ($form_id == 'user_register_form') {
return;
}
$accepted = FALSE;
$settings = \Drupal::config('legal.settings');
// Do nothing if configuration option is set to not display T&C.
if ($settings
->get('user_profile_display') == 0) {
return;
}
// User being edited.
$account = $form_state
->getFormObject()
->getEntity();
$uid = $account
->id();
// Do nothing for user 1 or user with exempt role.
$exempt = legal_user_is_exempt($account);
if ($exempt) {
return;
}
// Do nothing if there's no Terms and Conditions text set.
$language = \Drupal::languageManager()
->getCurrentLanguage();
$conditions = legal_get_conditions($language
->getId());
if (empty($conditions['conditions'])) {
return;
}
// Set reminder to change password if coming from one time login link.
if (isset($_REQUEST['pass-reset-token'])) {
$messages = \Drupal::messenger()
->messagesByType('status');
$status_messages = isset($messages['status']) ? $messages['status'] : [];
$reminder = t('You have just used your one-time login link. It is no longer necessary to use this link to log in. Please change your password.');
if (!in_array($reminder, $status_messages)) {
\Drupal::messenger()
->addMessage($reminder);
}
}
// Current logged in user.
$user = \Drupal::currentUser();
$uid_active = $user
->id();
// Get last accepted version for this account.
$legal_account = legal_get_accept($uid);
// If no version accepted, get version with current language revision.
if (!isset($legal_account['version']) || empty($legal_account['version'])) {
$conditions = legal_get_conditions($language
->getId());
// No conditions set yet.
if (empty($conditions['conditions'])) {
return;
}
}
else {
// Get version / revision of last accepted language.
$conditions = legal_get_conditions($legal_account['language']);
// No conditions set yet.
if (empty($conditions['conditions'])) {
return;
}
// Check latest version of T&C has been accepted.
$accepted = legal_version_check($uid, $conditions['version'], $conditions['revision'], $legal_account);
// Enable language switching if version accepted and revision up to date.
if ($accepted && $legal_account['language'] != $language
->getId()) {
$conditions = legal_get_conditions($language
->getId());
}
}
legal_display_fields($form, $conditions, 'login');
if ($accepted === TRUE) {
$form['legal']['legal_accept']['#value'] = 1;
if (!empty($conditions['extras'])) {
foreach ($conditions['extras'] as $key => $label) {
if (!empty($label)) {
$form['legal'][$key]['#value'] = 1;
}
}
}
}
// Disable checkbox if:
// - user is not account owner;
// - latest T&C has already been accepted.
if ($uid_active != $uid || $accepted == TRUE) {
$form['legal']['legal_accept']['#attributes'] = [
'disabled' => 'disabled',
];
if (!empty($conditions['extras'])) {
reset($conditions['extras']);
foreach ($conditions['extras'] as $key => $label) {
if (!empty($label)) {
$form['legal'][$key]['#attributes'] = [
'disabled' => 'disabled',
];
}
}
}
}
// Not required if user is not account owner.
if ($uid_active != $uid) {
$form['legal']['legal_accept']['#required'] = FALSE;
if (!empty($conditions['extras'])) {
reset($conditions['extras']);
foreach ($conditions['extras'] as $key => $label) {
if (!empty($label)) {
$form['legal'][$key]['#required'] = FALSE;
}
}
}
}
// Enable account owner to accept.
if ($uid_active == $uid && $accepted != TRUE) {
$form['legal']['legal_accept']['#default_value'] = isset($edit['legal_accept']) ? $edit['legal_accept'] : '';
$form['legal']['legal_accept']['#required'] = TRUE;
if (!empty($conditions['extras'])) {
reset($conditions['extras']);
foreach ($conditions['extras'] as $key => $label) {
if (!empty($label)) {
$form['legal'][$key]['#default_value'] = isset($edit[$key]) ? $edit[$key] : '';
$form['legal'][$key]['#required'] = TRUE;
}
}
}
}
}