Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Instead of user_register_form (new user creation) and user_profile_form (existing user edit) can't these be called together?, at the very least with a wrapper?
Although two hook_form_FORM_ID_alter s are needed, I don't quite understand why.
The alternative is hook_form_alter() and check the form_id for both.
Maybe I've missed something, but this seems a tad counter to Drupal's typical approach to altering forms.
For instance, I want to add a custom checkbox to the user form (obviously ignoring the Field UI at the moment):
function mymodule_form_alter($form, &$form_state, $form_id) {
if ($form_id == 'user_register_form' || $form_id == 'user_profile_form') {
$form['like_donuts'] = array(
'#title' => t('Tick the box if you like donuts.'),
'#type' => 'checkbox',
'#default_value' => !empty($form_state['values']['like_donuts']) ? $form_state['values']['like_donuts'] : '',
);
}
}
Comments
Comment #3
dpiThey're two forms, so they have two ID's