Community

Create New Content in a ctools Modal Frame

The title pretty much sums it up. I've figured out how to edit an existing node in a modal frame, but I haven't been able to figure out how to create new content in one.

Comments

Have you checked into CTools

Have you checked into CTools AutoModal?

Thank you for the reply;

Thank you for the reply; however, I'm wanting to learn a little more about what goes on "under the hood" other than just install another module.

Nevertheless, I've made some progress by creating a custom module and including the following code:

function my_modal_frames_menu() {
  $items['my/modal/%ctools_js/%/%'] = array(
      'title' => 'Fire Modal',
      'page callback' => 'my_modal_callback',
      'page arguments' => array(2,3,4),
  'access arguments' => array('access content'),
  );
  return $items;
}

function my_modal_callback($js = FALSE, $nid, $type) {
   
if(!$js) return "Javascript Required";
   
global $user;
ctools_include('node.pages','node', '');
        ctools_include('ajax');
ctools_include('modal');

$values = array(
  'type' => $type,
  'uid' => $user->uid,
  'status' => 1,
  'comment' => 1,
  'promote' => 0,
);

// Create Entity Object (node object more or less)
$entity = entity_create('node',$values);
$ewrapper = entity_metadata_wrapper('node', $entity);

// Preload entity reference fields
if ($type == "location")
    $ewrapper->field_account->set(intval($nid));
else if ($type == "service" or $type == "computer" or $type == "ticket")
           $ewrapper->field_location->set(intval($nid));

$form_state = array(
  'title' => t('Edit'),
  'ajax' => TRUE,
);

$form_state['build_info']['args'] = array($entity);
$form_type = $type . "_node_form";
$output = ctools_modal_form_wrapper($form_type,$form_state);

  // Fires after the form is exectued
  if (!empty($form_state['executed'])) {
    $output = array();
    // Close the modal
    $output[] = ctools_modal_command_dismiss();
  }

  print ajax_render($output);
}

Everything is working for the most part except all the nodes created this way show they are authored by anonymous users. Can someone give me a pointer on what to fix here?

P.S. The node types that I'm creating have an entity reference field prepopulated with this code (which is working fine). I wanted to explain that so I don't cause any confusion on that bit.