Skip to content

Instantly share code, notes, and snippets.

@justinspradlin
Created November 14, 2013 19:07
Show Gist options
  • Save justinspradlin/7472514 to your computer and use it in GitHub Desktop.
Save justinspradlin/7472514 to your computer and use it in GitHub Desktop.
Example Umbraco Macro Parameter editor. This editor allows you to pick a descendant node based on a specific document type alias. This particular parameter editor supported picking a descendant node to feed a macro to render a playlist. The document type Question has a rich text editor property bodyText. The content editors can create children "…
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using umbraco.interfaces;
using Umbraco.Core;
using Umbraco.Web;
namespace Customer.MacroParameters
{
public abstract class DescendantPickerBase : DropDownList, IMacroGuiRendering
{
protected abstract IEnumerable<string> AllowedDocumentTypes { get; }
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
int documentId;
if (int.TryParse(UmbracoContext.Current.HttpContext.Request.QueryString["umbpageid"], out documentId))
{
// Convert the list of allowed document types to lower case
var lowerCaseAllowedAliases = AllowedDocumentTypes.Select(
alias => string.IsNullOrEmpty(alias) ? alias : alias.ToLowerInvariant());
// Get all descendants so we can filter by the allowed node types
// Add the descendants that are in the white listed aliases to the drop down
ApplicationContext.Current.Services.ContentService.GetDescendants(documentId)
.Where(content => lowerCaseAllowedAliases.Contains(content.ContentType.Alias.ToLowerInvariant()))
.OrderBy(content => content.Name)
.ToList()
.ForEach(content => Items.Add(new ListItem(content.Name, content.Id.ToString())));
}
}
#region IMacroGuiRendering Implementation
public bool ShowCaption
{
get { return true; }
}
public string Value
{
get
{
return this.SelectedValue;
}
set
{
this.SelectedValue = value;
}
}
#endregion
}
}
using System.Collections.Generic;
namespace Customer.MacroParameters
{
public class DescendantPlaylistPicker : DescendantPickerBase
{
private readonly IEnumerable<string> allowedDocumentTypes = new List<string> { "playlist" };
protected override IEnumerable<string> AllowedDocumentTypes
{
get { return allowedDocumentTypes; }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment