Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
By TapocoL on
What I am trying to do is just change the user-login-form, to have Request a new password to not show up. I know I can do a display: none in CSS and I can just modify the core code. But, I would just rather not have it in the view source of the browser, and changing the core causes too many problems upgrading. And most of all, I am trying to learn all the hooks. So, why will the following code not change the $form['links'] inside of the user_login block. The name of the module is mod_theme.
function mod_theme_alter_form(&$form, $form_state, $form_id) {
if ($form_id == 'user-login-form') {
unset($form['links']);
if (variable_get('user_register', 1)) {
$form['links'] = array(
'#value' => l(t('Create new account'), 'user/register', array('title' => t('Create a new user account.'))),
);
}
}
}
The following code is excerpted from user.module, and shows what I am trying to alter.
function user_login_block() {
$form = array(
'#action' => url($_GET['q'], array('query' => drupal_get_destination())),
'#id' => 'user-login-form',
'#validate' => user_login_default_validators(),
'#submit' => array('user_login_submit'),
);
$form['name'] = array('#type' => 'textfield',
'#title' => t('Username'),
'#maxlength' => USERNAME_MAX_LENGTH,
'#size' => 15,
'#required' => TRUE,
);
$form['pass'] = array('#type' => 'password',
'#title' => t('Password'),
'#maxlength' => 60,
'#size' => 15,
'#required' => TRUE,
);
$form['submit'] = array('#type' => 'submit',
'#value' => t('Log in'),
);
$items = array();
if (variable_get('user_register', 1)) {
$items[] = l(t('Create new account'), 'user/register', array('title' => t('Create a new user account.')));
}
$items[] = l(t('Request new password'), 'user/password', array('title' => t('Request new password via e-mail.')));
$form['links'] = array('#value' => theme('item_list', $items));
return $form;
}
Comments
Hook name is wrong.
your function needs to be named modulename_form_alter() - yours is _alter_form()
Thanks
That was one of the fixes. For future reference, $form_id == 'user_login_block', and not 'user-login-form', even though the form set the id equal to 'user-login-form'. I guess there is another item that actually sets the $form_id variable.
-Craig Jackson
-Web Developer
In most cases (though this
In most cases (though this is Drupal 5 experience talking), the $form_id comes from the name of the function passed to drupal_get_form(). The #id 'user-login-form' is the HTML id attribute of the <form> element. There's no technical reason for them to be the same or different.
-----
Übercart -- One cart to rule them all.