function terms_of_use_form_user_register_form_alter in Terms of Use 8
Same name and namespace in other branches
- 8.2 terms_of_use.module \terms_of_use_form_user_register_form_alter()
- 7 terms_of_use.module \terms_of_use_form_user_register_form_alter()
Implements hook_form_FORM_ID_alter
File
- ./
terms_of_use.module, line 18 - This module adds Terms of Use to the registration page
Code
function terms_of_use_form_user_register_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// Administrative users can skip this. So admin/user/user/create won't show
// the terms of use.
if (\Drupal::currentUser()
->hasPermission('administer users')) {
return;
}
$config = \Drupal::config('terms_of_use.settings');
// Getting the nid of the the Terms of Use node.
$node_id = $config
->get('node_id');
// Get the language code
$langcode = \Drupal::languageManager()
->getLanguage(Language::TYPE_CONTENT);
// A nid for the desired language was found.
if ($node_id) {
// Adding the fieldset.
$fieldset_name = Xss::filter($config
->get('fieldset_name'));
$checkbox_label = Xss::filterAdmin($config
->get('checkbox_label'));
$form['terms_of_use'] = array(
'#type' => 'fieldset',
'#title' => $fieldset_name,
'#weight' => 10,
);
// Getting the node through its nid.
$node = Node::load($node_id);
$translation = \Drupal::entityManager()
->getTranslationFromContext($node, $langcode);
// If we were able to load the node.
if ($node->nid) {
// If we find @link in the text for the terms checkbox we just show a
// link. Otherwise we show the full terms.
if (strpos($checkbox_label, '@link') !== FALSE) {
$checkbox_label = str_replace('@link', \Drupal::l($node
->label(), 'node/' . $node->nid), $checkbox_label);
}
elseif ($items = $translation
->get('body')) {
// Limit the height of the body text with css.
$form['#attached']['library'][] = 'terms_of_use/terms_of_use';
$form['terms_of_use']['terms_of_use_text'] = $node->body[0]
->view('full');
}
else {
\Drupal::logger('terms_of_use')
->notice('The body field of the "terms of use" node was empty. Please check the the nodes content.');
}
// Adding the checkbox to the fieldset.
$form['terms_of_use']['terms_of_use'] = array(
'#type' => 'checkbox',
'#title' => $checkbox_label,
'#required' => TRUE,
);
}
else {
\Drupal::logger('terms_of_use')
->notice('The "terms of use" node could not be loaded. Please check the settings and if the node exists.');
}
}
else {
\Drupal::logger('terms_of_use')
->notice('No node is set as "terms of use" in the current language [@lang].', array(
'@lang' => $langcode,
));
}
}