-
-
Save PetersonDave/5096452 to your computer and use it in GitHub Desktop.
/* | |
Extends: Sitecore.Form.Core.Pipelines.FormSubmit | |
forms.config needs to be updated to: | |
<successAction> | |
<processor type="MyCustom.Pipelines.SuccessRedirect, MyLibrary"/> | |
<processor type="Sitecore.Form.Core.Pipelines.FormSubmit.FormatSuccessMessage, Sitecore.Forms.Core"/> | |
</successAction> | |
*/ | |
public class SuccessRedirect : ClientPipelineArgs | |
{ | |
public void Process(SubmitSuccessArgs args) | |
{ | |
Assert.IsNotNull(args, "args"); | |
if (args.Form == null) | |
{ | |
Uri uri = null; | |
if (Uri.TryCreate(args.Result, UriKind.RelativeOrAbsolute, out uri)) | |
{ | |
bool isUriValid = uri != null && !string.IsNullOrEmpty(uri.PathAndQuery); | |
if (isUriValid) | |
{ | |
args.AbortPipeline(); | |
WebUtil.Redirect(args.Result, false); | |
} | |
} | |
} | |
else | |
{ | |
if (args.Form.SuccessRedirect) | |
{ | |
var url = GetRedirectUrl(args); | |
if (!string.IsNullOrEmpty(url)) | |
{ | |
args.AbortPipeline(); | |
WebUtil.Redirect(url, false); | |
} | |
} | |
} | |
} | |
private string GetRedirectUrl(SubmitSuccessArgs args) | |
{ | |
string url = string.Empty; | |
var path = "/sitecore/content" + args.Form.SuccessPage.Url; // don't love the hard-coded path here. Other ideas? | |
if (!string.IsNullOrEmpty(path)) | |
{ | |
var item = Sitecore.Context.Database.Items[path]; | |
if (item != null) | |
{ | |
url = LinkManager.GetItemUrl(item); | |
} | |
} | |
return url; | |
} | |
} |
@scjunkie I previously tried the code above, but got a path which was invalid. This is due to a specific configuration in a multi-site setup:
WFFM Form Page: sitecore/content/my-site/home/form
WFFM Form Success Page: sitecore/content/my-site/home/form/success
WFFM Form: /sitecore/content/my-site/forms/my-form
For the configuration above, the values map to:
Sitecore.Context.Site.RootPath: sitecore/content/my-site
args.Form.SuccessPage.Url: /my-site/home/form/success
Keep in mind this issue is specific to my configuration. While I get an invalid URL, others who have a different configuration will not get an invalid URL. The impact may be a URL, which works, but has the site home node in its URL.
Yes -- if the rootPath attribute on your site node has a value different than /sitecore/content, then using Sitecore.Context.Site.RootPath won't work.
How about putting it in a patch include configuration file instead?
That's a good idea. I like it!
Hi @PetersonDave, Does this mean this is the reason for the reload of the form page that occurs on submit before actually forwarding the request to the success page?
This should remove the hard-coded path part on line 48:
var path = string.Concat(Sitecore.Context.Site.RootPath, args.Form.SuccessPage.Url);