You are here

terms_of_use.module in Terms of Use 8

Same filename and directory in other branches
  1. 8.2 terms_of_use.module
  2. 6 terms_of_use.module
  3. 7 terms_of_use.module

This module adds Terms of Use to the registration page

Alter the user form

File

terms_of_use.module
View source
<?php

/**
 * @file
 * This module adds Terms of Use to the registration page
 *
 * Alter the user form
 */
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\Language;
use Drupal\Component\Utility\Xss;
use Drupal\node\Entity\Node;

/**
 * Implements hook_form_FORM_ID_alter
 */
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,
    ));
  }
}

Functions

Namesort descending Description
terms_of_use_form_user_register_form_alter Implements hook_form_FORM_ID_alter