0

I have written a custom module which builds a custom user_profile_form():

mymodule_user_profile_form($form, $form_state, $user) {
  module_load_include('inc', 'user', 'user.pages');
  $form = user_profile_form($form, $form_state, $user, 'account');
  // doing stuff with $form ...
  return $form;
}

Now, it's working as desired but I noticed a lot of contrib modules are altering the user_profile_form() calling a hook_form_alter() on the $form_id == 'user_profile_form'.

Now, I want all these modifications applied to my custom profile form, too. How to achieve that?

The hacky way would be to go through all modules and change the checks for $form_id == 'user_profile_form' || $form_id == 'mymodule_user_profile_form', but this is a no-go and I would appreaciate a clean way to do this. Any idea?


Update: drupal_get_form() is not the answer as it is not working as desired. This is only returning the $form object without re-building it which causes many issues with theming layer and $form_state['values']. I'm adding this information as requested by moderator, but this is not part of my question. This is the not working code with drupal_get_form() b6d1f1d8 and this is the working code with user_profile_form() d5d43dc0.

6

You should use drupal_get_form($form_id) to get the form instead of directly calling user_profile_form() and it will ensure that all the form alters are applied before you get the form.

Update: Can you explain why you are not using hook_form_alter()?

But if you want to proceed this way, then instead of calling drupal_get_form() you can replace that with the code from drupal_build_form(), except the call to drupal_process_form(),

Replace drupal_get_form() with the following code, copied from drupal_build-form()

$form_state += form_state_defaults();
if (!isset($form_state['input'])) {
   $form_state['input'] = $form_state['method'] == 'get' ? $_GET : $_POST;
}
$form = drupal_retrieve_form('user_profile_form', $form_state);
drupal_prepare_form('user_profile_form', $form, $form_state);
  • drupal_get_form() is not building the form and is a total mess for the user profile form, for example the $form_state is not populated correctly and theming layer is broken. I need to build the form from scratch. – Waqar Lim Jun 23 '14 at 10:12
  • what do you mean by it is not building the form?, can you post your code and also the return value of drupal_get_form() – wolverine Jun 23 '14 at 10:23
  • Reference to full code history: This is not working b6d1f1d8 with drupal_get_form() the $form_state['values'] are empty after _submit() - and this fixed it: d5d43dc0 calling directly the form builder. There are similar discussions on the internet. – Waqar Lim Jun 23 '14 at 10:44
  • @vertoe I'm struggling to understand - is your problem sorted? If so, would be so kind as to put an answer in detailing how you fixed it? If not, could you update the question and make it clear why the approach in the second link in your previous comment didn't work? Would be nice to get the question closed off is all; thanks – Clive Jun 23 '14 at 11:17
  • 1
    @vertoe I have tested the code, you gave, with drupal_get_form() and it is working fine for me. – wolverine Jun 23 '14 at 12:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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