public function UserSettings::buildForm in Notify 1.0.x
Same name and namespace in other branches
- 8 src/Form/UserSettings.php \Drupal\notify\Form\UserSettings::buildForm()
- 2.0.x src/Form/UserSettings.php \Drupal\notify\Form\UserSettings::buildForm()
Form constructor.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Return value
array The form structure.
Overrides ConfigFormBase::buildForm
File
- src/
Form/ UserSettings.php, line 70
Class
- UserSettings
- Defines a form that configures forms module settings.
Namespace
Drupal\notify\FormCode
public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL) {
$config = $this
->config('notify.settings');
$userprofile = \Drupal::routeMatch()
->getParameter('user');
$account = \Drupal\user\Entity\User::load($userprofile);
if (!is_object($account)) {
return;
}
$result = \Drupal::database()
->select('users', 'u');
$result
->leftjoin('users_field_data', 'v', 'u.uid = v.uid');
$result
->leftjoin('notify', 'n', 'u.uid = n.uid');
$result
->fields('u', [
'uid',
]);
$result
->fields('v', [
'name',
'mail',
]);
$result
->fields('n', [
'node',
'teasers',
'comment',
'status',
]);
$result
->condition('u.uid', $userprofile);
$result->allowRowCount = TRUE;
$notify = $result
->execute()
->fetchObject();
// Internal error.
if (!is_object($notify)) {
$notify = NULL;
}
$form = [];
if (!$notify->mail) {
$url = '/user/' . $userprofile . '/edit';
$this->messenger
->addMessage($this
->t('Your e-mail address must be specified on your <a href="@url">my account</a> page.', [
'@url' => $url,
]), 'error');
}
$form['notify_page_master'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Master switch'),
];
// If user existed before notify was enabled, these are not set in db.
if (!isset($notify->status)) {
$notify->status = 0;
$notify->node = 0;
$notify->teasers = 0;
$notify->comment = 0;
}
if (\Drupal::service('module_handler')
->moduleExists('advanced_help')) {
$output = theme('advanced_help_topic', [
'module' => 'notify',
'topic' => 'users',
]);
}
else {
$output = '';
}
$form['notify_page_master']['status'] = [
'#type' => 'radios',
'#title' => $this
->t('Notify status'),
'#default_value' => $notify->status,
'#options' => [
$this
->t('Disabled'),
$this
->t('Enabled'),
],
'#description' => $output . ' ' . $this
->t('The master switch overrides all other settings for Notify. You can use it to suspend notifications without having to disturb any of your settings under “Detailed settings” and “Subscriptions”.'),
];
$form['notify_page_detailed'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Detailed settings'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#description' => $this
->t('These settings will only be effective if the master switch is set to “Enabled”.'),
];
$form['notify_page_detailed']['node'] = [
'#type' => 'radios',
'#title' => $this
->t('Notify new content'),
'#default_value' => $notify->node,
'#options' => [
$this
->t('Disabled'),
$this
->t('Enabled'),
],
'#description' => $this
->t('Include new posts in the notification mail.'),
];
$form['notify_page_detailed']['comment'] = [
'#type' => 'radios',
'#access' => \Drupal::service('module_handler')
->moduleExists('comment'),
'#title' => $this
->t('Notify new comments'),
'#default_value' => $notify->comment,
'#options' => [
$this
->t('Disabled'),
$this
->t('Enabled'),
],
'#description' => $this
->t('Include new comments in the notification mail.'),
];
$form['notify_page_detailed']['teasers'] = [
'#type' => 'radios',
'#title' => $this
->t('How much to include?'),
'#default_value' => $notify->teasers,
'#options' => [
$this
->t('Title only'),
$this
->t('Title + Teaser/Excerpt'),
$this
->t('Title + Body'),
$this
->t('Title + Body + Fields'),
],
'#description' => $this
->t('Select the amount of each item to include in the notification e-mail.'),
];
$set = 'notify_page_nodetype';
$form[$set] = [
'#type' => 'fieldset',
'#title' => $this
->t('Subscriptions'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#description' => $this
->t('Tick the content types you want to subscribe to.'),
];
$alltypes = \Drupal\node\Entity\NodeType::loadMultiple();
$enatypes = [];
foreach (\Drupal\node\Entity\NodeType::loadMultiple() as $type => $object) {
if ($config
->get(NOTIFY_NODE_TYPE . $type)) {
$enatypes[] = [
$type,
$object
->label(),
];
}
}
if (\Drupal::currentUser()
->hasPermission('administer notify queue') || empty($enatypes)) {
$enatypes = [];
foreach ($alltypes as $type => $obj) {
$enatypes[] = [
$type,
$obj
->label(),
];
}
}
//TODO: FIX '_notify_user_has_subscriptions' LATER ON
$exists = _notify_user_has_subscriptions($userprofile);
if ($exists) {
// Custom subscriptions exists, use those.
foreach ($enatypes as $type) {
$field = \Drupal::database()
->select('notify_subscriptions', 'n')
->fields('n', [
'uid',
'type',
])
->condition('uid', $userprofile)
->condition('type', $type[0])
->execute()
->fetchObject();
$default = $field ? TRUE : FALSE;
$form[$set][NOTIFY_NODE_TYPE . $type[0]] = [
'#type' => 'checkbox',
'#title' => $type[1],
'#return_value' => 1,
'#default_value' => $default,
];
}
}
else {
// No custom subscriptions, so inherit default.
foreach ($enatypes as $type) {
$form[$set][NOTIFY_NODE_TYPE . $type[0]] = [
'#type' => 'checkbox',
'#title' => $type[1],
'#return_value' => 1,
'#default_value' => TRUE,
];
}
}
$form['uid'] = [
'#type' => 'value',
'#value' => $userprofile,
];
return parent::buildForm($form, $form_state);
}