Skip to content

Instantly share code, notes, and snippets.

@joseftw
Last active April 11, 2016 18:43
Show Gist options
  • Save joseftw/780043544fd264f0596f1ddaf2b10901 to your computer and use it in GitHub Desktop.
Save joseftw/780043544fd264f0596f1ddaf2b10901 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using EPiServer.DataAbstraction;
using EPiServer.DataAbstraction.RuntimeModel;
using EPiServer.DataAnnotations;
using EPiServer.Framework;
using EPiServer.Framework.Initialization;
using EPiServer.ServiceLocation;
using Feature.CoolPage;
using Feature.MediaPage;
namespace InjectedAllowedTypes
{
[InitializableModule]
[ModuleDependency(typeof(EPiServer.Cms.Shell.InitializableModule))]
public class InitializationModule : IInitializableModule
{
private Injected<IAvailableModelSettingsRepository> AvailableModelSettingsRepository { get; set; }
public void Initialize(InitializationEngine context)
{
var predefinedSettings = this.AvailableModelSettingsRepository.Service.ListRuntimeSettings();
var customSettings = InjectedAvailableModelSettings.GetCustomAvailableModelSettings();
foreach (var customSetting in customSettings)
{
var predefinedSetting = predefinedSettings.FirstOrDefault(x => x.Key == customSetting.Key);
var mergedSetting = MergeSettings(customSetting.Value, predefinedSetting.Value);
//mergedSetting has the correct value here when it gets passed in to RegisterSetting
this.AvailableModelSettingsRepository.Service.RegisterSetting(customSetting.Key, mergedSetting);
}
//But when fetching all settings, it comes back with incorrect value..
var updatedValues = this.AvailableModelSettingsRepository.Service.ListRuntimeSettings();
}
public void Uninitialize(InitializationEngine context)
{
//Add uninitialization logic
}
private ContentTypeAvailableModelSetting MergeSettings(ContentTypeAvailableModelSetting customSetting, ContentTypeAvailableModelSetting runtimeSetting)
{
var mergedSetting = new ContentTypeAvailableModelSetting();
if (runtimeSetting == null)
{
return customSetting;
}
mergedSetting.Excluded = new HashSet<Type>(customSetting.Excluded.Concat(runtimeSetting.Excluded).Distinct());
mergedSetting.Included = new HashSet<Type>(customSetting.Included.Concat(runtimeSetting.Included).Distinct());
mergedSetting.IncludedOn = new HashSet<Type>(customSetting.IncludedOn.Concat(runtimeSetting.IncludedOn).Distinct());
mergedSetting.Availability = customSetting.Availability;
return mergedSetting;
}
}
}
using System;
using System.Collections.Generic;
using EPiServer.DataAbstraction;
using EPiServer.DataAbstraction.RuntimeModel;
using Feature.CoolPage;
using Feature.MediaPage;
namespace InjectedAllowedTypes
{
public static class InjectedAvailableModelSettings
{
public static Dictionary<Type, ContentTypeAvailableModelSetting> GetCustomAvailableModelSettings()
{
var mappedModelSettings = MappedModelSettings();
return mappedModelSettings;
}
private static Dictionary<Type, ContentTypeAvailableModelSetting> MappedModelSettings()
{
return new Dictionary<Type, ContentTypeAvailableModelSetting>
{
{
typeof(MediaPage) , new ContentTypeAvailableModelSetting
{
Availability = Availability.Specific,
Included = new HashSet<Type> {typeof(CoolPage) }
}
}
};
}
}
}
using EPiServer.Core;
using EPiServer.DataAbstraction;
using EPiServer.DataAnnotations;
namespace Feature.MediaPage
{
[ContentType(DisplayName = "Media Page", GUID = "5b393025-d856-4392-aa99-61f1a468ea79", Description = "")]
[AvailableContentTypes(Availability.Specific, Include = new [] {typeof(MediaPage) })]
public class MediaPage : PageData
{
[AllowedTypes(AllowedTypes = new [] {typeof(VideoBlock)})]
public virtual ContentArea ContentArea { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment