Skip to content

Instantly share code, notes, and snippets.

@mrconnerton
Last active December 4, 2018 15:13
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • 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
<?php
/*
Make Sure you include:
ctools_include('modal');
ctools_modal_add_js();
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', '');
ctools_include('modal');
ctools_include('ajax');
// 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);
exit;
}
@fsierra
Copy link

fsierra commented Aug 13, 2013

Thanks

@d0t15t
Copy link

d0t15t commented Feb 25, 2014

excellent! just what i was looking for - thanks!

@d0t15t
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?

@Padmakani
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.
}

@jlyon
Copy link

jlyon commented Mar 25, 2014

Thanks!

@liquidcms
Copy link

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

@avatxus
Copy link

avatxus commented Oct 10, 2014

you need to include these on the pages / functions where you are creating the link:
ctools_include('modal');
ctools_modal_add_js();

@atul-bhosale
Copy link

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

https://www.drupal.org/project/ctools_automodal
https://www.drupal.org/project/ctools_automodal_admin

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