Questions tagged [forms]

Use it for questions about the Form API. Don't use it for entity or entity field forms.

Filter by
Sorted by
Tagged with
51 votes
7 answers
64k views

How to a use a template file to theme a form?

While nodes, comments, blocks and many other things in Drupal are themed using theme template files (like node.tpl.php), forms are a different story. There are no theme template files for forms. How ...
Chaulky's user avatar
  • 6,453
41 votes
3 answers
61k views

Adding CSS and JS to form with attachments

I need to add some external and local CSS and JavaScript files to a form, but I can't seem to find the right way to do this. Do I simply add the paths and URLs for the JS and CSS files? I assume $...
vintorg's user avatar
  • 1,993
41 votes
1 answer
6k views

Why is hook_form_alter() so messy?

Here's an example I just ran into. I'm trying to put some HTML in the label of my field. $form['field_feedback']['und']['#title'] <- has the title $form['field_feedback']['und'][0]['#title'] <- ...
Joren's user avatar
  • 2,440
41 votes
1 answer
19k views

Managed file: the file used in the field may not be referenced

I am having some issues with the managed_file type. Whenever I try to edit and save a form that has a managed_file element in it an error occurs: The file used in the Picture field may not be ...
Bart's user avatar
  • 6,934
38 votes
1 answer
23k views

What is $form_state used for?

What is $form_state typically used for in the context of the Form API if it is used as an argument? Specifically, I'm looking for an example of when it is used.
chrisjlee's user avatar
  • 6,550
35 votes
2 answers
63k views

Custom validation for a form?

I need to add custom validation to a form, lets says add article form. I was expecting something like hook_FORM_ID_validate() to handle this, but cannot find anything relavant in the API ...
gilzero's user avatar
  • 6,037
33 votes
8 answers
26k views

How can I make a form element required with states?

I have a drop down list that displays various fields based on what is chosen and I know that I can toogle the visibility with states but when I try to use required the * span is display but it isn't ...
Sathariel's user avatar
  • 679
32 votes
13 answers
25k views

How can I disable a single checkbox in a 'checkboxes' FAPI element?

Title basically says it all, I want to disable a single checkbox of a checkboxes type FAPI element. I don't want to do if with JavaScript, nor is changing it from checkboxes to multiple checkbox ...
Decipher's user avatar
  • 3,722
31 votes
8 answers
77k views

How do I add a custom validation handler to an existing form/field?

How do I add a custom validation handler to an existing form (or form field) in Drupal 8? I have a form that I haven't created. I want to add my own validation rules on some fields when the form is ...
AngularChef's user avatar
  • 1,005
30 votes
5 answers
58k views

Dynamic select list in the form (dependent dropdown)

I'm using Drupal seven. I want to make the options in a select list be dependent on the value chosen in another select list in a form. I'm sure this has been asked many times before, but I am having ...
Ben's user avatar
  • 1,010
29 votes
3 answers
80k views

Add a custom submission handler to a form

How can I add a custom form submission handler? I tried adding $form['#submit'][] = 'mymodule_form_submit'; or $form['actions']['submit']['#submit'][] = 'mymodule_form_submit'; to hook_form_alter(). ...
Yuseferi's user avatar
  • 22k
29 votes
3 answers
34k views

How do I pass parameters to the form builder?

I have the following routes in module_name.routing.yml. module_name.usergroup_delete: path: 'module_name/usergroup/delete/{arg1}' defaults: _form: '\Drupal\module_name\Form\DeleteUserGroup' ...
KTM's user avatar
  • 2,362
29 votes
2 answers
28k views

How do I set allowed extensions for a managed_file form field?

Not having any luck find this in Drupal documentation or developer examples. I need to set a managed_file form field to only allow zip files in a Drupal 7 custom module.
Garrett's user avatar
  • 393
28 votes
4 answers
23k views

Difference between create new and edit node in form alter

I have an alter section for a particular content type. In the form alter how can I find out whether it is create new form or edit form? With dsm($form) in from alter I can get results with several ...
Yuseferi's user avatar
  • 22k
28 votes
7 answers
56k views

Adding form placeholders to text form elements

Is there a way to add placeholders to a form with a module, or should this be done with a form template?
chrisjlee's user avatar
  • 6,550
27 votes
7 answers
49k views

How to redirect to a page after submitting a form

I have a custom module that displays a form. Upon submitting the form, I want to redirect the user to a different page. How can I do that?
Carlos Muñiz's user avatar
26 votes
9 answers
83k views

How can I find the form ID of a form?

How do I get the form id of my form? I've just begun to make my modules and I'm at a standstill. I got this code from someone else and am trying to customize it: function hook_form_alter(&$form, &...
enchance's user avatar
  • 2,039
25 votes
3 answers
15k views

Multiple Values to Trigger #states

How can I have multiple values trigger the Form API's #states? Say for example, I wanted this field to be visible not only if the value is 5 (currently works below), but wanted to make the field ...
Citricguy's user avatar
  • 7,924
25 votes
5 answers
11k views

Difference between $form_state['values'] and $form_state['input']

Can someone please tell me the difference between these two variables? $form_state['values'] $form_state['input'] Optionally some example of action they are used with. I'm wondering especially about ...
hajnyon's user avatar
  • 395
24 votes
6 answers
40k views

How do I render a form?

What is the alternative to drupal_get_form('user_login'). in Drupal 8? I am using the following code in theme_name.theme, but it doesn't work. // Include namespace for userLoginForm. use \Drupal\...
Kamal Oberoi's user avatar
23 votes
10 answers
12k views

Adding HTML5 placeholder to all drupal forms

I'm trying to add the placeholder attribute to all textfields across my website, but I'm having no luck. I've created a module that overrides hook_form_alter function mymodule_form_alter(&$form, ...
Josua Pedersen's user avatar
21 votes
6 answers
16k views

How to upload multiple files at once?

We need to upload multiple files of different types, is there a module that will check for valid mime types and upload only the valid files, (based on site settings, eg: png, doc, jpg, txt) ?
palbakulich's user avatar
  • 1,367
21 votes
5 answers
29k views

Show forms in a modal window [closed]

I have a form and I am using Drupal 7. I need to show this form in a popup when a user clicks a link. The user should then be able to complete the form whilst in the pop up window. I can use anything ...
Hacker's user avatar
  • 3,024
20 votes
7 answers
52k views

Redirect after form submission

I created a custom form in Drupal 8. I remember we can redirect to custom path in Drupal 7 with $form_state['redirect'] = 'mycustompath'; in a submission handler. How can I achieve the same in Drupal ...
Yuseferi's user avatar
  • 22k
20 votes
3 answers
17k views

specify $form elements to be excluded from display?

I want to hide certain elements in form using form_alter or a template file. I have read somewhere that in Drupal 7 , we can specify $form elements so it will not display in form, but I can't remember!...
Serjas's user avatar
  • 1,357
20 votes
3 answers
31k views

Passing arguments to drupal_get_form()

How can I pass arguments to drupal_get_form() in Drupal 7? function new_menu_callback() { $vars = some_example_function(); // returns: array(1 => 'one', 2 => 'two'); $output = $vars[2]; $...
Citricguy's user avatar
  • 7,924
20 votes
3 answers
8k views

Remove subject field from comments

Is it possible to remove the subject field on comment forms? I find it to be kind of useless and redundant, since the subject of the comment is usually the title of the article being read, at least ...
Lester Peabody's user avatar
20 votes
3 answers
19k views

Form Redirect not working if 'destination' is in URL

In one of my forms, I'm trying to set a $form_state['redirect'] so the form will go to that destination after the user clicks one of the action buttons. If I inspect $form_state['redirect'] both ...
geerlingguy's user avatar
  • 5,483
19 votes
3 answers
16k views

Embed a "node add" form in a page

I'm looking for a way to embed a "node add" form on a landing page. I was using Form Block, but it appears to bring many error because I was using a development snapshot version.
ousneo's user avatar
  • 325
19 votes
1 answer
8k views

Saving a field of type 'text_format' as a system setting variable

I'm attempting to give my content editors the ability to change a variable for a custom module. This variable can have html. I don't want to require them to have any knowledge of HTML, so I want to ...
mrryanjohnston's user avatar
19 votes
4 answers
9k views

Easiest method for conditional fields in a form

What is the easiest way to get some javascript magic for enabling/disabling form fields based on another field's value? This sounds like something there should be an helper for somewhere, but I can't ...
Fuzzy76's user avatar
  • 578
18 votes
2 answers
22k views

How can I upload files with the form element type file?

I want to upload a file and I have to use form element type as 'file'. In submit function I am not able to get any proper data related to the file which I am uploading. SO Can anyone please help me to ...
Suraj's user avatar
  • 884
18 votes
6 answers
32k views

How do I add a CSS class to a form label?

I want to add a CSS class to a label on a page that is using Drupal 8 Form API. I have been unable to find a reference online on how to apply that. I have used the following workaround, but it ...
Steve D.'s user avatar
  • 181
18 votes
3 answers
36k views

How do I show a checkboxes form element that is selected by default?

How do I show a checkboxes form element that is selected by default? I tried setting #default_value as in the following code, but it doesn't work. $form['ios'] = array( '#type' => 'checkboxes', ...
Jurudocs's user avatar
  • 702
18 votes
3 answers
16k views

Add classes to form select option item

How can I add classes to a form option tag without JS? At the moment in Form API I can pass a keyed array like this array( '0' => 'option 0', '1' => 'option 1', ) and I'll get html like ...
Paul Sheldrake's user avatar
17 votes
5 answers
27k views

How do you implement a custom submit handler in hook_form_alter()?

I've been updating one of my Drupal 7 modules to 8 and I'm having an issue with my form alter. I'm adding a validation function and a submit handler. The validation function is getting called just ...
Arbee's user avatar
  • 181
17 votes
3 answers
25k views

How do I alter the form submission handler?

For some reason I need to replace some characters (if exist) in every single textfields in any form just right before they stored in database. In other words I need a hook in which I could get all ...
Pedram Behroozi's user avatar
17 votes
4 answers
28k views

Adding extra fields to Site Contact Form

This seems strangely difficult to me. What I thought would just be a case of adding extra fields a bit like adding fields to Content Types. But no. I can't locate any facility to add say a phone ...
Purplemonkey's user avatar
17 votes
12 answers
56k views

Set select list default value [closed]

I have select list with options: $options = array(0 => '0', 500 => '500', 1000 => '1000'); I'm trying to set it's default value to 500: $form['price']['min']['#options'] = $options; $...
breethe's user avatar
  • 401
16 votes
3 answers
15k views

How to make custom fieldset collapsed by default in Drupal 8

In Drupal 7 there was option to have fieldset collapsible or not, and to restrict default state of this form element (collapsed, or uncollapsed). Example: $form['contact_data'] = array( '#...
Juraj Nemec's user avatar
16 votes
2 answers
11k views

Change $form_state in AJAX callback

I am creating a form which uses the form API '#ajax' attribute, and I need to make changes to $form_state when the AJAX-enabled button is pressed. Here's what I've got: Inside my form builder ...
MW.'s user avatar
  • 311
15 votes
7 answers
20k views

How can I know which button was clicked?

I have a form with a "Submit" and a "Cancel" buttons. In hook_submit(), how do I know which button was clicked?
user1359's user avatar
  • 3,909
15 votes
3 answers
26k views

How can I implement AJAX form submission?

My task is to submit a contact form via AJAX and then to show a "Thank you for submission!" message, loaded in place where the form was. So I need to ajaxify existing contact form. I found ...
Sergey Kravchenko's user avatar
15 votes
3 answers
18k views

Getting the node object in hook_form_FORM_ID_alter

Follow up to this question but I don't have enough reputation to ask clarification in a comment. I am porting a contributed module to Drupal 8. The sugested code on the above mentioned CR and post ...
JoopSJ's user avatar
  • 487
15 votes
2 answers
16k views

"Illegal Choice has been detected" [duplicate]

I am dynamically populating <select> throught javascript in Drupal 7. The system gives me an error on submit: "Illegal Choice has been detected" How to mark, that this <select> must skip ...
user4035's user avatar
  • 1,896
15 votes
1 answer
13k views

How do I use a submit callback on a Drupal admin settings form?

I'm using this code to make my admin form: function custom_orders_admin(){ $form = array(); $form['custom_ignore_terms']['ignore_terms'] = array( '#type' => 'checkboxes', '#title' =&...
Aditya M P's user avatar
15 votes
2 answers
21k views

Form within a block

I'm using drupal_get_form('event_signup_form') to embed my custom form inside a block. The form is being rendered as intended, but submitting the form triggers nothing. The form function itself is ...
kristian nissen's user avatar
15 votes
3 answers
14k views

How do I save uploaded file permanently in file_manged table?

How do I save a uploaded file with status equal to 1 in the file_managed table, in Drupal 8? Whenever I upload a file, it is stored in the file_managed table with status value 0. I have used File::...
Jasodeep Chatterjee's user avatar
15 votes
3 answers
17k views

How do I add an additional submit handler in commerce checkout form?

How do I add an additional submit handler? I have tried this but it did not work. function c_coupon_form_alter(&$form, $form_state, $form_id) { drupal_set_message($form_id); switch($form_id) {...
Ahmad's user avatar
  • 2,835
14 votes
4 answers
17k views

Redirect to a custom page after a node has been added or edited

Using hook_form_alter, how can I redirect a user to a a custom page after creating or editing a node instead of the node that was created or edited? The current code for hook_form_alter() is the ...
Citricguy's user avatar
  • 7,924

1
2 3 4 5
137