Override default content save button

Topics: Administration, Customizing Orchard, General
Dec 24, 2012 at 2:29 PM

Hi there,

Is it possible to override default content save button, so that after the save operation is finished I do something else.

I have a problem that after the save, I have to redirect to some url depending on the saved object, but I'm unable to do so.

 

Thank you...

Dec 24, 2012 at 3:37 PM
Edited Dec 24, 2012 at 3:37 PM

I don't think so, but there has been talk about making the AdminController of the Content module extensible to allow this scenario. There's even a work item for it: http://orchard.codeplex.com/workitem/18998

Please vote it up.

Dec 25, 2012 at 6:40 AM

Does anyone have any solution how to implement this. I'd probably hide that button and put one with my functionality. However, I don't know how to hide it at first, and secondly how to do that after save process is finished.

Dec 25, 2012 at 9:03 AM

There are several things you can do.

  1. Copy the AdminController from the Contents module to your own module and setup the routes so that the Admin/Contents/Edit/{id} url points to your copied controller and edit action. From there you will have full control over what happens.
  2. This is more nasty, but it works: Add your custom buttons to the Actions zone and use CSS to hide the default buttons. Either use AJAX to submit the form to the default action and use the callback to redirect to any url you want.
  3. Modify the source code (not recommended unless you fork the source and implement a generic / reusable solution).

 

Dec 25, 2012 at 9:04 AM

I assume I'll go with the first solution, and hopefully solve issue. Thanks for response.

Dec 25, 2012 at 9:28 AM

My route is not Admin/Contents but Admin/Settings, does it changes thing?

Dec 25, 2012 at 9:31 AM

Or maybe is there a solution to force push of new data to DB ( force save ) from my handler or driver?

Dec 25, 2012 at 10:59 AM

I'm trying to implement my own save method, but TryUpdateModel returns true and the model is not updated. What can be the problem?

Dec 25, 2012 at 12:54 PM

You could try implementing your own controller to handle content management (using the AdminController mentioned earlier as an example) and implement a content handler that sets the EditorRouteValues metadata to point to your custom AdminController (see ContentsHandler of the Contents module for an example; or copy it to your own module and tweak the values o they match your own controller). The only tricky thing with this is that you have to be lucky enough that your content handler gets executed last when the GetItemMetadata method is invoked, but I would give it a try.

Dec 25, 2012 at 12:56 PM
Edited Dec 25, 2012 at 12:56 PM

Correction: you're not dependent on luck; the ContentsHandler class performs a check to see if the EditorRouteValues has been set; if it has been set, it will not override it. So you're in luck ;)

Dec 25, 2012 at 12:59 PM

After some more research I found the solution, though it is a dirty one.

 

OnUpdated<T>((ctx, part) =>
	{
		var fi = TransactionManager.GetType().GetField("_scope", BindingFlags.NonPublic | BindingFlags.Instance);
		var scope = (TransactionScope)(fi.GetValue(TransactionManager));
		scope.Complete();
		scope.Dispose();
		fi.SetValue(TransactionManager, null);

                HttpContext.Current.Response.Redirect(req.RequestUrl);
	});