The drupal_get_form and drupal_retrieve_form functions does not work.


  $output = drupal_get_form('user_profile_form');
  print_r($output);

When you run any of these functions, you would expect a return of the form that you wan't - but instead these functions returns nothing.

Windows XP SP2
WampServer v2.0
PHP v5.2.7

Comments

apaderno’s picture

Title: drupal_get_form and drupal_retrieve_form not working » drupal_get_form() and drupal_retrieve_form() do not work
Damien Tournoud’s picture

Status: Active » Postponed (maintainer needs more info)

You definitely needs to tell us more ;)

apaderno’s picture

Are you sure the form IDs you use are correct? If the form ID is not used by any forms, maybe the functions don't return anything.

dman’s picture

Category: bug » support

Although this post is somewhat less obnoxious than most of your last few other cross-posted winges and annoying complaints on the same thing,
you still have a lot to learn about the difference between:
"I don't understand how to use this"
and
"This sucks and doesn't work"

  include_once(drupal_get_path('module', 'user') . '/user.pages.inc');
  $output = drupal_get_form('user_profile_form');
  print_r($output);

works fine for me, but:

  • it's probably not what you actually want
  • it's not worth explaining why it works unless you can spend some of your own time time looking at examples and docs
  • you need to explain what you are actually trying to do
  • you are acting like a prick.

.dan.

apaderno’s picture

I have never had problems with the functions listed in this report; I would guess dman correctly found what is wrong with code used by DrupalNovice.

DrupalNovice’s picture

dman

Well, seems to me that the only way to getting any attention on this is to post an ISSUE here. You obviously did see those post, so instead of complaining on my frustration you could have write a line to guide me like you have with your post right here. Although your code doesn't seem to work..

1. it's probably not what you actually want
It is what I want. I need the $form array and the form itself for merging it into another form. And I've stated this in the links you provided above..
2. it's not worth explaining why it works unless you can spend some of your own time time looking at examples and docs
I've searched and tested it for three days now. I think that these functions should have worked from the beginning (what use do we have of functions that do not work? =P) - I searched the API, docs and forums for examples and tried with all of them, but nowhere could I find the answer.
3. you need to explain what you are actually trying to do
I'm suprised you even asking me this since, again, one of your links above points you to that answer:

What I'm basically trying to do is merging two forms together. So I'm using the hook_form_alter hook
to get the first form, and then using drupal_get_form to fetch the other form"

And below that I even give you the PHP-code.
4. you are acting like a prick.
Yes, I admit. And I'm sorry.. It's just that when I think I've tried everything, I've searched Drupal - API, docs, the forums... I search Google..and it seems that when I find an close enough answer, it's for Drupal 5.x, so I try to 'translate' those code snippets to D6 and messes things up even more.. I must learn to have much patience. I've had many "issues with Drupal" (all of them pointing back to my own coding error, of course), and some have even taken weeks and months to solve..so I really thought "Hey, finally two functions that would help me do the job so that I won't have to write custom modules and functions myself!"..

Sorry for annoying posts, I was in Big frustration when I wrote them..It was never my intent to offend you or the Drupal Community, or to make anyone mad!

I owe you an apoloqie!

Christo

DrupalNovice’s picture

Status: Postponed (maintainer needs more info) » Fixed

Although your code doesn't seem to work..

That was wrong, dmans code works! I forgot to clear the cache (since I'm testing this on a different page using hook_menu, I need to clear it). I've tried to get this to work with the other form that I want to merge the User Edit form with, but I'm having some issues. I'll try my absolute best to solve those myself!

dman’s picture

OK.
We can move forward from here.

I saw one of your earlier posts, didn't see the others 'tll I went looking for it and saw all your duplicates. :-(
Your tone put me off from spending time walking you through the long story. Your longer, clearer post in another thread was a little better, but all the first posts were not nice to help out on - but that's enough of that.

You are on the right track, but you need to fetch the form array - not the HTML - if you are merging forms. So drupal_retrieve_form is part of the job. drupal_get_form which returns the full HTML is not going to work. But yes, close, so there is hope.

- if merging forms, the parent form is the one that gets submitted, so these new fields will be ignored. Unless you ALSO add the appropriate FORM_submit callback to the $form['#submit'] array.
At that point, you've got most of the pieces. There may be conflicts between a few fields, so you need to examine the two form arrays and make sure they are compatible. I can't quite get exactly what you are doing, but it's likely that any conflicts - maybe the user uid? - will be the same anyway, so it might not hurt too bad.

I learned what I needed to know by examining http://api.drupal.org/api/function/drupal_get_form/6
There are a few steps involved, like drupal_prepare_form() that I needed to trace when merging forms also, but it eventually makes sense. And in fact is really powerful.

The magic I needed to add to your code was loading the user.pages.inc.
drupal_get_form will only work if the form definition is available. With a lot of dynamic, optional, cached loading going on, you can't always get what you want unless you ask for it explicitly these days.

I just looked at your code, did a find-in-files to see if the form you wanted actually existed by that name, and (here is the experience coming in) figured that the form probably wasn't available to you at whatever point in code you were putting this. Easy fix, but did require a bit of background.
And a not-small amount of guesswork to try and understand your actual issue.

Spend a little more time explaining the why, and what you've tried so far (and a lot less complaining about things that obviously do work being broken) and you'll get the answers a bit more promptly.

Keep going. It does require a bit of tracing, but the tools are there.

.dan.

DrupalNovice’s picture

Thanks for your help Dan. I feel stupid, embarrassed and like a total jerk. One should never say those things I said on drupal.org - blaming Drupal for errors I was responsible for; not even in the hours of frustation..so thanks for sticking with me!

The second form I want embed is a CCK Node form. I found the solution for that on TheDrupalBlog. It's a little trickier than just getting the User Edit page..I need to start a $node object and do drupal_prepare_form (as you sugessted), but It's working!

Thank you for the link to the API pages..been there many times but haven't been able to grasp the whole content of those pages, but I'm starting to understand them better now.
I can't thank you enough for your guidelines! With your help, I now have a clear roadmap for achieving what I'm trying to do:
1 Alter the user_profile_form through hook_form_alter.
2 Retrieve the second form - the CCK Node form - through the code given on this page (i.e. use drupal_retrieve_form as you said).
3 Merge the two forms together, and hide/delete any form elements that are uneccessary
4 Check for any conflicts.
5 Add validation functions to the form array.
6 Add submit functions to the form array.

You'd definetly deserve a gift, but I'm short on financial supplies at the moment (I'm working for free for a couple of non-profit organizations)

But you have my gratitude and respect!

Thank you!

dman’s picture

Those steps sound like it will work, and is a good general case procedure.

At the risk of continuing to sound didactic, you still need to describe the goal, not the step.
It's starting to sound like you want to auto-create a node for each new user when they are created. ?
That question may have had a quick answer.
Like usernode, nodeprofile ( d5 :-( ) or what looks like the next-gen : Content Profile

My experiments with something similar were merging term-edit and node-edit forms - as an extension to "node-auto-term". That task did have some conflicts to work around ($term->name vs $node->name) so I know a few of the pitfalls.
... but learned new bits about FAPI as I went.

I do recall that (as you seem to have found) you need to create a dummy node with a couple of values in it. Which ones is trail and error IIRC, but the node userid/name was required. This may be a pitfall if you are creating the user on the fly also. May need a little post-submit-fu

DrupalNovice’s picture

Thanks, for the links and the info.
Actually, I have the Content Profile module installed, and my goal is to have the Content Profile Node form embedded on the User Edit form..I'm trying to make it as simple as possible for the end-users..(and naturally making it harder for myself =P). I realize now that I should have mention this earlier, but I thought that it would have been irrelevant..(although I posted irrelevant information there anyway..;P)

Since the user that will edit the account and profile already exists, I'm hoping that it won't be an issue in this case.. oh, well I bet something else will pop up instead ;-P

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.