Hey,

I have a modal frame module implementation, and on my submit handler I have two options - one to reload the page, the other to redirect to an arbitrary page. Everything works well, however, I'd like the arbitrary page to redirect to the submitted node, and I'm trying to pass that into the javascript file.

Here is my javascript code:

  $('a.automodal:not(.modal-processed)', context).addClass('modal-processed').click(function() {

    function onSubmitCallback(args, statusMessages) {
      if(args && args.reload){
	setTimeout(function() { window.location.reload(01); }, 01);
      }
      if(args && args.redirect){
	window.location = "URL of Submitted Node"
      }
    }

    // Build modal frame options.
    var modalOptions = {
      url: $(this).attr('href'),
      autoResize: true,
      autoFit: true,
      width: 600,
      height: 400,
      onSubmit: onSubmitCallback
    }; 

    // Open the modal frame dialog.
    Drupal.modalFrame.open(modalOptions);

    return false;
  });

I use either one of two submit handlers, one reloads the page, the other redirects the page.

My question is this:

How do I get the node id to pass into JS where it says "URL of Submitted Node"?

Here is my submit handler:

function modalframeTool_form_submit_redirect($form, &$form_state){
  // Ignore preview requests. Close dialog only when user clicks "Save" button.
  if ($form_state['values']['op'] == t('Save')) {  
      modalframe_close_dialog(array('redirect' => TRUE)); 
  }
}

I just need to be able to pass the url to the JS so it properly redirects. Any ideas?

Thanks!

Comments

rc2020’s picture

I made a bit of progress by being able to pass variables form the submit handler directly to the js, but I still can't get the nid set. Here's what I got:

JS:

  function onSubmitCallback(args, statusMessages) {
      if(args && args.reload){
	setTimeout(function() { window.location.reload(01); }, 01);
      }
      if(args && args.redirect){
	window.location = args.path;
      }
    }

Submit handler:


function modalframeTool_form_submit_redirect($form, &$form_state){
  // Ignore preview requests. Close dialog only when user clicks "Save" button.
  if ($form_state['values']['op'] == t('Save')) {
  	$nid = $node->nid;
  	//$path = $form_state['redirect'];
        $path = "/node/$nid"; //this currently also does not work
      modalframe_close_dialog(array('redirect' => TRUE, 'path' => $path)); 
  }
}

I'm so close. I just need the NID of the submitted node to pass and I'm golden. Please chime in with ideas if you have them!

Thanks!
?>

markus_petrux’s picture

Status: Active » Fixed

If this is a node edit/create form, try looking at $form_state['nid'].

See an example in Node Relationships module, file noderelationships.pages.inc, function _noderelationships_child_node_form_submit().

rc2020’s picture

Status: Fixed » Active

Hey Markus,

Thanks for getting back to me. I commented out modalframe_close_dialog() and using devel's dsm function I passed

dsm($form_state);

There is no $form_state['nid']. However, there is a $form_state['values']['nid'] but it's a NULL value - although all CCK values have the info in them. I guess the CCK value is processed before the node is saved to the DB.

Any thoughts?

I'll try out node relationships but that I think that requires me to use a node reference field in order for it to work.

markus_petrux’s picture

Your submit handler is attached to ... ?

Have you tried like this?

  $form['buttons']['submit']['#submit'][] = 'modalframeTool_form_submit_redirect';
rc2020’s picture

Hey Markus,

My submit handler is attached to the node via form_alter()

function modalframeTool_form_alter(&$form, $form_state, $form_id) {
  $modal = $_GET['modal'];
  if($form_id == 'status_node_form' && $modal) {
	$form['#submit'][] = 'modalframeTool_form_submit_redirect';     
  }
}

My submit handler is as such:


function modalframeTool_form_submit_redirect($form, &$form_state){
  // Ignore preview requests. Close dialog only when user clicks "Save" button.
  if ($form_state['values']['op'] == t('Save')) {
  	 // This doesen't work - tried making it work via hook_nodeapi but it's being troublesome....
        $nid = $form['#node']->nid; 
  	$path = "/node/$nid";
     modalframe_close_dialog(array('redirect' => TRUE, 'path' => $path)); 
  }
}

As explained in commented line above I am under the impression that hook_nodeapi can help me here, but I need to implement it in a way that gives me access to the created node object on insert so I can extract the NID and pass it into the string for $path for the modalframe_close_dailog arguments. This doesen't seem to be happening at all - and I don't know why.


function modalframeTool_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
	if (user_access('access content')){
		 switch ($op) {
		 	case 'insert':
		 	$nid = $node->nid;
		 	break;
		 	
		 	case 'presave':
		 	$nid = $node->nid;
		 	break;
		 	
		 }
	}
}

Any thoughts?

markus_petrux’s picture

Have you tried attaching your submit handler as in second question in #4?

rc2020’s picture

It works! Ha!

Thanks so much dude! I had no idea putting it within buttons would make it work like that but it does!!!!

THANK YOU SO MUCH!

markus_petrux’s picture

Status: Active » Fixed

Yeah, the node edit form is quite particular. :)

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.