forms – Hide field label with hook_form_alter()

In my case I was trying to hide the label on quantity field provided by Drupal Commerce cart module and the following wasn’t working:

$form['my_field']['#title_display'] = 'invisible';

I had to do this inside my hook_form_alter

$form['quantity']['widget'][0]['value']['#title_display'] = 'invisible';

If somebody have an explanation on why the first solution was not working, I’d be glad to learn the difference.

hook_form_alter for a Webform – Drupal Answers

I have another post here:

Retrieve Webform submission data from database

I got some guidance but some of it wasn’t feasible for our application and the edited assistance was a bit general. Based on the person’s suggestions I am trying to use a hook_form_alter on a Webform module webform. Here’s the files and code I have.

name: 'CC Processing Get SubmissionData'
description: 'Provides a custom webform handler for retrieving the previous forms submission data'
core_version_requirement: ^8.8
Core: 8.x
package: Custom
type: module



 * Implementation of hook_form_alter
 use DrupalCoreFormFormStateInterface;
 function CCProcessingGetSubmissionData_webform_submission_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
    echo "form alter hook triggered";
    if($form('#webform_id') == 'credit_card_billing_information') {
        echo "form alter form id found";
        $myValue = 'Jared';
        $form('elements')('credit_card_name')('#default_value') = $myValue;

I am not new to programming but am new to Drupal programming. I have some other code in the module that’s not in the answer suggestion on my previous post like the echos I found on other posts in an attempt to try to get something to happen that lets me know the the hook is being called. Both files are in modules/custom/cc_processing_getsubmissiondata. The folder structure I got off of a YouTube video on building hooks but it was just a short 8 minute basic hook video. From all the posts I’ve seen out there, everything I have looks correct but nothing happens. Any assistance in helping me getting this hook to work would be awesome. 🙂

9 – Conditional form states in hook_form_alter()

I have a node with a field, whose widget uses select boxes and multiple choices are possible. The values are coming from a taxonomy entity reference field. There is another field, a text field, which needs to be hidden.

 * Implements hook_form_alter().
function mijn_condities_form_alter(&$form, DrupalCoreFormFormStateInterface $form_state, $form_id) {
  if($form_id == 'node_bedrijf_form' || $form_id == 'node_bedrijf_edit_form'){
    $form('field_afstand')('#states') = array(
      'visible' => array(
        ':input(name="field_openbaar_vervoer")' => array('value' => '236'),

A select box should be added according their choice. For example, when they choose bus or tram, there should be a field to enter the distance in meters. When they choose airport, the range must be in kilometers.

The field isn’t hiding.
I already tried to make the field invisible, but this doesn’t work.

Does anyone know what is wrong?

D8: Model confirmation type and hook_form_alter in WebForm module

I have a *_hook_form_alter for a form being submitted with the Webform module. The $form_id triggers one of a couple *_form_emails_form_submit functions

Using the inline confirmation message type (Webforms >> Setting >> Confirmation), I have no issues triggering the *_form_emails_form_submit function that handles it.

When I switched to use the modal confirmation I started getting errors that my $form_state->getValue(’email_address’) (One of my fields in the webform) was null and broke the functionality of the *_form_emails_form_submit function

For example:

    function my_module_form_emails_form_alter(&$form, FormStateInterface $form_state, $form_id) {
//Switch statement that looks for form_id and fires off below
     foreach (array_keys($form('actions')) as $action) {
            if ($action != 'preview' && isset($form('actions')($action)('#type')) && $form('actions')($action)('#type') === 'submit') {
              $form('actions')($action)('#submit')() = 'product_learn_more_form_emails_form_submit';
    function product_learn_more_form_emails_form_submit(&$form, FormStateInterface $form_state) {
      //When confirmation is inline
    $email_address = $form_state->getValue('email_address'); //
    //When conformation type is modal
    $email_address = $form_state->getValue('email_address'); //null

I am not sure what I am missing or why switching to modal changes thing but any hints you can give would be greatly appreciated.

Drupal Commerce select order-type and store with hook_form_alter

In drupal commerce, I have 3 stores and 3 orders type
When placing a manual order, i am first asked to choose the store and the order type ( which are entities), with radio buttons.
With a select list, I know how to programaticaly select one option, with something like :

function reservation_form_commerce_order_add_form_alter(&$form, DrupalCoreFormFormStateInterface $form_state, $form_id) {

    $form('type')('widget')('#default_value')(0) = 'type_order_1';
    $form('store_id')('widget')('#default_value')(0)('#value') = 1;


How to do the same with button ?
Thanks in advance

Forms – help with hook_form_alter

So I have a custom module that creates a page (/ simple-ajax-form) and contains a form with the ID "payment_form" and other fields with IDs like "number_1". My module name is "Drupalup Simple Custom Form" for reference.

The problem I can't figure out (since I'm new to hook_form_alter) is that on the form ID instead of "payment form" the ID is rendered as "payment form" and then in the input fields instead of as "payment form" it is rendered "number_1 "The ID is rendered as" edit-number-1 ".

What code do I need to render the form as a "payment form" and input without "edit" and also have underscores? And where does this code go – in my custom module?

8 – How can I access and change the display mode of a form in hook_form_alter () or similar?

Is it possible to access and change the display mode of a form in hook_form_alter ()? I want to use context to say, "Use this display mode, not the other."

My special use case is to select a form display mode for a paragraph type that is nested (in multiple levels) in one of several node forms. Based on the details of the higher levels, I want to control which form display mode is used.

I looked at hook_field_widget_entity_reference_paragraphs_form_alter() and hook_entity_form_display_alter() also. Although in principle I could change the display in the last one, there is no information that I could use to make the decision. The widget function doesn't seem to offer anything that may seem useful.

I would expect some kind of connection between the two $form or $form_state in the hook_form_alter() on the display used, but cannot find one. Either I haven't dug the right rabbit hole or there is no connection there and I have to look at another processing step, but I don't know which one.

8 – Why does the hook_form_alter function not work with admin forms?

In my custom module .module I implemented hook_form_alter as follows:

function my_module_form_alter(&$form, DrupalCoreFormFormStateInterface $form_state, $form_id) {
        Drupal::messenger()->addMessage('hook_form_alter found a form. It has the following name: ');

When I go to the home page of my Drupal installation, the search block form is found correctly, but when I go to mysite / admin / config / system / site-information, for example, the function does not find any forms. Strangely, I know that there is a form on this page called "system_site_information_settings". Other admin forms are also not discovered. Why is it? How can I find the name of the site information form other than by browsing the Internet?

8 – How do I know if a form has been loaded or sent in hook_form_alter?

I'm trying to insert a simple message at the top of a node editing form when a user creates a new node.
I added a Drupal::messenger()->addMessage() in one of my form_alter But this prints when opening the edit form (good!) But also in the view of the node after the node has been saved (bad!).

When I look in the debugger I can see that the hook is executed twice (loading and sending). However, I can not determine which version is running.

The only thing that strikes me is that $form_state->requestMethod is different in both cases; However, this is a protected property. So no access to it.

Code should not be required for this question, but:

function mymodule_form_alter(&$form, $form_state, $form_id) {
  Drupal::messenger()->addMessage(t('some msg'), 'warning');