0

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?

I am using Drupal 7. What I want is to move fields (in a field group) from the main profile 2 registration page form and display them in the new tab accessible when logged in to profile 2 account, instead of having everything in the same form. But I am happy to have to create a different form if it would work better. This is what's returned by custom menu:

$items['profile-main/%user/preferences'] = array(
  'title' => t(' Preferences'), 
  'page callback' => 'custom_user_preferences', 
  'page arguments' => array(1), // Uid 
  'access callback' => TRUE, 
  'access arguments' => array('access candidate_dashboard'), 
  'type' => MENU_LOCAL_TASK, 
);

This is the array for custom_user_preferences:

'custom_user_preferences' => array(
  'file' => 'includes/user_preferences.inc', 
  'template' => 'templates/user_preferences', 
  'variables' => array(
    'profile' => NULL, 
    'approved' => NULL, 
    'cust_type' => NULL,
  ) 
), 

edit: Am I right to assume that I have to

  1. programmatically create a form
  2. which upon submission saves the data in relevant profile2 fields
  3. display the form in the new path I created

edit 11/jan

In the end I am creating a form as the fields I need to add are not in the profile 2 set up to start with (the profile 2 registration form has more than 180 fields in it). I've been receiving error:

*Warning: Illegal string offset '#tree' in form_builder() (line 1891 of /**/**/**/site/includes/form.inc).
Warning: Illegal string offset '#tree' in form_builder() (line 1911 of /**/**/**/site/includes/form.inc).
Warning: Illegal string offset '#parents' in form_builder() (line 1911 of /**/**/**/site/includes/form.inc).
Notice: Array to string conversion in form_builder() (line 1911 of /**/**/**/site/includes/form.inc).
Warning: Illegal string offset '#array_parents' in form_builder() (line 1916 of /**/**/**/site/includes/form.inc).
Notice: Array to string conversion in form_builder() (line 1916 of /**/**/**/site/includes/form.inc).
Warning: Illegal string offset '#weight' in form_builder() (line 1920 of /**/**/**/site/includes/form.inc).
Warning: Illegal string offset '#processed' in form_builder() (line 1802 of /**/**/**/site/includes/form.inc).
Error: Unsupported operand types in form_builder() (line 1812 of /**/**/**/site/includes/form.inc).*

I am not sure if the problem is my form or the callback or something else...

function site_menu() {
    $items = array();
    $items['profile-main/%user/preferences'] = array(
        'title' => t(' Preferences'),
        'page callback' => drupal_get_form,
        'page arguments' => array('site_upload_form', 1), // Uid
        'access callback' => TRUE,
        'access arguments' => array('access content'),
        'type' => MENU_LOCAL_TASK,
    );
}

function site_upload_form($form, &$form_state){
    $form['email']= array(
        '#type'=>'textfield',
        '#title'=>t('Confirm email address'),
        '#maxlength'=> 255,
        '#required'=> TRUE,
    );
    $form['frequencyemail']= array(
        '#type'=>'select',
        '#title'=> t('How often do you want to hear from us?'),
        '#options'=> array(
            0 =>'Regularly',
            1 => 'Quarterly',
            2 => 'During the Summit',
            3 => 'Never',
            ),
        '#required'=> TRUE,
    );

    $form['submit']= array(
        '#type'=>'submit',
         '#value'=> t('Submit'),
     );
return $form;
}

Not sure what I am looking for

5
  • Hi! Are you on Drupal 7 or 8? Can you also clarify what you want to achieve? Sounds like you want some sort of a configuration form at /profile-main/%user/preferences, but not quite sure. In your custom_menu what are you returning for that path?
    – Beebee
    Jan 10, 2018 at 9:48
  • Drupal 7 What I want is to move fields (in a field group) from the main profile 2 registration page form and display them in the new tab accessible when logged in to profile 2 account, instead of having everything in the same form. Does that make sense? But I am happy to have to create a different form if it would work better. This is what's returned by custom menu: See below Jan 10, 2018 at 10:16
  • $items['profile-main/%user/preferences'] = array( 'title' => t(' Preferences'), 'page callback' => 'custom_user_preferences', 'page arguments' => array(1), // Uid 'access callback' => TRUE, 'access arguments' => array('access candidate_dashboard'), 'type' => MENU_LOCAL_TASK, ); Jan 10, 2018 at 10:17
  • this is the array for custom_user_preferences: 'custom_user_preferences' => array( 'file' => 'includes/user_preferences.inc', 'template' => 'templates/user_preferences', 'variables' => array( 'profile' => NULL, 'approved' => NULL, 'cust_type' => NULL,) ), Jan 10, 2018 at 10:26
  • Please edit the original question to add details as comments can get purged and readability isn't great ;)
    – Beebee
    Jan 10, 2018 at 14:39

1 Answer 1

0

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.

3
  • I have used form API a bit. For some reason I missed user_profile_form. I will look into that. Thank you for the hints. Jan 10, 2018 at 14:57
  • Hmm I suspect the code for my form is problematic... Because the tab displays and when I click on it it directs me to the right path - just nothing is showing there. I suspect my form is wrong. (I updated the initial question with the error message) Jan 12, 2018 at 11:27
  • @NanaBrownee are you on PHP7? IIRC Drupal 7 doesn't fully support PHP7. Try with PHP5. Also don't forget to clear your cache. (Also just an observation but you're not taking the $uid variable in your form function)
    – Beebee
    Jan 15, 2018 at 14:49

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.