0

I want to implement the create content form programmatically. In hook_menu() I have the following code.

$items['create/content/video'] = array(
  'title' => 'Create Video',
  'page callback' => 'create_content_video',
  'access arguments' => array('create video'),
  'type' => MENU_LOCAL_TASK,
  'weight' => 1
);

In hook_init(), I have the following code.

if (arg(0)=='node' && arg(1)=='add') {
  drupal_goto('create/content/'.arg(2));
}

In hook_theme(), I have the following code.

'video_node_form' => array(
  'arguments' => array(
    'form' => NULL,
  ),
  'template' => 'my_theme_video_form',
  'render element' => 'form',
),

In the page callback, I have the following code.

function create_content_video() {
  module_load_include('inc', 'node', 'node.pages');
  return node_add('video');
}

In the template file, I render form element correctly. My problem is with the the AJAX uploader in the form. Without, I use the upload button to upload the file, and the node is correctly saved; when I use the AJAX uploader, it returns an error.

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/mysite/includes/form.inc).
Notice: Undefined index: #node in comment_form_node_form_alter() (line 1193 of /var/www/mysite/modules/comment/comment.module).
Notice: Trying to get property of non-object in comment_form_node_form_alter() (line 1210 of /var/www/mysite/modules/comment/comment.module).
Notice: Undefined index: #node in menu_form_node_form_alter() (line 629 of /var/www/mysite/modules/menu/menu.module).
Notice: Trying to get property of non-object in menu_form_node_form_alter() (line 629 of /var/www/mysite/modules/menu/menu.module).
Notice: Undefined index: #node in menu_form_node_form_alter() (line 630 of /var/www/mysite/modules/menu/menu.module).
Notice: Trying to get property of non-object in menu_form_node_form_alter() (line 630 of /var/www/mysite/modules/menu/menu.module).
Notice: Undefined index: #node in translation_form_node_form_alter() (line 135 of /var/www/mysite/modules/translation/translation.module).
Notice: Trying to get property of non-object in translation_form_node_form_alter() (line 135 of /var/www/mysite/modules/translation/translation.module).
Notice: Undefined index: type in xmlsitemap_node_form_node_form_alter() (line 136 of /var/www/mysite/sites/all/modules/xmlsitemap/xmlsitemap_node/xmlsitemap_node.module).
Notice: Undefined index: nid in xmlsitemap_node_form_node_form_alter() (line 136 of /var/www/mysite/sites/all/modules/xmlsitemap/xmlsitemap_node/xmlsitemap_node.module).
Notice: Undefined index: field_news_attachment in file_ajax_upload() (line 276 of /var/www/mysite/modules/file/file.module).
Notice: Undefined index: #suffix in file_ajax_upload() (line 285 of /var/www/mysite/modules/file/file.module).

When I create content without this method, it works fine.
I am using Drupal 7.14.

  • This sounds like an awful lot of trouble, what exactly is wrong with the standard node add form? If you're just worried about the displayed path why not just add a URL alias from 'create/content/video' to 'node/add/video'? – Clive May 30 '12 at 8:32
  • i upgarde a website based on drupal 6, drupal 6 not support create themplate for add content type, but in drupal 7 support it with node--contentype--add.tpl.php, really i suggest the manager of team that implement it with this new method but he insistence to impelenet it with old version and with create_content_module(companye written module). – Yusef May 30 '12 at 9:42
  • Can you display a backtrace on the first error message? Are the error messages copied verbatim? It looks like there might be a missing underscore in the initial error? – Letharion May 30 '12 at 11:08
  • Company policies that dictate/micromanage how the developers solve problems should have a kick, somewhere that hurts. – Letharion May 30 '12 at 11:09
  • @ Letharion, tnx for your answers, finally i decide to implement it with another method, page--node--add--video.tpl.php , but in this tpl when i print $form return null , can u help me about this? – Yusef May 30 '12 at 11:15
1

I agree with Clive, that this seems like a great deal of trouble, but what you should be doing generally, is calling field_attach_form(), and more specifically for nodes, node_form(), which calls the former. The arguments it expects are

You can see an example on the first here: Is displaying a working Field Widget Form on its own possible?

  • tnx for your replies but please read my problem exactly, your answer about any thing else, my problem about response of ajax uploader of cck file uploading.a form and create and save of form is ok, problem is just in ajax uploading – Yusef May 30 '12 at 10:37
  • Yes, I realize, I just interpreted it as the original form being incorrectly built. In your edited question however, I can see that you properly get the form with node_add(), which means your problem is likely somewhere else. – Letharion May 30 '12 at 11:07
1

If you are just trying to theme the edit form for the "video" content type, then you can simply use the following code.

function mymodule_form_video_node_form_alter(&$form, &$form_state) {
  $form['#theme'] = 'mymodule_video_form';
}

Your hook_theme() implementation needs to be changed, as:

  • arguments is a property that is not used in Drupal 7; you should use variables.
  • As reported in the documentation, "each information array must contain either a 'variables' element or a 'render element' element, but not both."

If you are implementing that code just to be able to use create/content/video, instead of node/add/video, then I would suggest not to implement such code.
From the code you are showing, I see there is something wrong: You define a callback for create/content/video, but your hook_init() implementation is redirecting the users also for other content types, as it is not checking the value of arg(2).

  • tnx for your answer, in my tpl file i have $ form and <?php print drupal_render_children($form);?> not have a error and all forms render correctly for me, i have a comany policy to implement with this method althought i know we have a easier way, notice that my problem in in file ajax attach uplading not a form rendering – Yusef May 30 '12 at 10:28
  • tnx for notify me with this arguments is a property that is not used in Drupal 7; you should use variables. As reported in the documentation, "each information array must contain either a 'variables' element or a 'render element' element, but not both. this is good info but my problem in another thing – Yusef May 30 '12 at 10:31
  • 1
    The problem is that you are implementing code that should not be used. – kiamlaluno May 30 '12 at 11:14
0

As per this post, if you've a file upload widget, you should add the following lines th the menu array ($items):

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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