No 'node_form' function using drupal_execute();

fractile81 - December 17, 2007 - 17:29
Project:Documentation
Component:Developer Guide
Category:support request
Priority:normal
Assigned:Unassigned
Status:closed
Description

I'm running into a problem creating a node on a non-node page using drupal_execute();. Here's the error:

warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'node_form' was given in C:\htdocs\drupal6\includes\form.inc on line 340.

I know that node_form(); can be found in the node.pages.inc file, which appears to not be included because:

  1. I'm not on a node/add or node/%node/edit page.
  2. Nothing is being themed (the error is generated in drupal_retrieve_form();)

So, my question is whether there's a way to get this file included without implicitly including it with something like:

<?php
require_once drupal_get_path('module', 'node') . '/node.pages.inc');
?>

Should this re-classified as a bug?

-Fractile81

#1

asimmonds - December 21, 2007 - 14:20

I think drupal_execute() is somewhat of a edge case at the moment, it works but you have to be somewhat aware of what other code is required for the form to work properly, anyways I'm successfully using drupal_execute() to create content from within my install profiles for 6.x.
I use:

module_load_include('', 'node', 'node.pages.inc');

then the usual:
drupal_execute('page_node_form', $form_state, $node);

to create the node.

#2

mpare - December 21, 2007 - 16:13

I'm running into similar issues using drupal_get_form, also effecting node_add, node_page_edit, etc... I'm still looking to see if I'm doing something stupid or if there really is an issue here.

#3

mpare - December 21, 2007 - 17:06
Version:6.0-beta4» 6.x-dev

#4

fractile81 - December 21, 2007 - 17:07
Version:6.x-dev» 6.0-beta4

So, yeah, I suppose that's the question, do we:

1) Do the module_load_include(); as asimmonds suggests (I like that a lot better than how I did it), or
2) Should form functions, as a standard, be pulled into the main module function for the sake of drupal_execute();?

As D6 is now in RC1, it might be a little late in the game for #2 to happen, mandating #1 for now. However, this might be something good to push into the D7 arena. Any thoughts on this would be appreciated!

#5

fractile81 - December 21, 2007 - 17:08
Version:6.0-beta4» 6.x-dev

Oops, cross-posted.

#6

mpare - December 21, 2007 - 18:17
Project:Drupal» Documentation
Version:6.x-dev»
Component:node system» Developer Guide
Status:active» closed

I don't think that this is really a core issue but a documentation issue for d.o. I'm updating node 114774, Converting 5.x Modules... to reflect this necessary step for loading node forms from third party modules. I'm also changing the issue to reflect this. I think this is part of the new optimization practices implemented in D6 but can't say that with 100% confidence that's just the obvious explanation. Also a change to the above posted code.

<?php
  module_load_include
('', 'node', 'node.pages.inc')
?>

should probably read

<?php
  module_load_include
('inc', 'node', 'node.pages');
?>

According to the api docs and out of the simple fact that it doesn't work.

If the change of status of this issue is incorrect please feel free to revert.

-mpare

#7

mpare - December 21, 2007 - 19:45

I've updated the Module Conversion guide to reflect these new changes, http://drupal.org/node/114774#module_load_include.

#8

shunting - December 23, 2007 - 00:49

Thanks. This was driving me nuts.

mpare @7, a small editorial tweak?

Now:

A common use for this implementation is when a custom module needs to load a node form

Suggested:

A common use for this implementation is when a custom module needs to load a node form (for example, via drupal_execute) ...

Now drupal_execute will show up in a search....

#9

shunting - December 23, 2007 - 00:57

But don't we have a documentation bug in form.inc? This definitely makes drupal_execute look like a normal thing to do, and not at the bleeding edge.

This:

* // Create a new node
* $form_state = array();
* $node = array('type' => 'story');
* $form_state['values']['title'] = 'My node';
* $form_state['values']['body'] = 'This is the body text!';
* $form_state['values']['name'] = 'robo-user';
* $form_state['values']['op'] = t('Save');
* drupal_execute('story_node_form', $form_state, $node);

Should become this?
* // Create a new node
* $form_state = array();
* $node = array('type' => 'story');
* $form_state['values']['title'] = 'My node';
* $form_state['values']['body'] = 'This is the body text!';
* $form_state['values']['name'] = 'robo-user';
* $form_state['values']['op'] = t('Save');
* // include node_form
* module_load_include('inc', 'node', 'node.pages');
* drupal_execute('story_node_form', $form_state, $node);

#10

mpare - December 28, 2007 - 20:53

I don't really think there is a bug in that specific location. If there is a documentation bug in core for this issue I believe it is probably in CHANGELOG.txt and that's if there is one. See this issue is much broader than just forms or node functions it pertains to the entire drupal core anytime you are needing to use functionality contained in a specialized include file. I dare not dictate this issue alone and other feed back from others is more than welcome and even encouraged.

 
 

Drupal is a registered trademark of Dries Buytaert.