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
7
votes
2answers
11k views

Rebuild form AFTER ajax callback has run

I have a form with a button that when pressed re-builds a different part of the form via an ajax request. Upon clicking the button the form is rebuilt, then my ajax callback is called which saves the ...
7
votes
3answers
6k views

File save upload doesn't replace existing file despite FILE_EXISTS_REPLACE

I'm trying to force replacement of existing file with same name, if a new version is uploaded by user. I've seen a lot of posts about this topic but none that I could find addresses how to force the ...
7
votes
1answer
514 views

What is the best choice to create a node through code?

The scenario goes this way, I have a node type which has lot of Taxonomy term fields in it. It does also have other fields like text, date etc but 90% of it is a taxonomy field. I am aware that a node ...
7
votes
3answers
9k views

How do I create a progress bar?

I need to implement a progress bar using Drupal 7. This is the code I am actually using. $form[$name][$name.'_enc'] = array( '#type' => 'submit', '#value' => t($name.' Enable'), // '#...
7
votes
2answers
4k views

Drupal AJAX fires only once

In Drupal 7, I have a form where I would like to do the following: Display a list of options in a simple two column table Selecting an action from a drop down list fires AJAX and replaces options in ...
7
votes
2answers
2k views

(Un-) formatting each form element

When we declare something like $form['textfield'] = array( '#type' => 'textfield', '#title' => 'textfield title', '#maxlength' => 8, '#required' => true, ); The output will be ...
7
votes
1answer
1k views

How to speed up node add form with around 50 fields?

I had a node add form with around 50 fields, mostly text with 1 field collection (with another 5 fields) and a few entity and term references. The page is taking around 30 seconds to load making it ...
7
votes
1answer
3k views

AJAX callback function never called

I'm having trouble with something incredibly simple: When I select an element in a multiselect-form element, I want a textfield to become enabled. The problem I'm having is that Drupal never manages ...
7
votes
3answers
777 views

How to emulate all the AJAX functionality when the form is rendered after an AJAX callback?

I have a Drupal form, but I am adding it to the page as the result of an AJAX callback. I need this form to trigger a new callback. My code below: $form['#suffix'] = '<div id="form-return-...
7
votes
1answer
648 views

retaining upload file information in form when validation fails on other fields

If there are required fields in a form (ie '#required' is set to TRUE), and the user sets a file to be uploaded but not provide all the required fields, validation fails on the required fields and the ...
7
votes
1answer
871 views

Multisteps Webform loses current page when refreshing browser

I've created a Webform with three steps. When I refresh the browser (F5) on step 1 and 2, I correctly remains in the same page. If I do the same in step three, I go back to step 2. For ALL the steps ...
6
votes
2answers
32k views

How to use Form_submit hook?

I have a form, automatically generated by a content type. I want to run a form_submit function to input/change some data to get saved in the database. The content-type already has these fields setup, ...
6
votes
7answers
8k views

How to replace the search submit button with a Font Awesome character?

I would like to replace the search submit button with a Font Awesome character like the Magnifier (Search Icon) character, and put Search the site inside the search box. How can I achieve something ...
6
votes
2answers
13k views

Custom text/html in custom form module

I am following this tutorial and I've already created my custom form as Drupal 8 module. I wonder, how to add custom text/html in form? I mean when somebody goes to the path pointed in *.routing.yml, ...
6
votes
4answers
23k views

add class to form element

function ajax_register_form_user_login_alter(&$form, &$form_state, $form_id) { $form['fbuser'] = array( '#type' => 'item', '#markup' => 'You have a facebook account?', ...
6
votes
5answers
4k views

Make all the fields in a form not required

I know that, when I want to run off the #require property of a field in hook_form_alter(), I just need to set #require property of a specific field to FALSE. How can I remove the #required property ...
6
votes
3answers
8k views

How do I programmatically generate an entity form?

I need to programmatically generate the entity form for my custom entity - basically get the same output that happens when you call \Drupal::formBuilder()->getForm('form namespace') When I try ...
6
votes
3answers
6k views

How to add contact us form in to front-page?

I need to add contact form in the front page but I don't know how. there are some modules like Contact Form On Node that inserts this form in the node page but I only want it to be in the front page. ...
6
votes
4answers
14k views

Add arguments to custom submit

I added an extra submit-function to my form: $submit = array('_check_value_public_catalog'); $form['actions']['submit']['#submit'] = array_merge($submit, $form['actions']['submit']['#submit']); So ...
6
votes
1answer
6k views

How do I load the node create or edit form?

To load the login form I am using the following code. $form = \Drupal::formBuilder()->getForm('Drupal\user\Form\UserLoginForm'); I would like to load the form to create or edit a node. How can I ...
6
votes
2answers
7k views

How do I resolve the “Argument 1 passed to drupal_attributes() must be an array” error on my system_settings_form()?

I don't usually post specific questions like this, and ask for help on a specific issue but I am at the end of my rope wasting time way... I have a simple D7 module as seen below, when I load the ...
6
votes
1answer
5k views

How to change layout of node add/edit form with Display Suite?

Is there a way to use Display Suites to change a Layout for a Node Edit/Add form? i.e. form at: .../node/add/
6
votes
1answer
5k views

How to alter the “Request new password” form validation

I have adapted the login procedure with hook_form_FORM_ID_alter so that another database is also checked for credentials. Now I need to change the forgot password validation as well. It seems the ...
6
votes
4answers
8k views

Using theme_image_style()

I am trying to display thumbnails of uploaded images in a custom form. While I can see the images in my form array after uploading them to the tmp directory in Drupal, here is what I am trying to use ...
6
votes
3answers
14k views

Drupal 7 custom form submit handler is not being called

I am trying to write a custom handler for one of the Drupal 7 forms: function mymodule_formid_alter(&$form, &$form_state, $form_id) { array_unshift($form['#submit'], 'mymodule_formid_submit'...
6
votes
5answers
3k views

Permissions Administration Module

Is there a working module for Drupal 7 to restyle admin/people/permissions/list to add more usable controls? Some serious boons to building a large site with lots of permissions: Incremental search/...
6
votes
4answers
9k views

How to set multiple error messages with form_set_error?

I notice I can pass in a message variable to form_set_error(). How can I pass in multiple messages? I tried calling form_set_error() several times, but always the first one is displayed. So, to sum ...
6
votes
2answers
4k views

Clear form fields after AJAX submit

Given: I am using Drupal 8. I have a custom "add form" of the custom entity type. Form has an ajax submission. Problem: When I submit form all the values remain in fields. Expected: Form fields ...
6
votes
2answers
4k views

how to remove password confirmation field in user registration form?

I want to remove password confirmation field. Its ok if user enters the password only once. There is no need to confirm it again. I am using this code <?php function dp_form_alter(&$form, &...
6
votes
4answers
3k views

Remove drupal parameters from form submitted via GET

I have form which is submitted via GET method. The problem is that after submit the URL looks very bad, with lots of parameters like form_build_id & form_token. How can I get rid of them? Thanks
6
votes
2answers
333 views

Difficulties with the #ajax form item attribute

My form item is the following one: $form['foo'] = array( '#title' => t('Foo'), '#type' => 'radios', '#options' => $options, '#description' => t('Lorem ipsum.'), ...
6
votes
2answers
376 views

What difference between declaration of an ajax callback function with $this and without it?

There is a form working with AJAX Framework. And there is a form element having the callback function named "myFunction", which is a public method of the form class. The callback can be declared as '...
6
votes
1answer
3k views

Adding check boxes to contact form

I am attempting to make a contact form in Drupal 8 using "Contact forms" included in core. I'm struggling to figure out how to really customize it, like adding radio buttons or check boxes - does ...
6
votes
1answer
1k views

How do I add a Site Information variable and make it multilingual?

So I've used form alter to add in a site information variable (which auto-saves because it's a system_settings_form), but I can't seem to get it to show up as multilingual. Here is my form alter to ...
6
votes
4answers
5k views

Form api: Combine two text fields into a div?

Using the form api is it possible to combine two text fields onto the same row? E.g. form <?php function foo_form($form, $form_state) { $form = array(); $form['range'] = array( '#type' =...
6
votes
2answers
1k views

Difference between hide() and ['#access'] = FALSE

I want to remove the delete button from a standard edit form in certain cases and I'm looking for the right way to do that. So far I found that hide() marks an element as already rendered so it ...
6
votes
3answers
7k views

Get node ID in a form submit [closed]

I am struggling with how to get node id in my user_register_form_submit. I need to get the node id of the node where my form is, let me explain : I have created a tpl for my user register form in ...
6
votes
3answers
9k views

Form API Select List will not show empty option

I am trying to add a form that forces you to select an option from a dropdown. The documentation says: If #required is TRUE and there is no #default_value, an empty option is added to the select ...
6
votes
1answer
2k views

Multi-value form widget in a system settings form

I'm building a form for a modules configuration page. The last line of the form callback is the following: return system_settings_from($form); I want to add a textfield to the form, but I want to be ...
6
votes
2answers
6k views

How to extend or “hook” Drupal Form AJAX?

I'm working with standard admin form, which has 'add_more' button for one of fields. My task is to move some fields to jQuery UI Dialog just for usability. When dialog was closed I manually append it'...
6
votes
1answer
1k 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
2k 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
8k 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 ...