1

I wanted to know if it's possible to change the input labels on the address field module 'Address 1' and 'Address 2' without using the string overrides module?

I want to be able to change them to say 'Address line 1' and 'Address line 2'.

Can this be done in a template preprocess function?

1 Answer 1

2

This can be done with hook_form_alter:

function MY_MODULE_form_alter(&$form, &$form_state, $form_id) {

    //drupal_set_message("Form ID is : " . $form_id);

    switch($form_id) {

        case 'MY_FORM_ID':

            // dpm($form);

            // $form['NAME_OF_YOUR_FIELD'] - this is the field you want to edit

            // check that the field is set before looping over values
            if(isset($form['NAME_OF_YOUR_FIELD'][LANGUAGE_NONE])) {
                // loop over each values passing $item by reference
                foreach($form['NAME_OF_YOUR_FIELD'][LANGUAGE_NONE] as &$item) {
                    if(is_array($item) && isset($item['street_block'])) {
                        $item['street_block']['thoroughfare']['#title'] = 'Address Line 1';
                        $item['street_block']['premise']['#title'] = 'Address Line 2';
                    }
                }
            }
            break;
    }

}

I found the path to the variable I wanted to change by outputting the $form array to the message area using devel's dpm function, and then searching though the array for the value "Address 1" using the search krumo module.

13
  • Hi Felix, thank you for your help. I have added this to my template file, changed MY_MODULE to my theme name and changed 'MY_FORM_ID' to 'order-form-entityform-edit-form' but the changes are not happening. I have flushed caches. What am I doing wrong?
    – T Mardi
    Commented Mar 26, 2014 at 16:12
  • Arr - that is code for a custom module, not a template file. See Creating Drupal 7.x modules if you need any help with that.
    – Felix Eve
    Commented Mar 26, 2014 at 16:18
  • 1
    Form alters can be done in themes (I think any alter can). You just have to make sure the theme is in use on the form you are altering, especially on admin forms.
    – mpdonadio
    Commented Mar 26, 2014 at 16:22
  • I did not know that - cheers MPD.
    – Felix Eve
    Commented Mar 26, 2014 at 16:25
  • Ok, thanks guys. I've looked into hook form alter and have come up with this code but it's not working. Can you help? /** * Implements hook_form_FORM_ID_alter(). */ function my_theme_form_order_form_entityform_edit_form_alter(&$form, &$form_state) { // Change labels for the 'Address 1' and 'Address 2' elements. $form['street_block']['thoroughfare']['#title'] = t('Address line 1'); $form['street_block']['premise']['#title'] = t('Address line 2'); }
    – T Mardi
    Commented Mar 26, 2014 at 16:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.