How to add "Agree to terms" checkbox in Drupal 8 form I've tried several solutions starting from the Boolean checkbox field in the Drupal 8 contact form to this answer, this answer and even this module but none of them work for me.

Basically I just want to display a checkbox which displays the text, "I agree to the terms and conditions of this site", and the words "terms and conditions" needs to be linked to its respective page on the site - to open in a new tab.

Any ideas on how this could be achieved in Drupal 8, via code or even a module is greatly appreciated

This is the code which I tried using in the .theme file

function &lt;theme_name&gt;_form_alter(&amp;$form, $form_state, $form_id) {
  switch ($form_id) {
    case 'field_config_edit_form':
      $form['default_value']['widget'][1] = array(
        '#type' =&gt; 'checkbox',
        '#title' =&gt; t('Your checkbox'),
        '#size' =&gt; 10,
        '#maxlength' =&gt; 255,
        '#default_value' =&gt; 1,
        '#required' =&gt; TRUE,
        '#description' =&gt; '&lt;p&gt;' . t('Your checkbox altered.') . '&lt;/p&gt;',
    );
    break;
  }
}


And I need this done on all contact forms of the site
Your code was D7 and what should a checkbox do with #size and #maxlength? :)

This answer presumes you have the Devel submodule Kint installed.

/**
 * Implements hook_form_alter().
 */
function THEME_form_alter(&amp;$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {

  ksm($form_id); // Remove when done.

  if ($form_id == 'MY_FORM_ID') {

    $form['tos'] = [
      '#type'          =&gt; 'checkbox',
      '#title'         =&gt; t('Terms and Conditions'),
      '#required'      =&gt; TRUE,
      '#default_value' =&gt; TRUE,
    ];
  }
}


Check out this Introduction to Form API.