0

I need to add Placeholder property to Drupal 7 login form from template.php. Not to use a module. and also want to know how to add other attributes like Required etc.

1 Answer 1

4

I think it cannot be done unless using the module. You can try like this

1) Adding the hook_form_FORM_ID_alter in the module to add placeholders, required and some other form attributes.

2) In the template file you can call that form using drupal_get_form('form-id', args1, args2, etc..)

3) Finally, pass the form to the tpl file.

Example:

1) In .module

function yourModule_form_user_login_alter(&$form, $form_state) {
  $form['name']['#attributes'] = array(
    'placeholder' => array (
       t('Username')
     )
  );
  $form['name']['#required'] = TRUE;
  /* Add other attributes like this*/
}

2) In template.php

$variables['login_form'] = drupal_get_form('user_login');

3) In .tpl.php

<?php print render($login_form); ?>

I hope this will work !!

2
  • Thanks for your help it's working in template.php. I will also try them in module and .tpl. Commented Sep 29, 2017 at 7:02
  • This worked in drupal 8 too. Commented Mar 10, 2020 at 16:43

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.