0

I've exposed a node_add form and styled it for certain roles to use. The form is being displayed in panels, through an 'article' node type using the PHP text filter. Users can create new nodes with no issues, but there are errors displayed when users try to upload an image in the form:

Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'node_form' not found or invalid function name in drupal_retrieve_form() (line 795 of /var/www/html/includes/form.inc).
Notice: Undefined index: #node in comment_form_node_form_alter() (line 1193 of /var/www/html/modules/comment/comment.module).
Notice: Trying to get property of non-object in comment_form_node_form_alter() (line 1210 of /var/www/html/modules/comment/comment.module).
Notice: Undefined index: #node in menu_form_node_form_alter() (line 629 of /var/www/html/modules/menu/menu.module).
Notice: Trying to get property of non-object in menu_form_node_form_alter() (line 629 of /var/www/html/modules/menu/menu.module).
Notice: Undefined index: #node in menu_form_node_form_alter() (line 630 of /var/www/html/modules/menu/menu.module).
Notice: Trying to get property of non-object in menu_form_node_form_alter() (line 630 of /var/www/html/modules/menu/menu.module).
Notice: Undefined index: field_obs_image in file_ajax_upload() (line 271 of /var/www/html/modules/file/file.module).
Notice: Undefined index: #suffix in file_ajax_upload() (line 280 of /var/www/html/modules/file/file.module).

The actual node form code looks like this:

module_load_include('inc', 'node', 'node.pages');
$form = node_add('observation');
print drupal_render($form);

I have also tried this code with the same results:

$node_type = 'observation';
$form_id = $node_type . '_node_form';
// maybe add current users info
global $user;
// create a blank node

$node = new stdClass(); 
$node->uid = $user->uid; 
$node->name = (isset($user->name) ? $user->name : ''); 
$node->type = $node_type; 
$node->language = ''; 
node_object_prepare($node); 
$output = drupal_render(drupal_get_form($form_id, $node));

print $output;

The content type being added is 'observation'. I am also using the identical technique on another page with a different content type, and it results in the exact same error. The error appears directly above the image field during the AJAX image upload call.

I have also done a debug_backtrace() near line 795 of form.inc and can post the results if requested.

I have read all of these articles, to no avail:

Does anyone have an idea as to where I can start looking? This seems to be a tough problem. Thanks for your time!

4 Answers 4

1

You should get node_form in this way:

{
  ...
  // For node_add
  module_load_include('inc', 'node', 'node.pages');
  return drupal_render(node_add('type'));
}

or

{
  ...
  // For node edit
  $node = node_load($nid);
  module_load_include('inc', 'node', 'node.pages');
  return node_page_edit($node);
}

And call form_load_include function on form alter hook:

/**
 * Implements hook_form_FORM_ID_alter().
 */
function mymodule_form_[your_form_id]_alter(&$form, &$form_state) {
  form_load_include($form_state, 'inc', 'node', 'node.pages');
  // your code goes here if needed
}
2
  • This answer has a useful concept because it actually shows how to call form_load_include - I was struggling how to get a form_state. However I think the name of the hook is a bit mixed up, not sure what the _steps is doing, and it needs an actual form ID.
    – AdamS
    Sep 3, 2015 at 14:10
  • @AdamS Thanks for noticing strange function name, it was a long time ago when I added this answer. I guess it was copied from some working code and that function was a sub-function for an actual hook.
    – Dmitriy
    Sep 4, 2015 at 13:49
3

I believe the problem is, roughly, that while you've ensured the include happens before initial form build, that include doesn't happen when the AJAX file upload processing attempts to rebuild the form... So node_form() function is undefined and things fall apart.

The solution that worked for me was to use form_load_include() in place of module_load_include(). According to its documentation, this alternate function exists for this express purpose - "ensures that the include file is loaded whenever the form is processed."

1
  • The first paragraph is a great explanation of the problem and form_load_include is an important part of the answer. However I'm not sure if form_load_include can be used "in place of module_load_include". The parameters are different for one thing form_state is needed and at least in my case, all I had to hand was $form. The answer by Dmitriy gives a bit more of a clue about that part.
    – AdamS
    Sep 3, 2015 at 14:20
0

For what it's worth, I never did find a solution for these errors. I remain convinced that the problem lies in the calling of the node_add() function from a php-text-type node.

My final solution was to use the Form Block module instead.

2
  • 2
    the only difference between your code and the one in Form block module is that form block doesn't user drupal_render(). It just returns the whole form array. Jun 20, 2012 at 15:18
  • It looks to me as if Form block module calls form_load_include which presumably is the crucial difference.
    – AdamS
    Sep 3, 2015 at 14:07
0

Yeah, apparently you should add these line to the menu item array:

'file path' => drupal_get_path('module', 'node'),
'file' => 'node.pages.inc',

it is not enough to just add module_load_include('inc', 'node', 'node.pages'); before printing the form.

4
  • To which menu item should those items be added?
    – apaderno
    Oct 21, 2012 at 14:11
  • to the one for the page where you are rendering your form. you can use hook_menu_alter() to add these lines. I remember I once did the same for the system/ajax menu item so you may try this as well Oct 21, 2012 at 15:35
  • Maybe you can answer this question; the OP is wondering where to add those lines on his code.
    – apaderno
    Oct 21, 2012 at 15:38
  • I see the idea, but not sure how this one is going to work out. How can you know all the possible ajax paths that might be affected? And given these paths apply to many different scenarios, how to know exactly when to alter the paths? I think form_load_include has been designed to solve this problem.
    – AdamS
    Sep 3, 2015 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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