Skip to content

Instantly share code, notes, and snippets.

Last active December 4, 2018 15:13
Show Gist options
  • Save mrconnerton/1979037 to your computer and use it in GitHub Desktop.
Save mrconnerton/1979037 to your computer and use it in GitHub Desktop.
node form in ctools modal drupal 7
Make Sure you include:
On the pages you put your link.
* Implements hook_menu().
function mymodule_menu() {
$items['mymodule/%ctools_js/add'] = array(
'page callback' => 'mymodule_node_add_modal_callback',
'page arguments' => array(1),
'access arguments' => array('access content'),
return $items;
* mymodule node add modal callback
function mymodule_node_add_modal_callback($js = FALSE) {
global $user;
// If people aren't using javascript, then I just boot em. sorry. its 2011.
if (!$js) return "Javascript required";
// Include your ctools crap here
ctools_include('node.pages', 'node', '');
// Create a blank node object here. You can also set values for your custom fields here as well.
$node = (object) array(
'uid' => $user->uid,
'name' => (isset($user->name) ? $user->name : ''),
'type' => 'mytype',
'language' => LANGUAGE_NONE,
$form_state = array(
'title' => t('Add my conten type'),
'ajax' => TRUE,
$form_state['build_info']['args'] = array($node);
// change this to your type node form
$output = ctools_modal_form_wrapper('mytype_node_form', $form_state);
// This means the form has been exectued
if (!empty($form_state['executed'])) {
$output = array();
// Close the modal
$output[] = ctools_modal_command_dismiss();
// I use this method a lot on pages that have views, so what I do is get the latest view content
// with the latest node and replace the current view. magic!
$html = views_embed_view('my_view', 'my_display';
$output[] = ajax_command_html('.view-display-id-my_display', $html);
print ajax_render($output);
Copy link

fsierra commented Aug 13, 2013


Copy link

d0t15t commented Feb 25, 2014

excellent! just what i was looking for - thanks!

Copy link

d0t15t commented Feb 28, 2014

so now i want build a similar form, but add ctools multi-step functionality, but am not getting it. any clue on how to approach that?

Copy link

Thanks, great answer.

if (!$js) return "Javascript required"
Repalce below line.
if (!$js) {
return drupal_get_form('mytype_node_form'); // mytype_node_form this is your form id.

Copy link

jlyon commented Mar 25, 2014


Copy link

Does this still work? All i get is a json screendump.

Copy link

avatxus commented Oct 10, 2014

you need to include these on the pages / functions where you are creating the link:

Copy link

To open node add/edit form in modal window, I tried below modules & it works properly for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment