-
Notifications
You must be signed in to change notification settings - Fork 0
Multi Node Tree Picker
Multi Node Tree Pickers are one of the most popular and powerful property types available in Umbraco. In Concrete we will look at how each Mutli Node Tree Picker has been defined in Umbraco and create properties accordingly. Wherever possible we will work out the correct type for the property. There are 4 possibly ways your Multi Node Tree Picker will be defined in the your Concrete Model.
The properties created will always be lazy loaded so the objects referenced aren't created unless the property is accessed.
Definition of MNTP:
- Allowed Items of Type: undefined or Multiple Content types
- Maximum number of items: 1
This will create a property of type IPublishedContent, as we cannot determine the Concrete type required but we know there can only ever be one of them. If a value hasn't been specified for the property this will return null.
private IPublishedContent _linkedPage = null;
public IPublishedContent LinkedPage
{
get
{
if (_linkedPage == null)
{
int? contentId = Content.GetPropertyValue<int?>("linkedPage");
if (contentId.HasValue)
{
_linkedPage = UmbracoContext.Current.ContentCache.GetById(contentId.Value);
}
}
return _linkedPage;
}
}
- Allowed Items of Type: undefined or Multiple Content types
- Maximum number of items: >1 or not specified
This will create a property of type List<IPublishedContent> as we cannot determine the Concrete type and there may be more than one node selected. This property will default to an empty list if nothing is selected so you will never get a null reference exception.
private List<IPublishedContent> _multipleNodes = null;
public List<IPublishedContent> MultipleNodes
{
get
{
if (_multipleNodes == null)
{
_multipleNodes = new List<IPublishedContent>();
string val = Content.GetPropertyValue<string>("multipleNodes");
if (!string.IsNullOrEmpty(val))
{
string[] contentIds = val.Split(',');
foreach (string id in contentIds)
{
_multipleNodes.Add(UmbracoContext.Current.ContentCache.GetById(int.Parse(id)));
}
}
}
return _multipleNodes;
}
}
- Allowed Items of Type: Single Content type
- Maximum number of items: 1
If you have specified a single Content Type and a maximum of 1 then we create a property of the allowed Type.
private BlogAuthor _author = null;
public BlogAuthor Author
{
get
{
if (_author == null)
{
int? contentId = Content.GetPropertyValue<int?>("author");
if (contentId.HasValue)
{
_author = new BlogAuthor(contentId.Value);
}
}
return _author;
}
}
- Allowed Items of Type: Single Content type
- Maximum number of items: >1 or not specified
If you have specified a single Allowed Content type and a maximum of greater than one then we create a List of the Concrete type:
private List<BlogAuthor> _blogAuthors = null;
public List<BlogAuthor> BlogAuthors
{
get
{
if (_blogAuthors == null)
{
_blogAuthors = new List<BlogAuthor>();
string val = Content.GetPropertyValue<string>("blogAuthors");
if (!string.IsNullOrEmpty(val))
{
string[] contentIds = val.Split(',');
foreach (string id in contentIds)
{
_blogAuthors.Add(new BlogAuthor(int.Parse(id)));
}
}
}
return _blogAuthors;
}
}