I have to replace the label "Company" with "Building". Using 8.x-1.0-rc3, I was able to do this via hook_form_alter:
function mymodule_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if (in_array($form_id, array('node_location_edit_form', 'node_location_form'))) {
$form['field_address']['widget'][0]['organization']['#title'] = t('Building');
}
}
However, this no longer works in rc4 (and when I look at the form array, I can see that $form['field_address']['widget'][0]['organization'] no longer exists). I've also tried overriding the string in settings.php:
$settings['locale_custom_strings_en'][''] = array(
'Company' => 'Building',
);
I'm able to override other strings this way (so I'm confident that it's working in general), but it doesn't work for address fields.
What's the best way to change the label of an address field? Thanks in advance for any advice.
Comments
Comment #2
bojanz commentedYou'd add a #process callback to the address form element, then alter it from there.
That said, it feels like you're abusing the Organization field for something it was not intended for. I'd rather use the address_line2 for that.
Comment #3
noah commentedThanks @bojanz -- I'm not sure if it's a bug or if I was doing something wrong, but adding anything to #process as described above was causing the entire address widget to show up empty (even if I just returned the element unchanged). This put me on the right track, though, and I was able to make it work using #after_build. In case anyone comes across this looking to achieve something similar, here's what seems to have worked:
I agree with you that we're stretching the semantics a bit re: "Company" vs. "Building", but it sort of makes sense in this instance because here "Building" is analogous to "Organization" (also, we're already using address_line2).
Thanks again!
Comment #4
bojanz commentedYou probably didn't have "return $element;" at the end of you process callback. Forgot to mention that. Anyway, after_build is just as fine.
Comment #6
finneIf you only need to change the labels, you can also use a translation:
- enable the interface translation module
- for the English (built-in) language, enable Interface translation to English: /admin/config/regional/language/edit/en
- visit a form or display with an address field to generate the translation source strings
- translate the labels: /admin/config/regional/translate
The advantage is that the labels are changed everywhere using 1 setting.
Comment #7
budalokko commentedAt least for current versions (Drupal 8.5.3, Address 1.3), when adding following code to the form_alter the original field element's process callback (Drupal\address\Element\Address::processAddress) is not called at all:
$form['field_address']['widget'][0]['address']['#process'][] = 'mymodule_change_labels';That's why even if this function returns $element the wiget is empty.
For it to work its necessary calling the original field element's process callback explicitely for example like this:
Comment #8
FirstSanny commentedHello Guys,
I'm currently working with the versions Drupal Core 8.5.5 and Address 1.4. But the following code isn't changing anything. I do know it is called because i get the drupal message, but no change is made. Am I doing something wrong here?
@budalokko Maybe you should update to Address version 1.4? I don't need the original process call.
Comment #9
johnpitcairn commentedThe example code above does not work for me, nor does the documentation example at drupalcommerce.org. These appear to assume the addressfield is a standard field in the form.
In my case the addressfield is a field on a profile checkout pane, added via Commerce Profile Panes, using inline entity form. It's necessary to add the after_build callback via
hook_inline_entity_form_entity_form_alter(), and I must add it to the widget base element, not the widget address element. The code below successfully alters theaddress_line1field label:Hope this helps. I do wonder when the address_line1 label default got changed from simply "Address" to "Street address"? That's a big assumption to make.
Comment #10
bojanz commentedJust a note to say that there are docs now: https://docs.drupalcommerce.org/commerce2/developer-guide/customers/addr...
Of course, checkout will be a different story, especially if using a contrib module that does its own embedding.
Comment #11
djg_tram commentedBojan, the form id is something like
profile_customer_address-book-add_form, note the dashes instead of underlines. Is this a bug that can't be fixed any more because people rely on it, or is it on purpose?Comment #12
sagesolutions commentedAlso, for editing an existing address, the form id is
profile_customer_address-book-edit_form.Has anyone figured out how to override the address line during the checkout process? I tried #9, but my
MYMODULE_inline_entity_form_entity_form_alterfunction is never called.Comment #13
luksak@sagesolutions no, I am trying the same right now. Did you figure this out?
Comment #14
luksakI figured it out like this:
Comment #15
luksak@bojanz Should we update the docs at https://docs.drupalcommerce.org/commerce2/developer-guide/customers/addr... ? For me those instructions didn't work.