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!
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.
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.
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);
}
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:
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 -
No rush, thanks in advance if someone has solved this!
Hi M N, Did you solve this?
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
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.
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.
I acheived this by updating prevaluesource without making changes to it.
is working on a reply...