Last active
December 4, 2018 15:13
-
-
Save mrconnerton/1979037 to your computer and use it in GitHub Desktop.
node form in ctools modal drupal 7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
excellent! just what i was looking for - thanks!
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?
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.
}
Thanks!
Does this still work? All i get is a json screendump.
you need to include these on the pages / functions where you are creating the link:
ctools_include('modal');
ctools_modal_add_js();
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
Thanks