Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Previous" button not working as expected when using "Show Form Page" action #139

Open
linasw opened this issue May 18, 2023 · 1 comment

Comments

@linasw
Copy link

linasw commented May 18, 2023

Currently the "Previous" navigation step on a button is not working correctly with the "Show Form Page" action. It is opening the previous form page as it is set up in Sitecore Forms. But I would expect so that it goes to the previous page the user was actually on.

With the "normal" Sitecore way, with conditions, all works good.
E.g. there are 4 pages: Page1, Page2, Page3, Page4
Page1 has radio button list, which depending on which one is selected, the "Next" button will go to a different page, either Page3 or Page4 (based on Sitecore's condition). If the user then clicks "Previous" button (which has the "Previous" navigation step selected) on Page3 or Page4, they will go back to Page1.

image
image
image

The issue appears with the "Show Form Page" action. My "Next" button on Page1 has an action "Show Form Page" set up to show the Page3. Once I get on Page3, and press the "Previous" button, it shows me Page2 instead of expected Page1.

image

@linasw
Copy link
Author

linasw commented May 22, 2023

I've got it fixed in a hacky way. I don't think it's ideal, but at least it works now. Basically, when this action is called, I add the necessary info to the formRenderingContext history, so later, when the "Previous" button is pressed, it finds the previous page from the history from the formRenderingContext

using Feature.FormsExtensions.SubmitActions.ShowFormPage;
using Microsoft.Extensions.DependencyInjection;
using Sitecore;
using Sitecore.Data;
using Sitecore.DependencyInjection;
using Sitecore.ExM.Framework.Diagnostics;
using Sitecore.ExperienceForms.Models;
using Sitecore.ExperienceForms.Mvc;
using Sitecore.ExperienceForms.Processing;
using Sitecore.ExperienceForms.Processing.Actions;
using System;
{
    public class CustomShowFormPageAction : SubmitActionBase<ShowFormPageData>
    {
        private readonly ILogger logger;
        public CustomShowFormPageAction(ISubmitActionData submitActionData)
          : this(submitActionData, ServiceLocator.ServiceProvider.GetService<ILogger>())
        {
        }
        public CustomShowFormPageAction(ISubmitActionData submitActionData, ILogger logger)
          : base(submitActionData)
        {
            this.logger = logger;
        }
        protected override bool Execute(ShowFormPageData data, FormSubmitContext formSubmitContext)
        {
            int num;
            if (data.FormPageId.HasValue)
            {
                Guid? formPageId = data.FormPageId;
                Guid empty = Guid.Empty;
                num = formPageId.HasValue ? (formPageId.HasValue ? (formPageId.GetValueOrDefault() == empty ? 1 : 0) : 1) : 0;
            }
            else
                num = 1;
            if (num != 0)
            {
                this.logger.LogWarn("Empty FormPageId");
                return true;
            }
            ShowFormPageContext.FormPage = data.FormPageId;
            //my code starts here
            var formRenderingContext = ServiceLocator.ServiceProvider.GetService<IFormRenderingContext>();
            var nextFormPage = Context.Database.GetItem(ID.Parse(data.FormPageId.Value));
            if (nextFormPage is null)
            {
                this.logger.LogWarn($"Failed to get item of ID {data.FormPageId.Value}");
                return true;
            }
            var navigationData = new NavigationData
            {
                ButtonId = formSubmitContext.ButtonId,
                NavigationType = NavigationType.Page,
                NextPageKey = nextFormPage["Field Key"],
                PageId = formSubmitContext.PageId.ToString(),
                Step = 0
            };
            formRenderingContext.StoreLatestNavigationData(navigationData);
            return true;
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant