There is not possible to set a description for form elements of address (fields of address)

Created on 26 August 2020, over 4 years ago
Updated 10 January 2024, 12 months ago

Problem

In administrative pages or programmatically there is not possible to set a description for form elements of address (e.g. for Address line 1).
The function addressElements of class Address creates a form element this way:

$element[$property] = [
    '#type' => 'textfield',
    '#title' => $labels[$field],
    '#default_value' => isset($value[$property]) ? $value[$property] : '',
    '#required' => in_array($field, $required_fields),
    '#size' => isset($size_attributes[$field]) ? $size_attributes[$field] : 60,
    '#attributes' => [
      'class' => [$class],
      'autocomplete' => FieldHelper::getAutocompleteAttribute($field),
    ],
  ];

We can see there is no the key '#description' at all.

Proposed resolution

We be able to set a desctiption programmatically if to this code we add line:
'#description' => isset($element['#field_descriptions'][$field]) ? $element['#field_descriptions'][$field] : '',

Now, we can use a hook_form_alter to set description this way:
$form['shipping_information']...['widget'][0]['address']['#field_descriptions']['addressLine1'] = 'Our description';

πŸ’¬ Support request
Status

Fixed

Version

2.0

Component

Code

Created by

πŸ‡·πŸ‡ΊRussia ru.bsv Omsk

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 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 test and improved previous patch to have default value in element info and example.

  • πŸ‡ΊπŸ‡¦Ukraine sickness29

    Hi @bojanz
    Can you please review this? Perhaps we need to update this for 2.0.x?

    Also it would really help if you would make me a project maintainer so we can move fixes/tests into 2.0.x faster, please let me know

  • πŸ‡·πŸ‡ΈSerbia bojanz

    All feature requests are 2.0.x-dev only at this point, we will only commit essential bug fixes to 8.x-1.x.

    @sickness29
    I am happy to review any and every patch that you provide, but I would like to remain the sole maintainer (along dww) for now. This module has a big install base (100k) and a very wide set of use cases (Commerce, non-Commerce), as well as complex interactions with the underlying library. This makes it very easy to introduce regressions for a large number of sites, so I would rather ensure that each change gets my approval. Since we have less than two pages of issues, this feels doable. With important modules slow development is a feature, not a bug.

  • Status changed to Fixed 12 months ago
  • πŸ‡·πŸ‡ΈSerbia bojanz

    I've given this issue some thought, and decided not to proceed with it.

    The reason why is because we are introducing an Address-specific mechanism to replace a more generic Form API mechanism, namely using #after_build to modify subelements:

    public function buildForm(array $form, FormStateInterface $form_state) {
        $form['address'] = [
          '#type' => 'address',
          '#default_value' => [
            'country_code' => 'US',
            'administrative_area' => 'CA',
            'locality' => 'Mountain View',
            'postal_code' => '94043',
            'address_line1' => '1098 Alta Ave',
            'organization' => 'Google Inc.',
            'given_name' => 'John',
            'family_name' => 'Smith',
          ],
          '#after_build' => [[get_class($this), 'modifyDescriptions']],
        ];
        $form['submit'] = [
          '#type' => 'submit',
          '#value' => $this->t('Submit'),
        ];
    
        return $form;
      }
    
      /**
       * #after_build callback: Modify the address descriptions.
       */
      public static function modifyDescriptions(array $element, FormStateInterface $form_state) {
        $element['address_line1']['#description'] = 'This is the first address line';
        $element['address_line2']['#description'] = 'This is the second address line';
    
        return $element;
      }
    

    This doesn't feel too verbose, especially taking into account the fact that this is not a majority use case (as evidenced by the few subscribers on this issue). It also allows developers to modify all other properties, such as the #title, #placeholder, #maxlength, #size, etc. I am worried that if we make an exception for field_descriptions, we'll just end up having the same conversation for titles and other settings.

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

Production build 0.71.5 2024