Alter the address fields of the address module like adding a placeholder

Created on 30 October 2023, about 1 year ago
Updated 18 January 2024, 11 months ago

Problem/Motivation

I created a question on Drupal stack exchange but I came here to see if you would have an idea. I use the address module with Drupal commerce. I created a form with the form api to update a user's address fields and everything works fine. I created form elements for each address field.

But I saw by looking in the code that we can create an address form directly with an address field type :

$form['user_adress']['address'] = [
      '#type' => 'address',
      '#default_value' => [
        'country_code' => 'FR',
        'family_name' => $user_adress_lastname,
        'given_name' => $user_adress_firstname,
        'address_line1' => $address_line1,
        'address_line2' => $address_line2,
        'postal_code' => $user_adress_postal_code,
        'locality' => $user_adress_locality,
      ],

      '#used_fields' => [
        AddressField::FAMILY_NAME,
        AddressField::GIVEN_NAME,
        AddressField::ADDRESS_LINE1,
        AddressField::ADDRESS_LINE2,
        AddressField::POSTAL_CODE,
        AddressField::LOCALITY,

      ],
      '#available_countries' => ['FR'],
    ];

Here, everything works well, with a single form element, I display all the address fields I need and the information is updated correctly.

On the other hand, I cannot modify the fields to, for example, add a placeholder as indicated in the documentation. I tried with a hook and also with a call back function in my class but it doesn't work. Do you know if this is possible?

FYI, here is the full question: https://drupal.stackexchange.com/questions/317423/alter-the-address-fiel...

Steps to reproduce

$form['user_adress']['address']['widget'][0]['address']['#after_build'][] = '::customer_customize_address';

public function customer_customize_address($element, &$form_state) {

   $element['#attributes']['placeholder'] = 'HelloWorld!;
  }

Or

function mymodule_customer_form_alter(&$form, FormStateInterface $form_state, $form_id) {

    if (($form_id == 'customer_infos')) {
        $form['user_adress']['address']['widget']['#after_build'][] = 'mymodule_customize_address';
    }
    dump($form);
}

function mymodule_customize_address($element, &$form_state) {

    $element['#attributes']['placeholder'] = 'HelloWorld!;
    return $element;
}

I also tried simple things like :

$form['user_adress']['address'] = [
  '#type' => 'address',
  '#default_value' => [
    'country_code' => 'FR',
    'family_name' => $user_adress_lastname,
    'given_name' => $user_adress_firstname,
    'address_line1' => $address_line1,
    'address_line2' => $address_line2,
    'postal_code' => $user_adress_postal_code,
    'locality' => $user_adress_locality,
  ],
  '#placeholders' => [
    'address_line1' => 'Adresse',
    'locality' => 'Ville',
  ],
  // ...
];
💬 Support request
Status

Fixed

Version

1.0

Component

Code

Created by

🇫🇷France sébastien-fr Nantes - France

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @sébastien-fr
  • 🇫🇷France sébastien-fr Nantes - France
  • Assigned to sickness29
  • Issue was unassigned.
  • Status changed to Needs review about 1 year ago
  • Open in Jenkins → Open on Drupal.org →
    Core: 9.5.x + Environment: PHP 7.3 & MySQL 5.7
    last update about 1 year ago
    33 pass
  • 🇺🇦Ukraine sickness29

    Added placeholders as configuration to address default widget and to elements that used in address form.
    Used #empty_option property to display placeholders in country and administrative area select elements.
    Also added FunctionalJavascript test to make sure configured placeholders for the widget are shown to user.

  • 🇺🇦Ukraine sickness29

    Let me know if this at least can go into 2.0.x, so I can create patch for 2 version

  • Status changed to Fixed 12 months ago
  • 🇷🇸Serbia bojanz

    The documentation page is still valid, #after_build still works.

    I've also provided the same example (which I just retested) in this comment: https://www.drupal.org/project/address/issues/3167404#comment-15375862 💬 There is not possible to set a description for form elements of address (fields of address) Fixed

    And ultimately the reason why I said no to #field_descriptions is also the reason why I'm saying no to #placeholders. We do not need a custom mechanism on the form element level.

    On the other hand, I do agree that it is very tempting to allow setting field descriptions and placeholders via the UI, as core allows for individual fields. However, the interest for that seems very low, so I would like to get more buy-in from the community before we start implementing and supporting either or both.

    I am going to tentatively close this, in order to give sickness29 issue credit. I will also ping @dww to get his thoughts on the matter. Then, if and once enough interest materializes, we can restart this effort.

  • 🇺🇸United States dww

    +1 to continuing to document #after_build as the solution to these sorts of issues. It’s confusing for newer uses of address and developers, everyone expects form_alter to work. But adding a bunch of additional code to maintain seems worse.

    I’m not the best person to assess, since I’m a co-maintainer, but if there are any places where more / better docs would help, please open issues about that. Very happy to make it more obvious how to solve these things.

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024