Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@fsierra fsierra commented Aug 13, 2013

Thanks

@IsaacBigsbyTrogdon

This comment has been minimized.

Copy link

@IsaacBigsbyTrogdon IsaacBigsbyTrogdon commented Feb 25, 2014

excellent! just what i was looking for - thanks!

@IsaacBigsbyTrogdon

This comment has been minimized.

Copy link

@IsaacBigsbyTrogdon IsaacBigsbyTrogdon 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

This comment has been minimized.

Copy link

@Padmakani Padmakani commented Mar 10, 2014

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

This comment has been minimized.

Copy link

@jlyon jlyon commented Mar 25, 2014

Thanks!

@liquidcms

This comment has been minimized.

Copy link

@liquidcms liquidcms commented Sep 30, 2014

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

@avatxus

This comment has been minimized.

Copy link

@avatxus 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

This comment has been minimized.

Copy link

@atul-bhosale atul-bhosale commented Dec 15, 2014

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
You can’t perform that action at this time.