Created
July 19, 2012 21:44
-
-
Save SitefinityGuru/3147057 to your computer and use it in GitHub Desktop.
Sitefinity Dynamic Content Selector Field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// A simple field for displaying related dynamic content items in a drop-down menu. | |
/// Requirements: | |
/// * the name of the field must exactly match the name of the content type being displayed | |
/// * the content type must be within the same module as the content type using this field | |
/// </summary> | |
public class DynamicContentSelectorField : ChoiceField | |
{ | |
/// <summary> | |
/// Defaults the rendering of the list as a drop-down menu | |
/// </summary> | |
/// <value> | |
/// The option for which to render choices. | |
/// </value> | |
public override RenderChoicesAs RenderChoicesAs | |
{ | |
get { return RenderChoicesAs.DropDown; } | |
set { base.RenderChoicesAs = value; } | |
} | |
/// <summary> | |
/// Defaults the script descriptor to be ChoiceField so we don't have to reimplement the JavaScript for the field. | |
/// </summary> | |
/// <value> | |
/// The type of the script descriptor. | |
/// </value> | |
protected override string ScriptDescriptorType | |
{ | |
get { return typeof(ChoiceField).FullName; } | |
} | |
/// <summary> | |
/// Default the type of the resource assembly to ChoiceField so we don't have to implement a new resource file | |
/// </summary> | |
protected override Type ResourcesAssemblyInfo | |
{ | |
get { return typeof(ChoiceField); } | |
} | |
/// <summary> | |
/// Configures the specified definition. | |
/// </summary> | |
/// <param name="definition">The definition.</param> | |
public override void Configure(IFieldDefinition definition) | |
{ | |
// retrieve Control Definition Name to parse dynamic type of the module | |
var controlDefinitionName = definition.ControlDefinitionName.Split('.'); | |
if (controlDefinitionName == null || controlDefinitionName.Length == 0) | |
throw new NullReferenceException(string.Concat("Control Definition is empty or not found: ", definition.ControlDefinitionName)); | |
// re-join array minus last item to get full dynamic module type | |
var moduleType = string.Join(".", controlDefinitionName.Take(controlDefinitionName.Length - 1)); | |
// retrieve fieldname from definition to get the specified dynamic type | |
var fieldName = definition.FieldName; | |
if (string.IsNullOrEmpty(fieldName)) | |
throw new NullReferenceException("Field name cannot be empty."); | |
// resolve the type, ensure it exists | |
Type contentType; | |
var dynamicModuleManager = DynamicModuleManager.GetManager(); | |
moduleType = string.Concat(moduleType, ".", fieldName); | |
try | |
{ | |
contentType = TypeResolutionService.ResolveType(moduleType); | |
} | |
catch (Exception ex) | |
{ | |
throw new TypeLoadException(string.Concat(moduleType, " dynamic type not found.")); | |
} | |
// retrieve published items and add to choice list | |
var myCollection = dynamicModuleManager.GetDataItems(contentType).Where(g => g.Status == ContentLifecycleStatus.Live); | |
foreach (var contentItem in myCollection) | |
{ | |
var item = new ChoiceItem(); | |
item.Text = contentItem.GetValue<string>("Name"); | |
item.Value = contentItem.Id.ToString(); | |
this.Choices.Add(item); | |
} | |
base.Configure(definition); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// GET: /Store/Browse/Genre | |
public ActionResult Browse(string Genre) | |
{ | |
// get the matching genre | |
var genre = dynamicModuleManager.GetDataItems(StoreController.GenreType).Where(g => g.GetValue<string>("Name") == Genre && g.Status == ContentLifecycleStatus.Live).FirstOrDefault(); | |
// retrieve album list | |
var albumList = new List<Album>(); | |
var albums = dynamicModuleManager.GetDataItems(StoreController.AlbumType).Where(a => a.GetValue<Guid>("Genre") == genre.Id && a.Status == ContentLifecycleStatus.Live); | |
foreach (var album in albums) | |
albumList.Add(new Album(album)); | |
// store genre name | |
ViewBag.Genre = Genre; | |
return View(albumList); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment