0

If creating something like a form, but also perhaps any piece of front-end UI, what is the correct approach for adding things like extra Javascript (or HTML, or anything)?

I'm putting together a registration form using the Entity Registration module and would like to know if, say, I wanted to have a 'Delivery address is the same as invoice address' checkbox that dynamically hid/revealed the delivery address part of the form, what would be the Drupal way of achieving that?

0

You could implement hook_form_alter for adding a checkbox field to your form.

The most simple thing to do after that is to handle the display of the form elements with JS/CSS (checked or not). You have to handle the validation of the different submissions of the form (with or without the delivery address part).

Here you can find a tutorial for adding custom validation to a form.

For the JS/CSS part, you can put your code in the main files of your theme, or add specific files for this form. Check this answer for the second solution.

  • Thanks for this, very helpful - is hook_form_alter the correct place to access $form['#attached']? I've added the function to my sub-theme's template.php, it seems to execute, but adding files to the $form['#attached']['css'] array, for example, doesn't seem to do anything. I can't tell from the docs if I'm doing the right thing though – toby1kenobi Dec 5 '16 at 14:42
  • Ok, got it working - not 100% sure template.php is the right place for this code, seems to work though. Thanks! – toby1kenobi Dec 5 '16 at 15:18

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.