function private_message_form_user_form_alter in Private Message 8.2
Same name and namespace in other branches
- 8 private_message.module \private_message_form_user_form_alter()
Implements hook_form_FORM_ID_alter().
Add private message module specific form elements to the user form.
See also
File
- ./
private_message.module, line 268 - Contains hooks for the private message module.
Code
function private_message_form_user_form_alter(array &$form, FormStateInterface $formState) {
$config = \Drupal::config('private_message.settings');
// Get the user whose account is being modified.
$user = $formState
->getFormObject()
->getEntity();
// The form elements are only added if/when notifications have been enabled,
// and the account being edited has permission to use the private message
// system.
if ($config
->get('enable_notifications') && $user
->hasPermission('use private messaging system')) {
$form['private_messages'] = [
'#type' => 'fieldset',
'#title' => t('Private Messages'),
'#tree' => TRUE,
];
// User specific settings are stored and retrieved using the UserData
// service.
$user_data = \Drupal::service('user.data');
$user_setting = $user_data
->get('private_message', $user
->id(), 'receive_notification');
// If the user has not set a value, the system-wide default is used.
$default_value = is_null($user_setting) ? $config
->get('notify_by_default') : $user_setting;
$form['private_messages']['receive_notification'] = [
'#type' => 'checkbox',
'#title' => t('Receive notification of private messages'),
'#default_value' => $default_value,
];
$user_setting = $user_data
->get('private_message', $user
->id(), 'notify_when_using');
// If the user has not set a value, the system-wide default is used.
$default_value = is_null($user_setting) ? $config
->get('notify_when_using') : $user_setting;
$form['private_messages']['notify_when_using'] = [
'#type' => 'radios',
'#title' => t('Send notifications of new messages in a thread'),
'#options' => [
'yes' => t('For every private message'),
'no' => t('Only when not viewing the thread'),
],
'#default_value' => $default_value,
'#description' => t('Whether or not notifications should be sent when you are viewing a thread'),
'#states' => [
'visible' => [
':input[name="private_messages[receive_notification]"]' => [
'checked' => TRUE,
],
],
],
];
$options = [
60 => t('1 minute'),
180 => t('3 minutes'),
300 => t('5 minutes'),
600 => t('10 minutes'),
1800 => t('30 minutes'),
3600 => t('1 hour'),
14400 => t('4 hours'),
21600 => t('6 hours'),
43200 => t('12 hours'),
86400 => t('1 day'),
];
$user_setting = (int) $user_data
->get('private_message', $user
->id(), 'number_of_seconds_considered_away');
// If the user has not set a value, the system-wide default is used.
$default_value = is_null($user_setting) ? $config
->get('number_of_seconds_considered_away') : $user_setting;
// The system default used by administrators is a free value, whereas users
// have a limited set of values. If the default value is not in that limited
// set of values, then a default of five minutes is used.
$default_value = isset($options[$default_value]) ? $default_value : 300;
$form['private_messages']['number_of_seconds_considered_away'] = [
'#type' => 'select',
'#title' => t('Amount of time after leaving a thread that the system starts sending notifications of new messages'),
'#options' => $options,
'#default_value' => $default_value,
'#states' => [
'visible' => [
':input[name="private_messages[receive_notification]"]' => [
'checked' => TRUE,
],
':input[name="private_messages[notify_when_using]"]' => [
'value' => 'no',
],
],
],
];
// Add a custom submit handler so the form values can be saved.
$form['actions']['submit']['#submit'][] = 'private_message_user_form_submit';
}
}