Fix for put a node form in a ctools modal (fixed)

  1. /**
  2.  * Implementation of hook_menu().
  3.  */
  4. function somemodule_menu() {
  5.   $items = array();
  6.   $items['initial-page'] = array(
  7.     'title' => t('My title'),
  8.     'page callback' => 'somemodule_initial_page',
  9.     'access arguments' => array('access content'),
  10.     'type' => MENU_NORMAL_ITEM,
  11.   );
  12.   foreach (node_get_types('types', NULL, TRUE) as $type) {
  13.     $type_url_str = str_replace('_', '-', $type->type);
  14.     $items['somemodule/%ctools_js/node-add/'. $type_url_str] = array(
  15.       'title' => 'Create content',
  16.       'page callback' => 'somemodule_node_add_page',
  17.       'page arguments' => array(1, 3, 4, 5, 6),
  18.       'access callback' => 'node_access',
  19.       'access arguments' => array('create', $type->type),
  20.       'type' => MENU_CALLBACK,
  21.     );
  22.   }
  23.   return $items;
  24. }
  25.  
  26. function somemodule_initial_page() {
  27.   // Include the CTools tools that we need.
  28.   ctools_include('ajax');
  29.   ctools_include('modal');
  30.  
  31.   // Add CTools' javascript to the page.
  32.   ctools_modal_add_js();
  33.  
  34.   return ctools_modal_text_button(t('Create page'), 'livingclassic/nojs/node-add/page/redirect/some---other---page', t('Create a page'));
  35. }
  36.  
  37. function somemodule_node_add_page($js = NULL, $type, $action, $destination, $modal_destination) {
  38.   global $user;
  39.  
  40.   $types = node_get_types();
  41.   $type = isset($type) ? str_replace('-', '_', $type) : NULL;
  42.   ctools_include('ajax');
  43.   ctools_include('modal');
  44.   // If a node type has been specified, validate its existence.
  45.   if (isset($types[$type]) && node_access('create', $type)) {
  46.     // Initialize settings:
  47.     $node = array('uid' => $user->uid, 'name' => (isset($user->name) ? $user->name : ''), 'type' => $type, 'language' => $user->language);
  48.  
  49.     $form_state = array(
  50.       'ajax' => $js,
  51.       'node' => $node,
  52.     );
  53.    $form_state['args'] = array($node);
  54.     $output = ctools_modal_form_wrapper('somemodule_node_form', $form_state);
  55.     if (empty($output)) {
  56.       // empty $output signifies success, so we'll use it as our $commands
  57.       // array.
  58.       $output = array();
  59.       if ($action == 'redirect') {
  60.         $destination = str_replace('---', '/', $destination);
  61.         $output[] = ctools_ajax_command_redirect($destination);
  62.       }
  63.       else if($action == 'inplace' && $destination == 'modal' && $modal_destination) {
  64.         $output[] = ctools_ajax_command_reload();
  65.       }
  66.       else {
  67.         $output[] = ctools_ajax_command_reload();
  68.       }
  69.     }
  70.   }
  71.   ctools_ajax_render($output);
  72. }
  73.  
  74. function somemodule_node_form(&$form_state) {
  75.   $node = $form_state['node'];
  76.   unset($form_state['node']);
  77.   module_load_include('inc', 'node', 'node.pages');
  78.   $form = drupal_retrieve_form($node['type'] .'_node_form', $form_state, (object)$node);
  79.   drupal_prepare_form($node['type'] .'_node_form', $form, $form_state);
  80.   return $form;
  81. }

Submit Fix

Any tags you'd like to associate with your code, delimitered by commas (example: Views, CCK, Module, etc).
Select the syntax highlighting mode to use.