0

I'm trying to add a new fields with hook_form_alter on user_profile_form. From the core module right now there are:

Password confirm

Pass and re-enter pass

E-mail

This is the part of my code:

/*
 * Implements hook_form_alter().
 */

 function mymodule_form_alter (&$form, &$form_state, $form_id) {

switch ($form_id) {
        case 'user_profile_form':




        $form['account']['telephone'] = array(
        '#type' => 'textfield',
        '#title' => t('New mobile'),
        );

        $form['account']['pass_title'] = array(
        '#type' => 'markup',
        '#markup' => '<div class="profile-pass-title">Password change</div>',
        '#weight' => -6,
        );


        //krumo($form); 
        break;  
        }
}

As you can see I've added the telephone field into the form, but when I add text into that text field it doesn't save. The url is user/%/edit. I'm missing something.

The core fields password and email are properly saving.

The next thing is to do is when I add a new field on user_profile_form to be displayed into user/%.

Previously I've mention admin/config/people/accounts/fields, when I add a field from there let's say "telephone" it's working great. On user/%/edit is appearing as a form field and on user/% the result is rendering well. But my idea is: when I've added this field I want to have a title over the "telephone" field result. The title will be "Changing telephone" and bellow will be displayed the "telephone" field result (the result is what the user filled in the textfield on user/%/edit).

@Alfred Armstrong post helped a lot.

With The Field group module this is possible to achieve. It's amazing that on user_profile_form I can use fieldset and wrap the field into, it displays into user/%/edit and on display field that render the results on user/% I can use different fieldset and with little CSS I can do exactly what I want. Many thanks to Alfred Amstrong for showing me this module and thank you all who helped me to find an aswer for this task. I wish I could add +1 to you all :)

10
  • What do you mean by "doesn't display on the user/%/edit, but only on user/%"? You need to add some hard-coded text? Or you want it to be editable on user's profile page directly? If so, where will it be displayed?
    – Mołot
    Oct 2, 2013 at 9:17
  • I mean if I add a field "Contact info" field in admin/config/people/accounts/fields the user will see that as a field text, but my idea is "Contact info" to be displayed only in user/%. If I use hook_form_alter and type-markup with text it will not be displayed in user/% Oct 2, 2013 at 9:18
  • I still don't get you. Where should it be editable and where should it be rendered?
    – Mołot
    Oct 2, 2013 at 9:20
  • "Custom info" should not be editable and will be displayed only in user/%. The other fields like: name, mobile...should be editable. Oct 2, 2013 at 9:21
  • If you only need to display a static text / HTML piece, you don't need fields at all, you only need to theme profile page.
    – Mołot
    Oct 2, 2013 at 9:25

1 Answer 1

1

Take a look at the Field Group module. That will enable you to group fields under a heading as you require. You can add groups on the form or on the display independently.

After you enable the module the display editor at Administration -> Configuration -> People -> Account Settings -> Manage Display will show a new entry to "Add new group". Add a new group of type fieldset, give it a title as required and set it to be "open" (not collapsible). You can then drag and drop individual fields into the group.

By default the group will display in a box. If you don't want that, the CSS to override it is pretty simple, eg:

body.page-user fieldset {border: none;padding:0;margin:0}
1
  • Yes, this is working great. I'm gonna add more details on the main thread. Thanks very much. Oct 3, 2013 at 8:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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