display field group in different tab/URL

by NanaBrownee   Last Updated January 10, 2018 09:07 AM

I have been struggling with this. Views cannot help me (or I didn't figure out how).

I have created a new tab to my profile 2 in my custom module using custom_menu custom_menu_alter custom_menu_local_tasks_alter

The URL is mywebsite.com/profile-main/%user/preferences

Preferences is the new URL linked to the new tab.

My aim is to display a field group with a bunch of preferences info in that new tab/URL Technically under that tab I want the user to log any preferences they have regarding newsletters and how we contact them etc...(GDPR related stuff) I need the data to be attached to the specific user.

I can't seem to figure it out. I am not very confident with hooks and custom code and at this point I am stuck. What did I miss?

Any clue on how I could achieve this?

Tags : fields users hooks


Answers 1


Your assumption is correct I suppose! You might want to remove the said fields from the original profile edit form additionally.

I hope you're familiar with Form API! Take a look at user_profile_form. This function is the main form function for the profile form. This form calls user_account_form function and passes the $form variable by reference, and in user_account_form the profile fields are created and put into the $form variable.

So:

  1. Programmatically create a new form. If I were you, I'd just call user_account_form from here too, but then unset the fields you don't want. The form could also call the default validate and submit handlers then: $form['#validate'][] = 'user_profile_form_validate'; and $form['#submit'][] = 'user_profile_form_submit';

  2. hook_form_alter to alter the original user_profile_form and unset the fields which you are showing in your other form. (Or you can set '#access' => false too)

  3. Show the form at the path you created. This is just a case of returning drupal_get_form as your page callback and your form ID as page arguments. Or you can return drupal_get_form('your_form_id'); from your page callback function. Whatever works for you.

Beebee
Beebee
January 10, 2018 14:49 PM

Related Questions


Inserting in user's field on creating the user?

Updated February 21, 2018 10:07 AM


Drupal 6 login attempts user block

Updated August 20, 2017 22:07 PM

hook_user_insert not working

Updated May 10, 2016 08:03 AM