6

Is it possible to add a title attribute through a form_alter to element #type 'password_confirm'?

For example, I have a form with the following fields:

  • email
  • password
  • password confirm

To apply a title attribute to the email element is fine...

$form['account']['mail']['#attributes']['title'] = 'enter your email address';

When I dump the contents of $form in search for the input elements for 'password_confirm', I only get the following:

[pass] => Array
                (
                    [#type] => password_confirm
                    [#size] => 25
                    [#description] => We will not spam you or give your email address to third parties.
                    [#required] => 1
                )

I'm not seeing 'pass1' and 'pass2' in the array.

Please could someone point me in the right direction?

4

The password confirm element is converted to two fields in a process function (form_process_password_confirm()), so adding your own process function to the password element would do the trick.

Per this issue it seems you actually need to overwrite the existing #process array instead of just appending to it, e.g for the user register form password element:

function MYMODULE_form_user_register_form_alter(&$form, &$form_state, $form_id) {
  $form['account']['pass']['#process'] = array(
    'form_process_password_confirm',
    'MYMODULE_password_confirm_process',
    'user_form_process_password_confirm'
  );
}

function MYMODULE_password_confirm_process($element) {
  $element['pass1']['#attributes']['title'] = 'Title';
  $element['pass2']['#attributes']['title'] = 'Title2';
  return $element;
}
  • "adding your own process function to the password element would do the trick", this I didnt know possible, incredible! Many many thanks Clive. – Alex Gill Jun 28 '13 at 16:28
1

In my case with drupal 7, I corrected with:

function MYMODULE_password_confirm_process($element) {
  $element['pass1']['#title'] = 'Title';
  $element['pass2']['#title'] = 'Title2';
  return $element;
}
  • This changes the label next to the input, while the OP wanted to change the "title" HTML attribute on the input itself. – user2534880 Oct 7 '16 at 10:41
0

Slightly improved version of Clive's answer (based on a comment from the issue he linked in his answer):

function MYMODULE_form_alter(&$form, &$form_state, $form_id)
{
    switch($form_id)
    {
        case 'my_form':
            // Get default process function array:
            $element_info = element_info('password_confirm');
            $process = $element_info['#process'];
            // Add our process function to the array:
            $process[] = 'MYMODULE_process_password_confirm';
            $form['account']['pass']['#process'] = $process;
            break;
    }
}

function MYMODULE_process_password_confirm($element)
{
    $element['pass1']['#attributes']['title'] = 'Title';
    $element['pass2']['#attributes']['title'] = 'Title2';
    return $element;
}

The call to the element_info function is necessary because the '#process' array can be affected by other modules.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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