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?
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
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';
$form['#submit'] = 'user_profile_form_submit';
hook_form_alter to alter the original
unset the fields which you are showing in your other form. (Or you can set
'#access' => false too)
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.