I am using the Profile2 module.

I would like to alter one of the Profile2 forms, however these forms are apparently built dynamically, therefore I don't think i can use hook_form_alter() or drupal_get_form(). Is this correct?

How can I alter and then display a Profile2 form? anyone knows?

Comments

Niyas’s picture

Hi,

You can do it using form_alter function from your template.php or using your custom module.

Configure the $form array as you wish!!

Try it.

Thanks,
Niyas

mortona2k’s picture

Have you worked it out yet? Check out profile2.api.php at the very bottom:

* Modules may alter the profile2 entity form regardless to which form it is
* attached by making use of this hook or the profile type specifiy
* hook_form_profile2_edit_PROFILE_TYPE_form_alter().

The other Andrew Morton

ehowland’s picture

I am trying to make a profile2 field required for some users. I have created a profile2 profile with the machine name of more_info. Initially I added two fields firstname and lastname and made the firstname field required.

I initially tried to make this alteration with hook_form_alter. I saw a reference to problems when profile2 loads after my module but altering the name of my module did not change this behavior.

I then saw a reference to hook_form_profile2_edit_PROFILE_TYPE_form_alter at https://drupal.org/node/1198522 and later at: http://drupalcontrib.org/api/drupal/contributions!profile2!profile2.api....

I created a simple module and included this function:


function mymodule_form_profile2_edit_more_info_form_alter(&$form, $form_state)  {
  global $user;
  if (in_array('care provider', array_values($user->roles))){
    dpm($form);
    $form['profile_more_info']['field_lastname']['#required']=1;
    $form['profile_more_info']['field_lastname']['und']['#required']=1;
    $form['profile_more_info']['field_lastname']['und'][0]['#required']=TRUE;
  }
}

The code successfully dumps the form so it is getting evoked, but unfortunately, when the form loads the lastname field remains optional. This is the same as when I tried with hook_form_alter.

mortona2k’s picture

You should be able to make a field required through the interface when you added it. Just go back to "manage fields" for your profile and edit the ones you want and click the required checkbox.

The other Andrew Morton

aaki’s picture

That actually should work. You might want to dump your form after you altered it. And double check the validity of your changes.

What I have done with implementing that profile2 hook was setting a field to be hidden, because it's set automatically. For some reasons I did not want to set this using the field settings.

/**
 * Implementation of hook_form_profile2_edif_PROFILE_NAME_form_alter()
 */
function my_module_form_profile2_edit_custom_profile_form_alter(&$form, $form_state) {
  $form['profile_custom_profile']['field_profile_function'][LANGUAGE_NONE]['#type'] = 'hidden';
}

And BTW instead of using ['und'] you probably should use [LANGUAGE_NONE].

quinns’s picture

I got this to work, in my case we have a profile type called "parent" which contains (among other things) a "last name" field. We wanted the last name to be optional for most users but required for users with the "parent" profile type applied to their account. This is what worked for us:

<?php
function MYMODULE_form_profile2_edit_parent_form_alter(&$form, &$form_state){
	$form['profile_main']['field_profile_last_name'][LANGUAGE_NONE][0]['value']['#required'] = true;
}
?>
simone960’s picture

How come I didn't see ".....[LANGUAGE_NONE][0]['value']....." in the devel? e.g:

$form['profile_main']['field_profile_last_name'][LANGUAGE_NONE][0]['value']['#required'] = true;

The most I can see is "....['und'][0].....", e.g:

$form['profile_main']['field_profile_last_name']['und'][0]['#required'] = true;

What's the different between them actually?

mortona2k’s picture

LANGUAGE_NONE is a php constant that represents no language defined ('und'). Notice how it is all caps and has no quotes. Using 'und' will also work, but LANGUAGE_NONE is more correct.

The other Andrew Morton

Abhishek Verma’s picture

Thanks, worked for me tooo. ;)

rcodina’s picture

Thanks! This worked like a charm!

simone960’s picture

A) I wonder how to write if I want to target the user that has no role assigned, plainly the registered user alone.

if (in_array('care provider', array_values($user->roles))){ ..}

B) What if I want check more than 1 role assigned. E.g If user role is "Administrator" OR "Webmaster" than do something ......

mortona2k’s picture

Try:

if (in_array('care provider', array_values($user->roles)) ||
in_array('Administrator', array_values($user->roles)))
{ ..}

The other Andrew Morton