terms_of_use.module in Terms of Use 8.2
Same filename and directory in other branches
Main module file of the terms of use module.
This module adds Terms of Use to the registration page.
File
terms_of_use.moduleView source
<?php
/**
* @file
* Main module file of the terms of use module.
*
* This module adds Terms of Use to the registration page.
*/
use Drupal\Core\Link;
/**
* Implements hook_form_FORM_ID_alter().
*/
function terms_of_use_form_user_register_form_alter(&$form, &$form_state) {
// 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');
$title = $config
->get('terms_of_use_label_name');
$form['terms_of_use'] = [
'#type' => 'details',
'#title' => !empty($title) ? $title : t('Terms of Use'),
'#weight' => 10,
];
// Set the details tag open/close based on the configuration.
$t_and_c_collapsed = $config
->get('terms_of_use_collapsed') ? $config
->get('terms_of_use_collapsed') : 0;
$t_and_c_collapsed = $t_and_c_collapsed ? 0 : 1;
$form['terms_of_use']['#open'] = $t_and_c_collapsed;
$nid = $config
->get('terms_of_use_node');
$node = '';
if (isset($nid)) {
$node = \Drupal::entityTypeManager()
->getStorage('node')
->load($nid);
$language = \Drupal::languageManager()
->getCurrentLanguage();
if ($node
->hasTranslation($language
->getId())) {
$node = $node
->getTranslation($language
->getId());
}
}
$data = '';
$checkbox_title = $config
->get('terms_of_use_label_checkbox');
if (!empty($node)) {
if (!empty($checkbox_title) && strpos($checkbox_title, '@link') !== FALSE) {
$replace = $node
->toLink($node
->label())
->getUrl();
if ($config
->get('terms_of_use_open_link_in_new_window')) {
$attributes = $replace
->getOption('attributes');
$attributes['target'] = '_blank';
$replace
->setOption('attributes', $attributes);
}
$checkbox_title = str_replace('@link', Link::fromTextAndUrl($node
->label(), $replace)
->toString(), $checkbox_title);
}
else {
$node_data = $node
->toArray();
if (!empty($node_data['body'])) {
$data = $node_data['body'][0]['value'];
}
}
}
$form['terms_of_use']['terms_of_use_data'] = [
'#type' => 'markup',
'#markup' => !empty($data) ? $data : '',
];
$form['terms_of_use']['terms_of_use_checkbox'] = [
'#type' => 'checkbox',
'#title' => !empty($checkbox_title) ? $checkbox_title : t('I agree with these terms'),
'#required' => TRUE,
];
}
/**
* Implements hook_entity_extra_field_info().
*/
function terms_of_use_entity_extra_field_info() {
$fields = [];
$fields['user']['user']['form']['terms_of_use'] = [
'label' => t('Terms of use settings'),
'description' => t('Terms of use module form element.'),
'weight' => 10,
];
return $fields;
}
Functions
Name![]() |
Description |
---|---|
terms_of_use_entity_extra_field_info | Implements hook_entity_extra_field_info(). |
terms_of_use_form_user_register_form_alter | Implements hook_form_FORM_ID_alter(). |