Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • M N 125 posts 212 karma points
    Sep 26, 2019 @ 18:02
    M N
    0

    Dynamic Custom PreValue Source - Caching Issue?

    Hey all,

    Was wondering if anyone has had any success creating dynamic PreValue sources? Meaning, they are not cached and are updated in real-time after every page refresh.

    We have a client that offers Classes/Courses, so there are nodes of Classes with Dates, Times, etc. and we want to populate a dropdown on a form so as the client adds/remove classes the dropdown auto updates.

    Specifically, I have this custom prevalue that will fetch child nodes:

    public class GetValuesFromChildNodes : FieldPreValueSourceType
        {
            [Attributes.Setting("NodeId", description = "The node to get children from", view = "TextField")]
            public string NodeId { get; set; }
    
            public GetValuesFromChildNodes()
            {
                this.Id = new Guid("FAC1238E-CDF7-4A93-B27C-6E97B9B71A2D");
                this.Name = "Get Values From Child Nodes";
                this.Description = "Get all child nodes for specific node id";
            }
    
            public override List<Core.PreValue> GetPreValues(Field field, Form form)
            {
                List<Core.PreValue> vals = new List<Core.PreValue>();
                IPublishedContent node = MyUmbracoHelper.GetNodeById(NodeId);
    
                foreach (IPublishedContent child in node.Children)
                {
                    Umbraco.Forms.Core.PreValue v = new Umbraco.Forms.Core.PreValue { Id = child.Name, Value = child.Name };
    
                    vals.Add(v);
                }
    
                return vals;
            }
    
            public override List<Exception> ValidateSettings()
            {
                List<Exception> exs = new List<Exception>();
                // TODO
                return exs;
            }
        }
    

    Pretty simple, just lists published child nodes. The only way to force this to update on the server is to go back into the PreValues and refresh the values. Then refresh the page with the form and the dropdown updates. Republish site cache doesn't work either, so I'm wondering if -

    • There is a way to dynamically force the cache to update (I could do this on a Node Save event I guess if I absolutely have to)
    • There is a way to just disable the Umbraco Forms caching.

    No rush, thanks in advance if someone has solved this!

  • Andrey Karandashov 23 posts 215 karma points c-trib
    Nov 15, 2019 @ 15:14
    Andrey Karandashov
    0

    Hi M N, Did you solve this?

  • M N 125 posts 212 karma points
    Nov 15, 2019 @ 22:59
    M N
    0

    Yes, but I ended up querying the nodes in the CSHTML which was less than ideal.

    Created a custom Field Type for it, but never really tested it thoroughly since we ended up taking a different approach to the original business problem we had at the time.

    Happy to share it with you if you like!

    -Marc

  • Stephen Brewer 9 posts 31 karma points
    Mar 18, 2020 @ 10:12
    Stephen Brewer
    0

    After much head scratching i found a way around the issue of cached prevalues. In our example we need to call the values from an external source and wanted full control of the data.

    using Umbraco.Forms.Web.Controllers;
    
    protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            UmbracoFormsController.FormPrePopulate += UmbracoFormsController_FormPrePopulate;
        }
    
        private void UmbracoFormsController_FormPrePopulate(object sender, global::Umbraco.Forms.Core.FormEventArgs e)
        {           
            var brochureMultipleChoiceField = new BrochureMultipleChoiceField();
    
            foreach (var field in e.Form.AllFields)
            {
                if (field.FieldTypeId == brochureMultipleChoiceField.Id)
                {
                    field.PreValues = ServiceLocator.Current.GetInstance<IStaticDataService>().GetBrochures();
                }
            }
        }
    

    This approach gives you full access to the form and all fields prior to the data being pushed to the cshtml and hence you can manipulate any data you like.

  • Shaheed ulHaq 1 post 71 karma points notactivated
    Jun 20, 2023 @ 14:59
    Shaheed ulHaq
    0

    I acheived this by updating prevaluesource without making changes to it.

                // Updating the prevaluesource refreshes the cache
                var tripsPrevalueSourceType = _fieldPreValueSourceTypes.SingleOrDefault(x => x.Id.Equals(TripsPrevalueSource.PrevalueSourceId)); // TripsPrevalueSource.PrevalueSourceId is the constant that I used while creating the custom prevaluesource
    
                if (tripsPrevalueSourceType != null)
                {
                    var prevalueSource = _prevalueSourceService.Get(tripsPrevalueSourceType.FieldPreValueSource.Id);
                    _prevalueSourceService.Update(prevalueSource);
                }
    
Please Sign in or register to post replies

Write your reply to:

Draft