Last active
April 18, 2016 18:21
-
-
Save PNergard/e931dc956492967d0cc0fe8c2c1b8c11 to your computer and use it in GitHub Desktop.
Singleton page- and blocktypes. A attribute and initializationmodule that limits page and block types to only be created once. http://world.episerver.com/blogs/Per-Nergard/Dates/2016/4/limit-block-and-page-types-to-be-created-only-once-updated/
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
namespace AlloyDemoKit.Business.DataAnnotations | |
{ | |
public class Singleton : Attribute | |
{ | |
} | |
} |
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
using System; | |
using EPiServer; | |
using EPiServer.Core; | |
using EPiServer.DataAbstraction; | |
using EPiServer.Framework; | |
using EPiServer.Framework.Initialization; | |
using EPiServer.ServiceLocation; | |
using AlloyDemoKit.Business.DataAnnotations; | |
namespace AlloyDemoKit.Business.Initialization.CreateTypeOnlyOnce | |
{ | |
[InitializableModule] | |
public class SingletonTypeInitialzableModule : IInitializableModule | |
{ | |
public void Initialize(InitializationEngine context) | |
{ | |
DataFactory.Instance.CreatedContent += Instance_CreatedContent; | |
DataFactory.Instance.DeletedContent += Instance_DeletedContent; | |
} | |
public void Preload(string[] parameters) { } | |
public void Uninitialize(InitializationEngine context) | |
{ | |
DataFactory.Instance.CreatedContent -= Instance_CreatedContent; ; | |
DataFactory.Instance.DeletedContent -= Instance_DeletedContent; | |
} | |
void Instance_CreatedContent(object sender, ContentEventArgs e) | |
{ | |
var repository = ServiceLocator.Current.GetInstance<IContentTypeRepository>(); | |
if (repository != null) | |
{ | |
var contentType = repository.Load(e.Content.ContentTypeID); | |
Attribute[] attributes = Attribute.GetCustomAttributes(contentType.ModelType); | |
foreach (var attr in attributes) | |
{ | |
if (attr is Singleton) | |
{ | |
var writableContentType = (ContentType)contentType.CreateWritableClone(); | |
writableContentType.IsAvailable = false; | |
repository.Save(writableContentType); | |
} | |
} | |
} | |
} | |
void Instance_DeletedContent(object sender, DeleteContentEventArgs e) | |
{ | |
var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>(); | |
var contentTyperepository = ServiceLocator.Current.GetInstance<IContentTypeRepository>(); | |
foreach (ContentReference cr in e.DeletedDescendents) | |
{ | |
var content = contentRepository.Get<IContent>(cr); | |
if (content != null) | |
{ | |
var contentType = contentTyperepository.Load(content.ContentTypeID); | |
System.Attribute[] attributes = System.Attribute.GetCustomAttributes(contentType.ModelType); | |
foreach (var attr in attributes) | |
{ | |
if (attr is Singleton) | |
{ | |
var writableContentType = (ContentType)contentType.CreateWritableClone(); | |
writableContentType.IsAvailable = true; | |
contentTyperepository.Save(writableContentType); | |
} | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment