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
6
votes
1answer
2k views

How to add taxonomy term reference field in a custom form

In the previous version of Drupal, it was possible to reference taxonomy tags in Drupal's forms using the following option: '#autocomplete_path' => 'taxonomy/autocomplete/'.$vid, However, in the ...
6
votes
2answers
3k views

How to customize the default search block form template?

How can I customize the default search block form template, I now use below template to display a search form block, but this code doesn't allow me to edit and customize each part of this form ...
6
votes
2answers
4k views

How to validate a field value programatically in Drupal 8?

I've created a module for Drupal8 that checks against values in the user register and set an error if the values do no match with something you have entered in a custom form. My module has got a form ...
6
votes
1answer
5k views

How do I use the form hook alt to edit fields of inline entity form?

I am trying to use the hook_form_alter to modify the fields of an Inline entity form in a commerce product display node. I know how to edit a normal field but this does not work for an entity form ...
6
votes
4answers
6k views

Using form_set_error in the submit handler

I have a multistep form and on each submit I send completed fields off to an external API which returns data about the next step of the form. The problem is, if I use form_set_error in the submit ...
6
votes
5answers
6k views

AJAX form - replacing #default_value not working

I have an AJAX form. I want to update the text of a textfield when a button is triggered. But this does not work. My form is below: <?php function my_form($form, &$form_state) { $form['...
6
votes
1answer
2k views

Setting Values for TableSelect checkboxes

Is there anyway to set the default values for checkboxes? What im trying to do is set a specific value for each checkbox so it will delete the selected row in my db, however table select seems to just ...
6
votes
4answers
9k views

How to rename uploaded file using form API and Drupal managed_file field

I make a Drupal Form using FAPI (Drupal 7) in this form I used managed_file field $form['image_fid'] = array( '#title' => t('Image'), '#type' => 'managed_file', '#upload_location' => '...
6
votes
2answers
3k views

How can I change the year range displayed by a FAPI date field?

I have created a FAPI form with an element of #type => date and it works as expected. $form['date'] = array( '#type' => 'date', '#title' => t('Event date:'), ); However, the options ...
6
votes
2answers
3k views

$form['redirect'] = “something=$someId”; doesn't work in Drupal 7

I am here again with a problem! I want to make a form redirect usign a url with ids and somethings else as parameter, but when the code is executed something encode my url, so, if I put something like ...
6
votes
1answer
9k views

User registration form validate and submit hooks not called

I try to add custom validation or submit function to the user registration form, but nothing works. Here is the code in /sites/all/themes/mytheme/template.php: function ...
6
votes
2answers
2k views

Adding a class to select options from Views Exposed Filters

Currently I have a views exposed filter for a "City" field. Typical output looks like: <option value="Liverpool">Liverpool</option> I would love to be able to add a class to this such as:...
6
votes
1answer
5k views

form_state->setRedirect(to_a_new_node_page) from the submitForm function, route “does not exist” issue

I created a custom module, It generate a custom Form in a page. When I submit my form, I create a new node. So, I am redirect to the form page but I would like be redirect to the new node page. I add ...
6
votes
2answers
3k views

How to get Form API #states selected value onchange?

When I'm using #states to set the visibility of a form field I'm checking an input element for its value. Is there then any way to grab the selected value the moment it's selected? In the working ...
6
votes
2answers
4k views

hook_form_alter() exposed filter form item #weight

I'm trying to insert a form element with hook_form_alter(), and then rearrange the form elements so that the new form item appears at a particular point amongst the existing exposed filters. I've ...
6
votes
3answers
6k views

Field Validation and Highlighting

How can I get custom Form API validation to highlight a field the same way the failed #required validation does? (See attached graphic) Note Graphic: 1) (Green Line) Company Name is empty. '#...
6
votes
2answers
5k views

When should the “container” form element be used?

The System module defines the container form element, but that form element is not documented in the Form API Reference. When should that form element be used? What is its purpose?
6
votes
1answer
632 views

How to show a string once you submit a form in a page reloading the page?

I need to create a page with a form and once you enter some values in the form and send it make some calculations and show a string under the form. Something like this image: I know how to create a ...
6
votes
6answers
2k views

Is there an equivalent of view modes for form editing?

I have a content type with a lot of fields. End users need to create content of this type, so I want to provide them with a simplified form so that they can create a content item and edit it without ...
6
votes
2answers
103 views

Is there a standard Drupal way to link to 3rd party configuration forms?

I am writing a custom module. On my module's configuration form I would like to provide links to relevant modules' configuration forms; e.g. the cron configuration form. Is there a standard Drupal ...
6
votes
3answers
7k views

How to add a custom Save & <do some action> button to Node Add/Edit form?

I added the following code in hook_form_alter() and I get my button on the Add/Edit node form: $form['actions']['save_add_lesson'] = array( '#type' => 'submit', '#value' => 'Save & Add ...
6
votes
1answer
2k views

How to add separator option in Form API 'select' element?

Is there a way with the Form API to specify that one or more #options in a 'select' element should be disabled? I would like to insert a separator in my dropdown options that is not a selectable value....
6
votes
2answers
6k views

How to load modal form on page load using ctools?

I have an issue to show modal form on page load. This form will have 3 steps to submit. I've found CTools will suit the best. I'm newbie in drupal, that's why I've looked all tutorials ctools. Every ...
6
votes
3answers
2k views

Adding title attribute to #type password_confirm

Is it possible to add a title attribute through a form_alter to element #type 'password_confirm'? For example, I have a form with the following fields: email password password confirm To apply a ...
6
votes
2answers
5k views

How do I programatically submit a webform via drupal_form_submit?

Drupal 7, Webform 3 I want to programmatically submit a webform via drupal_form_submit. However, I'm having difficulty finding a $form_state structure that works. I've submitted my webform via the ui ...
6
votes
1answer
1k views

How do I append HTML after a form title?

This is the output I want: <label>Name</label><span>!</span> The only way I can think of is to concatenate my HTML to the form title value '#title' => t('Name') . '<...
6
votes
3answers
3k views

How can I change the order of vertical tabs?

More specifically, I am using Location in Drupal 7 to allow people to add location to nodes, but I want it to appear at the top of the vertical tabs fieldgroup.
6
votes
2answers
2k views

How should I handle container form elements?

I have added some custom fields to the register user form and I'm discovered that they are sent back, packaged in a form element of type "container". My code needs to handle all kind of different ...
6
votes
1answer
975 views

Unlimited value field in custom form, with remove button

I'm working on creating an unlimited value field in a custom form, and getting hung up on the remove button. I've been able to create a form that will pull the saved values for the fieldset, then ...
6
votes
5answers
4k views

Content Type vs Webform vs Form API vs Custom Form vs? [closed]

I'm currently working on a new Drupal 7 site, which will be used as a data collection and reporting site. After much research / trial and error / etc, I've decided to bring this question to this forum,...
6
votes
2answers
13k views

Add fields to form dynamically with ajax and retrieve values

I am trying to add a combination of text field and a select box to a form dynamically using Drupal 7 Ajax, when i click on a button. But i have 2 issues here: I am not able to add more than 1 set of ...
6
votes
1answer
1k views

How do I add a menu subtitle to field sets displayed as vertical tabs?

I have formatted my field sets in a form builder function to display as vertical tabs. Here's a snippet of my code. It works fine: $form['vertical_tabs'] = array( '#type' => 'vertical_tabs', );...
6
votes
2answers
600 views

drupal_add_js and drupal_flush_all_caches

I was trying to call drupal_add_js in the callback of a button. It seems, even with after_build, that this can be done successfully only you are working without cached data. So, before i start the ...
6
votes
1answer
3k views

How do I validate form elements in a modal without closing?

I'm using Drupal 8 and I have defined a custom entity type 'my_entity_type', with form handlers defined in the @ContentEntityType annotation: * @ContentEntityType( ... * handlers = { * "...
6
votes
2answers
2k views

User Login Form Error Message Problem

I'm adding the user login form to the homepage of my site and through printing it out in my page.tpl.php, the validation is getting lost a bit. The first time I enter in an incorrect username or ...
6
votes
2answers
4k views

Drupal load form with ajax callback in Bootstrap modal

I need to load a form into a 'bootstrap modal dialog' which will submit data via ajax callback to callback function. The form display properly when the modal dialog is launched: <a data-toggle='...
5
votes
4answers
12k views

How do I set the default value of a datetime form element?

I have a requirement of developing a custom contact form in drupal 8. I am able add all the fields but coming to date time field unable to set the default value to current date. This is my date field ...
5
votes
1answer
9k views

Add javascript to form by module

i am writing a module in drupal 8. I wish to add a javascript when loading that form. function ajax_example_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) { $...
5
votes
2answers
9k views

Removing input formatting options and description from comment form

How do I remove the input formatting options and descriptions from the comment form? I'm not interested in a CSS and/or jquery based solution, nor am I interested in using the Better Formats module, ...
5
votes
3answers
4k views

Is there a way to disallow spaces in the user name registration form in drupal 7?

I hate spaces in usernames, a user can easily add two or more spaces without realizing it. I need to have more control over the username format than Drupal allows by default.
5
votes
1answer
883 views

#states not working properly in IE 9

I am using #states form API to do the show hide animation of one element based on the values of another form element.like follows, $price['#states'] = array( 'visible' => array( ':...
5
votes
7answers
18k views

How to add reset button to forms?

I've been researching this matter for a while with no clear answer yet. So, here are a few questions ... I'm assuming (correct me if I'm wrong) that Drupal 7 doesn't support reset buttons. It ...
5
votes
4answers
7k views

Creating node forms

Does anyone know how to create a node form for specific Content Types? Instead of clicking Content > Add Content > Content Type they can just, I don't know, go to the sidebar and fill out the info ...
5
votes
4answers
8k views

How do I prevent a form from being cached for anonymous users?

I have a form that is pre-populated with information specific to an anonymous user's session information. However, the form appears to get cached for all anonymous users by default. 'Internal Dynamic ...
5
votes
4answers
2k views

How do I find a Form_ID for hook_form_FORM_ID_alter?

I want to alter the form for a content type I created for videos. The type is called Videos. How would I use hook_form_alter to alter the form to create the content?
5
votes
2answers
743 views

altering a hook_form_alter function

Is it possible to use hook_form_alter() on a hook_form_alter() function already defined in another module? For example, if I want to alter the output of the following hook from the menu module, can I ...
5
votes
2answers
3k views

What is the difference between hidden and value types in form API?

In Drupal's Form API page mentioned both hidden and value will not display to user. What is the difference between them?
5
votes
2answers
10k views

File upload with ajax

Is there any example how to set up form element with type file to upload it via AJAX. I want to be same as field image in node type article. thx
5
votes
3answers
5k views

How to abort the ajax request and redirect in the ajax callback of form element

I want to redirect the page in the ajax callback function, and nothing (like "header", "drupal_goto" etc.) works for redirect in the ajax callback function.
5
votes
2answers
4k views

How to change a node add form without changing the node edit form

I'm using drupal 7 I'm writing a custom hook_form_alter something like this function mymodule_form_alter(&$form, &$form_state, $form_id){ if($form_id == "rating_node_form") { ...