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

torotil’s picture

Now that moment when your searching hours for a solution and just after you ask for it you find it?

This way it works:

	$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_load_include($form_state, 'inc', 'node', 'node.pages');
	$form = drupal_build_form($type . '_node_form', $form_state);
jaypan’s picture

That may work, but I think this is what you want:

    $form = drupal_get_form('node_add', $node);

Untested though.

Contact me to contract me for D7 -> D10/11 migrations.

torotil’s picture

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().

jaypan’s picture

Yeah, I just meant the line after that.

Contact me to contract me for D7 -> D10/11 migrations.

torotil’s picture

drupal_get_form() doesn't allow you to use $form_state directly which in turn keeps you from using form_load_include().

jaypan’s picture

...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.

webchick’s picture

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.