using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.Web;
using Umbraco.Forms.Core;
using Umbraco.Forms.Core.Models;
namespace MyFormsExtensions
public class FormPrevaluesSourceNode : FieldPreValueSourceType
{
private readonly ILogger _logger;
private readonly IUmbracoContextFactory _UmbracoContextFactory;
//DEFINE ANY CONFIGURATION SETTING HERE
[Umbraco.Forms.Core.Attributes.Setting(name: "Source Node",
Alias = "SourceNodeId",
Description = "Node holding the Options desired.",
View = "Umb.PropertyEditorUi.ContentPicker.Source")]
public string SourceNodeId { get; set; }
public FormPrevaluesSourceNode(
ILogger<FormPrevaluesSourceNode> logger
, IUmbracoContextFactory umbracoContextFactory
)
{
_logger = logger;
_UmbracoContextFactory = umbracoContextFactory;
this.Id = new Guid("0E4D4E2B-56E1-4E86-84E4-9A0A6051B57C"); //MAKE THIS UNIQUE!
this.Name = "Content-defined Form Prevalues Source Node";
this.Description = "Select a node of type 'FormPrevaluesSourceNode'";
this.Group = "Custom";
this.Icon = "icon-science";
}
/// <summary>
/// The main method where the PreValues are defined and returned.
/// </summary>
/// <param name="field"></param>
/// <param name="form"></param>
/// <returns>List of 'Umbraco.Forms.Core.Models.PreValue'</returns>
public override Task<List<PreValue>> GetPreValuesAsync(Field? field, Form? form)
{
List<PreValue> result = new List<PreValue>();
try
{
// Access the Configuration Setting and check that is is valid
if (!string.IsNullOrEmpty(SourceNodeId))
{
var nodeId = 0;
var isValidId = Int32.TryParse(SourceNodeId, out nodeId);
if (isValidId)
{
IPublishedContent iPub;
using (var umbracoContextReference = _UmbracoContextFactory.EnsureUmbracoContext())
{
iPub = umbracoContextReference.UmbracoContext.Content.GetById(nodeId);
}
if (iPub != null)
{
int sort = 0;
//This is using a ModelsBuilder Model to strongly-type the selected node
var preValSourceNode = new Models.FormPrevaluesSourceNode(iPub, null);
foreach (var prevalue in preValSourceNode.PreValues)
{
PreValue pv = new PreValue();
pv.Id = $"{iPub.Id}-{sort}";
pv.Value = prevalue.StoredValue;
pv.Caption = prevalue.DisplayText; //.Caption only available in Forms Versions 8.13.0+, 9.5.0+, & 10.1.0+
pv.SortOrder = sort;
result.Add(pv);
sort++;
}
}
}
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Unable to get options from FormPrevaluesSourceNode #{SourceNodeId}", SourceNodeId);
}
return Task.FromResult(result);
}
/// <summary>
/// This is where any checks for Configuration validity are done.
/// The exceptions will be displayed in the back-office UI to the user.
/// </summary>
/// <returns>List of 'System.Exception'</returns>
public override List<Exception> ValidateSettings()
{
List<Exception> exceptions = new List<Exception>();
if (string.IsNullOrEmpty(SourceNodeId))
{
exceptions.Add(new Exception("'Source Node' setting not filled out"));
}
else
{
var nodeId = 0;
var isValidId = Int32.TryParse(SourceNodeId, out nodeId);
if (isValidId)
{
IPublishedContent iPub;
using (var umbracoContextReference = _UmbracoContextFactory.EnsureUmbracoContext())
{
iPub = umbracoContextReference.UmbracoContext.Content.GetById(nodeId);
}
if (iPub != null && iPub.ContentType.Alias != Models.FormPrevaluesSourceNode.ModelTypeAlias)
{
exceptions.Add(new Exception("'Source Node' needs to be of type 'FormPrevaluesSourceNode'"));
}
}
}
return exceptions;
}
}
}