You are here

public function LegalAdminTermsForm::buildForm in Legal 8

Same name and namespace in other branches
  1. 2.0.x src/Form/LegalAdminTermsForm.php \Drupal\legal\Form\LegalAdminTermsForm::buildForm()

Module settings form.

Overrides ConfigFormBase::buildForm

File

src/Form/LegalAdminTermsForm.php, line 87

Class

LegalAdminTermsForm
Settings form for administering content of Terms & Conditions.

Namespace

Drupal\legal\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config = $this
    ->config('legal.settings');
  $conditions = legal_get_conditions();
  $multilingual = $this->moduleHandler
    ->moduleExists('language');
  if ($multilingual) {
    $langcode = $this->languageManager
      ->getCurrentLanguage()
      ->getId();
    $conditions = legal_get_conditions($langcode);
    foreach ($this->languageManager
      ->getLanguages() as $key => $object) {
      $languages[$key] = $object
        ->getName();
    }
    $language = $langcode;
    $version_options = [
      'version' => $this
        ->t('All users (new version)'),
      'revision' => $this
        ->t('Language specific users (a revision)'),
    ];
    $version_handling = 'version';
  }
  else {
    $languages = [
      'en' => $this
        ->t('English'),
    ];
    $language = 'en';
    $version_handling = 'version';
  }
  $form['current_tc'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Current T&C'),
  ];
  if (empty($conditions['version'])) {
    $form['current_tc']['no_tc_message'] = [
      '#type' => 'html_tag',
      '#tag' => 'strong',
      '#value' => $this
        ->t('Terms & Conditions are not being displayed to users, as no T&C have been saved.'),
    ];
  }
  else {
    $form['current_tc']['#theme'] = 'legal_current_metadata';
    $form['current_tc']['current_version'] = [
      '#type' => 'item',
      '#title' => $this
        ->t('Version'),
      '#markup' => $conditions['version'],
    ];
    $form['current_tc']['current_revision'] = [
      '#type' => 'item',
      '#title' => $this
        ->t('Version'),
      '#markup' => $conditions['revision'],
    ];
    $form['current_tc']['current_language'] = [
      '#type' => 'item',
      '#title' => $this
        ->t('Language'),
      '#markup' => $conditions['language'],
    ];
    $form['current_tc']['current_date'] = [
      '#type' => 'item',
      '#title' => $this
        ->t('Created'),
      '#markup' => $this->dateFormatter
        ->format($conditions['date'], 'short'),
    ];
    $form['current_tc']['multilingual'] = [
      '#type' => 'item',
      '#markup' => $multilingual,
    ];
  }
  $form['legal_tab'] = [
    '#type' => 'vertical_tabs',
  ];
  $form['terms_of_use'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Terms of use'),
    '#group' => 'legal_tab',
  ];
  $form['terms_of_use']['conditions'] = [
    '#type' => 'text_format',
    '#title' => $this
      ->t('Terms & Conditions'),
    '#default_value' => $conditions['conditions'],
    '#description' => $this
      ->t('Your Terms & Conditions'),
    '#format' => isset($conditions['format']) ? $conditions['format'] : filter_default_format(),
    '#required' => TRUE,
  ];
  $form['registration'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Display Style Registration'),
    '#group' => 'legal_tab',
  ];

  // Override display setting.
  $form['registration']['registration_terms_style'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Display Style'),
    '#default_value' => $config
      ->get('registration_terms_style'),
    '#options' => [
      $this
        ->t('Scroll Box'),
      $this
        ->t('Scroll Box (CSS)'),
      $this
        ->t('HTML Text'),
      $this
        ->t('Page Link'),
    ],
    '#description' => $this
      ->t('How terms & conditions should be displayed to users on the registration form.'),
    '#required' => TRUE,
  ];
  $form['registration']['registration_modal_terms'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Link target'),
    '#default_value' => $config
      ->get('registration_modal_terms') === TRUE ? 1 : 0,
    '#options' => [
      0 => $this
        ->t('New window'),
      1 => $this
        ->t('Modal overlay'),
    ],
    '#description' => $this
      ->t('How to display the T&Cs when a user clicks on the link.'),
    '#required' => TRUE,
    '#states' => [
      'visible' => [
        ':input[name="registration_terms_style"]' => [
          'value' => 3,
        ],
      ],
    ],
  ];
  $form['registration']['registration_container'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Display wrapped with details container'),
    '#default_value' => $config
      ->get('registration_container'),
    '#description' => $this
      ->t('How terms & conditions should be displayed to users on the registration form.'),
  ];
  $form['login'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Display Style Login'),
    '#group' => 'legal_tab',
  ];
  $form['login']['login_terms_style'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Display Style'),
    '#default_value' => $config
      ->get('login_terms_style'),
    '#options' => [
      $this
        ->t('Scroll Box'),
      $this
        ->t('Scroll Box (CSS)'),
      $this
        ->t('HTML Text'),
      $this
        ->t('Page Link'),
    ],
    '#description' => $this
      ->t('How terms & conditions should be displayed to users after the login form.'),
    '#required' => TRUE,
  ];
  $form['login']['login_modal_terms'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Link target'),
    '#default_value' => $config
      ->get('login_modal_terms') === TRUE ? 1 : 0,
    '#options' => [
      0 => $this
        ->t('New window'),
      1 => $this
        ->t('Modal overlay'),
    ],
    '#description' => $this
      ->t('How to display the T&Cs when a user clicks on the link.'),
    '#required' => TRUE,
    '#states' => [
      'visible' => [
        ':input[name="login_terms_style"]' => [
          'value' => 3,
        ],
      ],
    ],
  ];
  $form['login']['login_container'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Display wrapped with details container'),
    '#default_value' => $config
      ->get('login_container'),
    '#description' => $this
      ->t('How terms & conditions should be displayed to users after the login form.'),
  ];

  // Only display options if there's more than one language available.
  if (count($languages) > 1) {

    // Language and version handling options.
    $form['language'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Language'),
      '#group' => 'legal_tab',
    ];
    $form['language']['language'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Language'),
      '#options' => $languages,
      '#default_value' => $language,
    ];
    $form['language']['version_handling'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Ask To Re-accept'),
      '#description' => $this
        ->t('<strong>All users</strong>: all users will be asked to accept the new version of the T&C, including users who accepted a previous version.<br />
                           <strong>Language specific</strong>: only new users, and users who accepted the T&C in the same language as this new revision will be asked to re-accept.'),
      '#options' => $version_options,
      '#default_value' => $version_handling,
    ];
  }
  else {
    $form['language']['language'] = [
      '#type' => 'value',
      '#value' => $language,
    ];
    $form['language']['version_handling'] = [
      '#type' => 'value',
      '#value' => $version_handling,
    ];
  }

  // Additional checkboxes.
  $form['extras'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Additional Checkboxes'),
    '#description' => $this
      ->t('Each field will be shown as a checkbox which the user must tick to register.'),
    '#open' => FALSE,
    '#tree' => TRUE,
    '#group' => 'legal_tab',
  ];
  $extras_count = count($conditions['extras']) < 10 ? 10 : count($conditions['extras']);
  for ($counter = 1; $counter <= $extras_count; $counter++) {
    $extra = isset($conditions['extras']['extras-' . $counter]) ? $conditions['extras']['extras-' . $counter] : '';
    $form['extras']['extras-' . $counter] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Label'),
      '#default_value' => $extra,
    ];
  }

  // Notes about changes to T&C.
  $form['changes'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Explain Changes'),
    '#description' => $this
      ->t('Explain what changes were made to the T&C since the last version. This will only be shown to users who accepted a previous version. Each line will automatically be shown as a bullet point.'),
    '#group' => 'legal_tab',
  ];
  $form['changes']['changes'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Changes'),
    '#default_value' => !empty($conditions['changes']) ? $conditions['changes'] : '',
  ];
  $form['preview_section'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Preview'),
    '#open' => FALSE,
  ];
  $form['preview_section']['preview'] = [
    '#type' => 'container',
    '#tree' => TRUE,
    '#attributes' => [
      'id' => [
        'legal-preview',
      ],
    ],
  ];
  $form['preview_section']['trigger'] = [
    '#type' => 'html_tag',
    '#tag' => 'p',
  ];
  $form['preview_section']['trigger']['preview_button'] = [
    '#type' => 'button',
    '#value' => $this
      ->t('Preview'),
    '#ajax' => [
      'callback' => 'Drupal\\legal\\Form\\LegalAdminTermsForm::preview',
      'event' => 'click',
      'wrapper' => 'legal-preview',
      'progress' => [
        'type' => 'throbber',
        'message' => $this
          ->t('Verifying entry...'),
      ],
    ],
  ];
  return parent::buildForm($form, $form_state);
}