By torotil on
I'm trying to load a content-type's node creation form on a custom path (using a page callback). At first I've tried to simply use node_add. Which worked until I tried to upload the first image. As it seems the AJAX request doesn't "know" anything about the function "node_form" because it doesn't include modules/node/node.pages.inc.
Digging a bit deeper I found the array $form_state['build_info']['files'] which seems to solve exactly this problem. It declares which additional files are needed to render a form. But it doesn't work.
My current approach looks like this:
global $user;
$type = 'something';
$node = (object) array(
'uid' => $user->uid,
'name' => (isset($user->name) ? $user->name : ''),
'type' => $type,
'language' => LANGUAGE_NONE,
);
$form_state['build_info']['args'] = array($node);
$form_state['build_info']['files'][] = array('inc', 'node', 'node.pages');
module_load_include('inc', 'node', 'node.pages');
$form = drupal_build_form($type . '_node_form', $form_state);
Any suggestions?
Comments
Now that moment when your
Now that moment when your searching hours for a solution and just after you ask for it you find it?
This way it works:
That may work, but I think
That may work, but I think this is what you want:
Untested though.
Contact me to contract me for D7 -> D10/11 migrations.
This only works if you
This only works if you include node.pages.inc with the menu item's file setting (in hook_menu()) else it breaks AJAX as I've already mentioned. If the include file containing the form callback is not included via the menu item you need to manually put it in $form_state,, which is done by form_load_include().
Yeah, I just meant the line
Yeah, I just meant the line after that.
Contact me to contract me for D7 -> D10/11 migrations.
drupal_get_form() doesn't
drupal_get_form() doesn't allow you to use $form_state directly which in turn keeps you from using form_load_include().
...but you are including the
...but you are including the file before you ever build the form.
Anyways, you have obviously done it, and I haven't even tested it, so regardless of whether I think the other way may be better (and it may not, as I have said, I haven't tested it), you have found a working solution which is good, and hopefully can help someone else having the same issue in the future.
Contact me to contract me for D7 -> D10/11 migrations.
Just dropping by to say thanks!
Thanks so much, torotil, for both posting your question and your own answer. :) I had gotten as far as form_load_include() but couldn't figure out the rest. I used your solution as part of #2159813: Display parts of the issue edit form instead of the comment form on issue pages.