commerce_order_address_field_after_build() to set 'Company' label prevents (documented) ways to alter this

Created on 19 March 2024, about 1 year ago

Describe your bug or feature request.

Rename the label of AddressField::ORGANIZATION from "Company" to "Organization" Fixed happened upstream. To preserve BC for existing sites, 📌 Start using Address 2.x Needs review wanted to force the label to be 'Company', which was implemented like this:

function commerce_order_field_widget_single_element_form_alter(...) {
  ...
  if ($field_name === 'address' && $entity_type === 'profile') {
    $element['#after_build'][] = 'commerce_order_address_field_after_build';
  }
}

However, that breaks anyone using the documented process for altering this label:

function mymodule_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if (($form_id == 'profile_customer_edit_form') || ($form_id == 'profile_customer_add_form')) {
    $form['address']['widget'][0]['address']['#after_build'][] = 'mymodule_customize_address';
  }
}

function mymodule_customize_address($element, $form_state) {
  $element['organization']['#title'] = t('Organization');
  $element['organization']['#size'] = 30;
  $element['organization']['#required'] = TRUE;
  return $element;
}

That's no longer accurate, since those aren't the form IDs anymore. But even if it was, it's not sufficient. We have to unset or make sure we run after commerce_order_address_field_after_build(), which is now being set up in $form['address']['widget'][0]/code>, not <code>$form['address']['widget'][0]['address']. If we don't, all of our alterations are overwritten.

Also, I'm starting a new commerce site from scratch, and no one wants this label 'Company', yet it's being forced this way "for BC".

  1. Can we make this forced behavior more flexible? Is it worth a (hidden?) setting that defaults to off but there's an update hook for existing sites or something?
  2. Should we document that you are now encouraged to #after_build not just ['widget'][0]['address'] but also ['widget'][0] if you want to change this?
  3. Or?

Thanks!
-Derek

📌 Task
Status

Active

Version

2.0

Component

Developer experience

Created by

🇺🇸United States dww

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

Comments & Activities

  • Issue created by @dww
  • 🇺🇸United States dww
  • 🇮🇱Israel jsacksick

    Also, I'm starting a new commerce site from scratch, and no one wants this label 'Company', yet it's being forced this way "for BC".

    That may not be true for everyone as I haven't heard anyone else complaining about this... We could have handled this using a settings flag Or maybe using state?
    Perhaps a state flag set from commerce_order_update_8217() but I guess it is too late now... Don't really know what else we could rely on to figure out whether the override is wanted...

  • Status changed to Closed: won't fix about 1 month ago
  • 🇮🇱Israel jsacksick

    Considering the little interest with this, setting this as "won't fix". You can implement a hook_module_implements_alter() to unset commerce_order_field_widget_single_element_form_alter().

Production build 0.71.5 2024