3

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 <theme_name>_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id) {
    case 'field_config_edit_form':
      $form['default_value']['widget'][1] = array(
        '#type' => 'checkbox',
        '#title' => t('Your checkbox'),
        '#size' => 10,
        '#maxlength' => 255,
        '#default_value' => 1,
        '#required' => TRUE,
        '#description' => '<p>' . t('Your checkbox altered.') . '</p>',
    );
    break;
  }
}

And I need this done on all contact forms of the site

5
  • What form? What code of the linked answer didn't work exactly? Please try to provide an MCVE.
    – leymannx
    May 14 '18 at 20:05
  • sorry for the lack of clarity, I edited the question now with the code sample
    – Yoosuf
    May 15 '18 at 5:59
  • Checkbox doesn't have #size and #maxlength.
    – leymannx
    May 15 '18 at 6:36
  • Apart from that it is Drupal 7 sample code which won't work in D8.
    – leymannx
    May 15 '18 at 6:37
  • Oh ok, so then how is it done in Drupal 8?
    – Yoosuf
    May 15 '18 at 6:42
4

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(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {

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

  if ($form_id == 'MY_FORM_ID') {

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

Check out this Introduction to Form API.

7
  • The form ID is supposed to be the machine name of the form right? I tried the above code but it still doesn't work for me. I put it in the themename.theme file and changed the function title accordingly
    – Yoosuf
    May 15 '18 at 7:12
  • Sure, the uppercase strings are supposed to be replaced by you. Maybe you want to install Kint (the Devel submodule) and then do ksm($form_id) to figure out the right ID.
    – leymannx
    May 15 '18 at 7:27
  • Hi, I tried to output the form ID using kint and ksm as you suggested but it doesn't display the form id. I included the snippet in the contact page twig file and even in the form twig file but it only displays the "website encountered unexpected error" page after clearing the cache. However if I use {{ kint(page) }} it shows a list of all variable but form related variables are not there. Am I doing something wrong here?
    – Yoosuf
    May 15 '18 at 8:19
  • @Yoosuf – Yes. Updated answer code. ksm in the form_alter function. Not in Twig. Then flush cache.
    – leymannx
    May 15 '18 at 8:28
  • Ok thanks a lot it working now, but there is an issue as this checkbox was added via code I can't seem to move it to the bottom of the form. Is there any way to manually arrange the form elements or move this checkbox to the end of the form? And second ques, how do I make a part of the text (after the checkbox) to link to a page on the site and make it required field?
    – Yoosuf
    May 15 '18 at 8:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.