I have custom content type that is basically a NID. My add form collects NID, my render callback renders node from NID. I would like my edit form to show node edit form from NID. I get a form to display, but got problems with image field. After ajax request it throws errors.

Very simplified code that adds node edit form to my edit form. Image content type has image field.

<?php
  global $user;
  module_load_include('inc', 'node', 'node.pages');
  $node = (object) array('uid' => $user->uid, 'name' => (isset($user->name) ? $user->name : ''), 'type' => 'image', 'language' => LANGUAGE_NONE);
  $form += node_form($form, $form_state, $node);
?>

Comments

mansspams’s picture

Notice: Undefined index: maga_pane_content_type_edit_form in drupal_retrieve_form() (line 760 of ...includes\form.inc).
Warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'maga_pane_content_type_edit_form' was given in drupal_retrieve_form() (line 795 of ...includes\form.inc).
Notice: Undefined index: field_image_image in file_ajax_upload() (line 271 of ...modules\file\file.module).
Notice: Undefined index: #suffix in file_ajax_upload() (line 280 of...modules\file\file.module).

merlinofchaos’s picture

Status: Active » Fixed

You need to add the file you're including to the form state. Use form_load_include() instead.

mansspams’s picture

Status: Fixed » Active

Im sorry, I still cannot seem to get it. Below is bare minimum of code that shows new image node form and still fails on upload button.

I looked at ctools and see ctools_form_include() and ctools_form_include_file(), could these help? I looked at Fieldable panels panes code, which seemed recent and dealt with field forms. In both modules i see in comments "module_load_include uses an unfortunately annoying syntax to work..." and then I see some arrays like $form_state['build_info']['files']["$module:$name.$type"]. Should I build such array for node form? Which "files" needs to be included? In short, what would be approximate code or workflow to get this to work?

function maga_pane_content_type_edit_form($form, &$form_state) {

  global $user;
  form_load_include($form_state, 'inc', 'node', 'node.pages');
  $node = (object) array('uid' => $user->uid, 'name' => (isset($user->name) ? $user->name : ''), 'type' => 'image', 'language' => LANGUAGE_NONE);
  $form += node_form($form, $form_state, $node);

  return $form;
}

May be related: I also noticed that code above works for 'add form' in plugin defined as function ctools_node_content_type_content_types($plugin) (it uses ctools node content_type?), but not in my custom $plugin array. Also noticed that fieldable_panels_panes defines plugin as function instead of $plugin array, with following comment: "We have to use this because the form here can be loaded via form caching and if this .inc file is loaded before the plugin is requested, the $plugin = array() notation doesn't work." Could this be a problem? Or (more likely) code above is shitty.

mansspams’s picture

I managed my node edit forms to work by cloning, hacking and adapting fieldable_panels_panes. It looks like following code makes the difference (in add form, before calling form_load_include($form_state, 'inc', 'node', 'node.pages');)

  $plugin = $form_state['plugin'];
  $module = $plugin['module'];

  // Cut the module path out.
  $path = str_replace(drupal_get_path('module', $module) . '/', '', $plugin['path'] . '/' . $plugin['file']);
  $type = pathinfo($path, PATHINFO_EXTENSION);
  $name = str_replace('.' . $type, '', $path);

  // Now add this to the build info files so that AJAX requests will know to load it.
  $form_state['build_info']['files']["$module:$name.$type"] = array(
    'type' => $type,
    'module' => $module,
    'name' => $name,
  );

I wonder if this is already somewhere in ctools module?

swentel’s picture

Status: Active » Fixed

As merlinofchaos pointed out, use form_load_include(). Subtle detail however: you changed the module_load_include to form_load_include() to include the node.pages.inc file, but what you should actually include is the file of your custom content type.

  // Make sure our own file is included.
  form_load_include($form_state, 'inc', 'mymodule', 'plugins/content_types/NAME_OF_YOUR_CONTENT_TYPE/FILE);

  global $user;
  form_load_include($form_state, 'inc', 'node', 'node.pages');
  $node = (object) array('uid' => $user->uid, 'name' => (isset($user->name) ? $user->name : ''), 'type' => 'image', 'language' => LANGUAGE_NONE);
  $form += node_form($form, $form_state, $node);

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.