100% found this document useful (1 vote)
221 views

Node Pre Values Node Factory

This document defines a NodePrevaluesNodeFactory class that is used to retrieve published document nodes from Umbraco to use as prevalues for a form field. The class has properties to configure the root node, whether to use the current page as root, and document type. The GetPreValues method retrieves the child nodes of the configured root matching the type and returns them as prevalues.

Uploaded by

api-34526575
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
221 views

Node Pre Values Node Factory

This document defines a NodePrevaluesNodeFactory class that is used to retrieve published document nodes from Umbraco to use as prevalues for a form field. The class has properties to configure the root node, whether to use the current page as root, and document type. The GetPreValues method retrieves the child nodes of the configured root matching the type and returns them as prevalues.

Uploaded by

api-34526575
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using Umbraco.Forms.Data;
using umbraco.presentation.nodeFactory;
using umbraco.cms.businesslogic.web;
using Umbraco.Forms.Core;

namespace ContourExtensions {
public class NodePrevaluesNodeFactory : FieldPreValueSourceType {

public NodePrevaluesNodeFactory()
{
this.Id = new Guid("4F711F20-D5A6-4A2E-B678-4FBFFB3CDB2F");
this.Name = "Umbraco Documents (published)";
this.Description = "Uses published nodes from a specific source as
prevalues";
}

[Umbraco.Forms.Core.Attributes.Setting("Root node", description = "Source


to fetch nodes from", control =
"Umbraco.Forms.Core.FieldSetting.Pickers.ContentWithXpath")]
public string RootNode { get; set; }

[Umbraco.Forms.Core.Attributes.Setting("Use current page as root",


description = "Does not work in preview mode", control =
"Umbraco.Forms.Core.FieldSetting.Checkbox")]
public string UseCurrentPage { get; set; }

[Umbraco.Forms.Core.Attributes.Setting("Document type", description = "The


type of nodes you would like to use", control =
"Umbraco.Forms.Core.FieldSetting.Pickers.DocumentType")]
public string DocType { get; set; }

public override List<PreValue> GetPreValues(Field field) {


List<PreValue> retval = new List<PreValue>();
Node root = null;
int _root = 0;

if (DocType != string.Empty)
{
int dtid = 0;

if (int.TryParse(DocType, out dtid) && dtid > 0)


{
DocumentType dt = new DocumentType(dtid);
DocType = dt.Alias;
}
}

if (UseCurrentPage == true.ToString() &&


umbraco.presentation.UmbracoContext.Current != null &&
umbraco.presentation.UmbracoContext.Current.PageId.HasValue &&
umbraco.presentation.UmbracoContext.Current.PageId.Value > 0)
root = new
Node(umbraco.presentation.UmbracoContext.Current.PageId.Value);
else
{
if (int.TryParse(RootNode, out _root) && _root > 0)
root = new Node(_root);
else
{
if (umbraco.presentation.UmbracoContext.Current != null &&
umbraco.presentation.UmbracoContext.Current.PageId.HasValue &&
umbraco.presentation.UmbracoContext.Current.PageId.Value > 0)
{
string transval = XpathExecutionHelper.GetResult(RootNode);

if (int.TryParse(transval, out _root) && _root > 0)


root = new Node(_root);
}
}

if (root != null)
{
foreach (Node n in root.Children)
{
if (n.NodeTypeAlias == DocType || DocType == string.Empty)
{
PreValue pv = new PreValue();
pv.Id = n.Id;
pv.Value = n.Name;
retval.Add(pv);
}
}
}
return retval;
}

public override List<Exception> ValidateSettings() {


return new List<Exception>();
}
}
}

You might also like