Ctools modalframe for node creation forms

Posted on 24 Mar
Expanding on my calendar and module code here, this is what I used to open a calendar event creation form into a ctools modalframe popup. The content type is, "public_event". ctools_modal_text_button() creates the button that loads the modalframe, which I've added to specific block content on my site through hook_block_view_alter.
<?php /** * @file * Create Event module file * * This module allows anonymous users to create calendar events. */ /** * Implementation of hook_menu */ function mymodule_menu() { $items = array(); $items['create/%ctools_js/public-event'] = array( 'title' => 'Submit an event', 'page callback' => 'modal_content', 'page arguments' => array(1), 'access callback' => TRUE, 'type' => MENU_CALLBACK, ); return $items; } /** * Function for creating ctools text button */ function mymodule_link() { global $user; ctools_include('ajax'); ctools_include('modal'); ctools_modal_add_js(); $output = ctools_modal_text_button(t('Submit an event'), 'create/nojs/public-event', 'public-event'); return $output; } /** * Implementation of hook_block_view_alter * Just adding ctools text button to a block on the calendar page */ function mymodule_block_view_alter(&$data, $block) { if (($block->delta == 'submit_event') && ($block->context == 'event_calendar')) { $string = $data['content']; $data['content'] = array(); $data['content'] = array( '#type' => 'markup', '#markup' => $string.'<div class="modal">'.mymodule_link().'</div>', ); } } /** * Callback function for loading events node/add form into a ctools modal window */ function modal_content($js = FALSE) { global $user; ctools_include('node.pages', 'node', ''); ctools_include('modal'); ctools_include('ajax'); $type = 'public_event'; $node = (object) array( 'uid' => $user->uid, 'name' => (isset($user->name) ? $user->name : ''), 'type' => $type, 'language' => LANGUAGE_NONE); if (!$js) { return drupal_get_form($type . '_node_form', $node); } $form_state = array( 'title' => t('Submit an event'), 'ajax' => TRUE, ); $form_state['build_info']['args'] = array($node); form_load_include($form_state, 'inc', 'node', 'node.pages'); $output = ctools_modal_form_wrapper($type . '_node_form', $form_state); if (!empty($form_state['executed'])) { $output = array(); $output[] = ctools_modal_command_display( t('Successful.'), '<div class="modal-message"> <h3>Thanks for submitting your event to us.</h3> <p>Your submission will be approved by an editor before it will appear on the site.</p></div>'); } print ajax_render($output); exit; } /** * Implementation of hook_form_alter * Alter public_event creation form */ function mymodule_form_alter(&$form, &$form_state, $form_id) { if ($form_id == 'public_event_node_form') { $form['author']['#access'] = FALSE; $form['comment_settings']['#access'] = FALSE; $form['locations']['#weight'] = 3; $form['locations']['#title'] = t('Location or venue'); $form['actions']['submit']['#value'] = t('Submit your event for review'); $message = t('<p><strong>Create a public event. All submissions will be reviewed and approved by an editor before appearing on the site. </strong></p>When entering events:</ br> <ul> <li>Enter the event at least 2 weeks prior to event date.</li> <li>Fill out all information required as completely as possible.</li> </ul>'); drupal_set_message($message, $type = 'status', $repeat = FALSE); } }


