1

I have assigned class names to both user name & password text fields in user login block separately like this and it's working.

function mymodule_form_alter(&$form, &$form_state, $form_id, $element) {
   if ( TRUE === in_array( $form_id, array( 'user_login', 'user_login_block') ) ) {
       $form['name']['#attributes']['class'] = array('mgnB10', 'fntXL', 'drkFg1');
       $form['pass']['#attributes']['class'] = array('fntXL', 'drkFg1');
   }
}

I want to include these fields inside a single '' and assign class name for design purpose, like this.

<div class="class-name">
  username field
  password field
</div>

I am using the following code but I didn't get the expected output.

function mymodule_form_alter(&$form, &$form_state, $form_id, $element) {
   if ( TRUE === in_array( $form_id, array( 'user_login', 'user_login_block') ) ) {
       $form['user_login']['#attributes']['class'] = array('#prefix' => 'myclass');
       $form['name']['#attributes']['class'] = array('mgnB10', 'fntXL', 'drkFg1');
       $form['pass']['#attributes']['class'] = array('fntXL', 'drkFg1');
       $form['user_login'] = array('#suffix' => '</div>');
   }
}
2

Hope this will help you,

In Drupal 7 you can achieve it by using the #prefix and #suffix properties in hook_form_alter().

   /**
    * Implements hook_form_alter().
    */
    function hook_form_alter(&$form, &$form_state, $form_id) {
      switch ($form_id) {
        case 'user_login':
        case 'user_login_block':
          $form['name']['#prefix'] = '&#60;div class="username-password-wrapper"&#62;';
          $form['pass']['#suffix'] = '&#60;/div&#62;';
          break;
      }
    }
  • Welcome enjoy :) – inizio Nov 11 '13 at 6:04
0

Instead of doing this programmatically, you can use the Field Group module, which allows you to group fields using the Manage Fields tab in the UI.

<div> can be selected as the group type.

  • I installed Field Group module. In manage field page of Article I added group and two fields inside it. In Widget column I saw <pre>div open required_fields yes classes group-test-group field-group-div show_label Yes label_element Header 3 effect None speed Fast</pre> Now it doesn't work and I how can I add <pre> div close</pre>. How do I add close div? – dhinesh Nov 6 '13 at 10:10

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.