Allow disabling of individual fields within the field override UI

Created on 7 December 2017, over 6 years ago
Updated 27 December 2023, 6 months ago

I have a use case where I want to disable certain fields on the address form: people are connected to schools, and are only allowed to request an item sent to them go to their school. I want to pull in their school address as the default for the order, but some addresses are incomplete, so I want any non-populated address fields to be editable, but not let people change any other fields.

I tried an #after_build function, but the changes were ignored (too late in the process I guess). Anytime else I could get into the form, the elements were not present to customize.

Seems like this is another property like "in use" or "default value" that could be easy to alter in hook_form_alter with just a little bit of code.

✨ Feature request
Status

Active

Version

2.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States gcb

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.

  • πŸ‡·πŸ‡ΈSerbia bojanz

    Retitling for clarity. And changing version because new features only go into 2.0.x.

    We are talking about the field override UI here. Disabling fields via form_alter already works via #after_build:

      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), 'disableFields']],
        ];
        $form['submit'] = [
          '#type' => 'submit',
          '#value' => $this->t('Submit'),
        ];
    
        return $form;
      }
    
      /**
       * #after_build callback: Disables certain fields.
       */
      public static function disableFields(array $element, FormStateInterface $form_state) {
        $element['address_line2']['#attributes']['disabled'] = 'disabled';
    
        return $element;
      }
    

    We should add this example to the documentation.

Production build 0.69.0 2024