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

How would I disallow special characters in usernames?

By default, Drupal 9 allows usernames to have spaces, periods(.), hyphens(-), apostrophes('), underscores(_) and @ symbols. How would I disallow spaces, periods, apostrophes and @ symbols?
0 votes
2 answers
87 views

How can I alter the login form in the login block but not in the login page?

How should I use hook_form_alter() to modify the user login block but not the user login form page? Everything I've tried either modifies both entities or doesn't work, not even using the following ...
0 votes
0 answers
27 views

If i have to add a content in drupal through custom form which table in database should be used? [duplicate]

I have created a custom Form through form api which only contains one form field of adding image to node and their will be a different form through which i am going to add body , title and tags in ...
0 votes
1 answer
32 views

How can I access or expose the Commerce data in $order->data[] to the Rules module?

Following this old checkout_pane_example.module tutorial, I created a custom Drupal 7 Commerce checkout pane. It's simply a text field to collect special order instructions from the customer. ...
  • 3,794
0 votes
1 answer
112 views

Link to images in form view creates wrong url

I noticed this after drupal 9 update, but I've tried to find the exact moment when it was broken and I couldn't find it. I use S3 and cloudfront for images so maybe the problem is related with that ...
  • 83
0 votes
1 answer
45 views

Templating the user edit page

i'm using the theme startupgrowth lite and i want to theme my user edit page , only when the user is a 'testeur' and he's editing the 'candidate user profile'. i know that when i want to modify the ...
  • 1
1 vote
0 answers
72 views

How to reload flagging form via ajax?

My task is to ajaxify complex flag form (with additional fields). I need this because the default ajax flag does not dupport fiedls. I need to have the flagging form for create flag and then update it ...
-1 votes
2 answers
344 views

How do I sanitize the uploaded files name for sending safely as email attachments?

I have create a form that upload a file. This file is send as email attachment .. $form['cv'] = [ '#type' => 'managed_file', '#title' => 'Votre CV', '#...
  • 3,109
0 votes
1 answer
76 views

After rendering entity form how can be modified?

I rendering a custom entity form display with the following code: $form = $this->entityFormBuilder->getForm($entity, 'array_delimiter'); This is perfect, as it shows what I set on the form ...
2 votes
1 answer
832 views

Form with two submission buttons

I try to create a form with two submit buttons and each button will use a different function: the default submitForm function and a custom submitFormEndSession function. So I have this code: class ...
-1 votes
1 answer
681 views

InvalidArgumentException: Class ... does not exist in custom configuration form [closed]

I create configuration form, so that I could dynamically set the API key for my custom weather module. But when I write in the url address http://drupalsite/admin/config/services/weather/settings I ...
0 votes
0 answers
287 views

Batch Error: Using $this when not in object context, so Can I call batch method in non static way?

The existing code when a form is submitted I execute this batch : /** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $node_titles = ['node1', '...
0 votes
1 answer
55 views

specific submit handler to user form

I have a submit handler to the user form. It redirect the user to a specific page. use Symfony\Component\HttpFoundation\RedirectResponse as RedirectResponse; /** * @file * Primary module hooks for ...
1 vote
2 answers
379 views

Confirmation message on comment submission

When a comment is submitted, no confirmation message is shown and I would like to achieve this. I want to show the following message. Your comment has been added for review and approval. I tried ...
  • 33
1 vote
0 answers
68 views

Can't save value of custom datetime field

I'm reaching out because I just can't find out why my custom datetime field isn't saving its value after I submit. Context: I've created a field containing two datetime fields. I will use it to add or ...
  • 11
0 votes
1 answer
55 views

Time spent with custom module

Can anyone help me with this, I want to activate time spent module on my custom form module, and store the time spent on a field on my form. I think that I could do it on the file timespent.module ...
  • 1
3 votes
1 answer
60 views

How to not split select list by content type

This is about the regular select widget that you can choose in the Manage form display page. If this widget is linked to an entity_reference field allowing several bundles to be selected; then you end ...
  • 438
2 votes
1 answer
45 views

How to alter the view behind a select widget in a regular node edit form

I have an entity_reference field in a content type: field_node_ref In the field definition, it can reference all the nodes of a given type. The widget used for this field is a select widget Depending ...
  • 438
0 votes
1 answer
36 views

Add default value to existing field

I have tried to add default value to an existing field using hook_form_alter() but it does not work. Below is the html of the fields <fieldset class="js-form-item js-form-type-textfield form-...
user avatar
1 vote
1 answer
314 views

Adding an "Accept Terms" checkbox on User Registration form

I need to add an "Accept terms and conditions" checkbox onto a user registration form. What is the best way to do this? Also, it seems odd to me that there is no easy way to use radio ...
0 votes
0 answers
47 views

Altering user_admin_account and user_register_form

I'm pretty new to Drupal and I'm trying to build a Drupal 7 custom module to alter the user listings table in admin/people. I would like to display the first and last names of the users. I've already ...
0 votes
1 answer
120 views

Validation for radio inputs don't work for me

I'm building a form with the drupal forms api. Validation works well for all my other fields. But I've started to try and work with radio buttons too now. It doesn't matter if I click on a radio ...
-1 votes
1 answer
1k views

Formatting date field with the Form API

Hi there I'm using drupal 9.x and the forms api to generate a dynamic form. Altough I'm living in the Netherlands where the date format standard is 'dd/mm/yyyy'. I've looked around on the internet for ...
2 votes
1 answer
185 views

Block Settings inside Form

Please I need help with my code. I have a form and a block created programmatically. The block embeds the form to show it on the frontend. The block also has some settings (form preffix text). I want ...
1 vote
1 answer
29 views

Place field in "Url Alias" advanced group section (path-0)

I'm trying to place my node field to the Advanced group - URL alias section. I'm doing it in custom hook_form_BASE_FORM_ID_alter(), where I'm trying to set custom #group like this: $form['my_filed']['#...
0 votes
1 answer
481 views

Use ajax to save modal / dialog form instead of page reload

I'm looking for a way to save modal & dialog forms through ajax instead of redirecting the user after the form submit. Ajax is quite unknown territory to me. I've found several interesting topics ...
  • 92
0 votes
1 answer
26 views

How do I add a field to a node form?

The goal is to set a default counter, but let people override it. The counter should be the biggest value of the field plus 1. Using an event subscriber, FORM ALTER event. public static function ...
  • 1,385
0 votes
1 answer
50 views

Pager element not showing on form

I have a simple tableselect element inside my form and the tableselect rows are populated using a pager as follows: $pagerLimit = 10; //default value if nothing is passed in $query = $this->...
  • 105
2 votes
1 answer
64 views

Why can't some fields be moved within details element?

I am trying to move 2 form fields to the advanced sidebar: dismissible and status. Status was already part of the meta container and setting #group='meta' on dismissable also placed it there; but ...
  • 1,008
0 votes
1 answer
127 views

Custom validation constraints multi value paragraphs : how to get weight value

I am validating a multi value paragraph field using constraints. /** * Implements hook_entity_bundle_field_info_alter(). */ function cnfpt_internet_entity_bundle_field_info_alter(&$fields, \...
  • 69
0 votes
1 answer
171 views

Custom constraints validation of multi value paragraphs: highlighting missing field

I am using constraint for the validation my multi value paragraph field. I can correctly implement the different validation rules but I haven't found out how to highlight only the erroneous field(s). ...
  • 69
0 votes
1 answer
865 views

Build custom form with entity form field

I have a feature to migrate from Drupal 7 to Drupal 9. This allows me to build a custom form made up of static fields, which I define in the code of my module, and fields dynamically retrieved from an ...
0 votes
1 answer
26 views

Using both Drupal Core Contact Form and contributed Webform module

I have a Drupal 8 site already utilizing core Contact Form. However, we now need a more complex form and thinking about installing a contributed module: Webform (https://www.drupal.org/project/webform)...
  • 1
0 votes
0 answers
163 views

Set ['#default_value'] of taxonomy term reference field via hook_form_alter()?

Here's a different question with the same taxonomy term reference field I mentioned in my previous question. Users with Administrator and Coach roles are granted permission to register Student users ...
  • 2,052
-2 votes
1 answer
529 views

How to add anti csrf token to the forms (No Anti-CSRF tokens were found in a HTML submission form.)

When i was testing my drupal 9 website for security i got the error as "No Anti-CSRF tokens were found in a HTML submission form." solution is given as "use anti-CSRF packages such as ...
0 votes
1 answer
72 views

entity_form how to programmatically add references and update the form visually (entity_browser)

I´m having an fieldable entity with a reference field to paragraphs field_paragraphs and a paragraph_type with a reference field to content_type event field_events. What i am trying in the entity form ...
0 votes
1 answer
51 views

How to retrieve #tree values inside multiple containers?

I am using Drupal 8 and I am creating multiple forms that are made of a #tree hierarchy. I need to retrieving the value of the "first name" from the #tree using the below code and it doesn't ...
  • 11
2 votes
1 answer
325 views

How to return an XLS file to download on form submit

I am trying to create an XLS file and send it to the browser on a form submit but I cannot figure out how to do it. I am using XLS Serialization module but I'm open to all other suggestions. With this ...
  • 7,251
0 votes
2 answers
359 views

How to retrieve #tree values inside a form?

I am new at Drupal 8 and I am creating a module that has a #tree form. I chose #tree form to create multiple forms which has the same textfields and button. I came across one document in drupal about #...
  • 11
1 vote
1 answer
588 views

Render responsive image inside form fields programmatically

I am trying to figure out how to render a responsive image programmatically inside a form field. I am doing maintenance in a code on my current job, and sometimes there are form fields using the '#...
-3 votes
2 answers
39 views

How to fill a form field of type fieldset with values from an array

How can I fill a formfield type fieldset with more than one markup value from an array? foreach ($drugis_postgis_connection->tableNames() as $table) { $form['tables'] = [ '#type' => '...
0 votes
1 answer
101 views

How can I filter the path aliases by node id?

On this page admin/config/search/path, we can only filter the results by URL alias. I want to filter the results by node id as well? How can I do that? It is not possible with form alter. As the ...
  • 2,765
0 votes
1 answer
293 views

Throw error for required field in hook_form_alter?

I have a node type that can be created by authenticated users. In the node create form, I added a terms-of-service checkbox via a form hook like so: function themename_form_alter(&$form, &$...
0 votes
0 answers
38 views

How to create similar multiple forms using a button?

I am using Drupal 9. I am creating a module that has a form with a button that will re-create another form similar to the first one. These is my code. public function buildForm(array $form, ...
  • 11
1 vote
0 answers
106 views

Register form block into modal

I want to display the register form in a modal. For that, I created a custom plugin block which returns the rendering of the form register. /** * {@inheritdoc} */ public function build() { $...
-2 votes
1 answer
510 views

How to open a form in modal from controller while redirect

I can open a form in modal when click on a link (and there are lot of examples in the net as well). What I like to do: When some one go to a url in browser or redirect to a url, it will open a dialog/...
3 votes
2 answers
131 views

AJAX Form using a sub-class of ContentEntityForm fires/invokes/calls the WidgetBase::massageFormValues() twice

I'm using a AJAX form. This form I build from a sub-class of ContentEntityForm. The Entity edited by this form has a field MyField. To bring all the field values in a appropriate structure I'm using ...
-3 votes
1 answer
322 views

Open new tab with external link using an image button

I am making a custom module with a form that has an image button. The image button will open the external link with the value of a textfield inside the form. I have searched any possible solution in ...
  • 11
-2 votes
2 answers
79 views

Can I use two different forms in one module?

I'm new to Drupal 8 and I was wondering if I can use two forms which are inside the Form folder(mymodule/src/Form/). The name of my forms are FormOne.php and FormTwo.php. I can only display and use ...
  • 11
0 votes
1 answer
64 views

Actions form array overriding to set HTML id attribute

I extend the EntityForm class and I try to override a submission button, to set its HTML ID attribute (try #1). Since it doesn't work, I tried to wrap the button in a wrapper <div>. I tried to ...