I'm writing a module to get a simple node add form into a ctools modal dialog. I get the form into the modal ok, but when I click save I get this ajax error
An AJAX HTTP error occurred. HTTP Result Code: 500 Debugging information follows. Path: /itin-add1/ajax StatusText: Service unavailable (with message) ResponseText: EntityMalformedException: Missing bundle property on entity of type node. in entity_extract_ids() (line 7721 of /mnt/p0/data/www/docroots/mkm/includes/common.inc).
I've tried several examples, currently working with this one http://deeson-online.co.uk/labs/insert-form-pop-modal-ctools-and-drupal-7
I've also been examining the modal_forms module and it appears to be doing basically the same thing as the link above. As I say I get the form to appear in the modal ok, but saving the form fails. I've yet to find a good complete example of a node add in a modal.
I appreciate any advice or pointers to a good example of node add in a modal.
Thanks
<?php
# testmodal module
function testmodal_ctools_render_alter(&$info, &$page, &$context) {
$data = array_values($context['contexts']);
if (is_array($data) && count($data) && property_exists($data[0], "data")) {
testmodal_node_view($data[0]->data, 'full', '');
}
}
function testmodal_node_view($node, $view_mode, $langcode) {
ctools_include('modal');
ctools_modal_add_js();
}
# Insert a form into a pop-up modal with CTools and Drupal 7
# http://deeson-online.co.uk/labs/insert-form-pop-modal-ctools-and-drupal-7
function testmodal_menu() {
# We dont' need this. Our popup links appear on the /products page
#
# 1. "/mymodule/page" - A page that a user might travel to and see the
# link that, when clicked, will cause a modal to popup with the form in it.
# 2. "/mymodule/%ctools_js" - The second is a callback which returns the HTML of
# the modal form. The second argument is a variable. It's value will be either "nojs",
# if the users browser does not handle JavaScript, or "ajax" if it does.
$items['itin-add0'] = array(
'page callback' => 'testmodal_form',
'title' => "hook",
'access callback' => TRUE,
);
$items['itin-add1/%ctools_js'] = array(
'page callback' => 'testmodal_callback',
'page arguments' => array(1),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
# 3 link generator - don't need it it's for their example
# 4 page call back - need to determine correct hook to load
# ctools_include('modal');
# ctools_modal_add_js();
# 5. Define the ajax callback
# This is what runs when the user's browser makes a request to either "/mymodule/nojs" or
# "/mymodule/ajax". In the case of nojs, the full form must be returned as normal. In the
# case of "ajax", the ajax delivery system sends back the information on creating a modal.
function testmodal_callback($ajax) {
ctools_include('modal');
ctools_modal_add_js();
if ($ajax) {
$form_state = array(
'ajax' => TRUE,
'title' => t('Itinerary Item Add Modal Form'),
);
// Use ctools to generate ajax instructions for the browser to create
// a form in a modal popup.
$output = ctools_modal_form_wrapper('testmodal_form', $form_state);
// If the form has been submitted, there may be additional instructions
// such as dismissing the modal popup.
if (!empty($form_state['ajax_commands'])) {
$output = $form_state['ajax_commands'];
}
// Return the ajax instructions to the browser via ajax_render().
print ajax_render($output);
drupal_exit();
} else {
return drupal_get_form('testmodal_form');
}
}
# 6. Create an example form The next thing to do for this example is to create
# the form and its submission function. In 5 above, we told ctools the forms name
# was "mymodule_form", so lets create that form now with a textfield and a submit
# button. You'll note that there is nothing special about the form itself and any Drupal
# form could therefore be used.
function testmodal_form() {
global $user;
module_load_include('inc', 'node', 'node.pages');
$node_type = 'itinerary_item';
$form_id = $node_type . '_node_form';
$node = new stdClass();
$node->type = 'itinerary_item';
node_object_prepare($node);
$node->language = LANGUAGE_NONE;
$node->body[LANGUAGE_NONE][0]['value']='';
$node->uid = $user->uid;
$node->title = 'testmodal title';
$node->status = NODE_PUBLISHED;
$node->promote = 0;
$node->comment = 0;
$node->active = 1;
$form = drupal_get_form($form_id, $node);
return $form;
}
# 7. Create the submission function When submitting the form we want to send back
# ajax commands to the browser to dismiss the modal and change the text on the link we
# clicked. You can see the ajax_command_replace function is targeting the div with the
# id set by our helper function back in 3. In situations where you are not using your
# own form but some other in the Drupal system you might need to add additional ajax
# actions by including your own submission functions to the form.
function testmodal_itinerary_item_node_form_submit($form, &$form_state) {
# $form_state['ajax_commands'][] = ctools_modal_command_dismiss();
}
/**
* Drupal form submit handler.
*/
function itinerary_item_node_form_submit($form, &$form_state) {
// Generate the new link using the submitted text value.
# $link = _mypopup_make_link($form_state['values']['new_link_text']);
// Tell the browser to close the modal.
$form_state['ajax_commands'][] = ctools_modal_command_dismiss();
// Tell the browser to replace the old link with the new one.
# $form_state['ajax_commands'][] = ajax_command_replace('#magical-modal-link', $link);
}