Try our conversational search powered by Generative AI!

AvailableContentTypes settings inside an InitializationModule : yay or nay ?

Vote:
 

I would like to group all my AvailableContentTypes settings inside an initializationModule so that I can find all the settings in one place. Is there a performance issue with that kind of approach ? We are talking about roughly ~30 PageType with ~5 types of pages as possible children & Availability.Specific for every settings.

#230030
Oct 28, 2020 17:13
Vote:
 

This is how I do it.  I highly recommend this approach because it keeps everything in one spot with this init module having a single responsibility.  I don't think you will cause any performance impacts because my guess is that Epi does the same thing with an init module by scanning the code, finding usages of the attribute, and updating the AvailableTypesRepository.

Here's an example for references:

    [InitializableModule]
    [ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
    public class PageRestrictionInitializationModel : IInitializableModule
    {
        private IContentTypeRepository _contentTypeRepository;
        private IAvailableSettingsRepository _availableSettingsRepository;
        private ILogger _log;

        public void Initialize(InitializationEngine context)
        {
            _contentTypeRepository = ServiceLocator.Current.GetInstance<IContentTypeRepository>();
            _availableSettingsRepository = ServiceLocator.Current.GetInstance<IAvailableSettingsRepository>();
            _log = LogManager.GetLogger();

            // pages that should be leaves and not allow anything
            DisallowAll<EventPage>();
            DisallowAll<NewsPage>();
            DisallowAll<BlogPage>();
            DisallowAll<ProductPage>();

            // Home Page
            SetPageRestriction<HomePage>(new List<Type>
            {
                typeof(NewsEventsLandingPage),
                typeof(BlogLandingPage),
                typeof(GeneralPage),
                typeof(ProductCategoryPage)
            });

            SetPageRestriction<NewsEventsLandingPage>(new List<Type>
            {
                typeof(EventPage),
                typeof(NewsPage)
            });

            // ... etc ...
        }

        private void DisallowAll<T>()
        {
            var page = _contentTypeRepository.Load(typeof(T));

            var setting = new AvailableSetting
            {
                Availability = Availability.None
            };

            _availableSettingsRepository.RegisterSetting(page, setting);
        }

        public void Uninitialize(InitializationEngine context)
        {
        }

        private void SetPageRestriction<T>(IEnumerable<Type> pageTypes)
        {
            var page = _contentTypeRepository.Load(typeof(T));

            var setting = new AvailableSetting
            {
                Availability = Availability.Specific
            };

            foreach (var pageType in pageTypes)
            {
                var contentType = _contentTypeRepository.Load(pageType);
                setting.AllowedContentTypeNames.Add(contentType.Name);
            }

            _availableSettingsRepository.RegisterSetting(page, setting);

            _log.Log(Level.Debug, $"{page.Name} restriction added.");
        }
    }
#230034
Oct 28, 2020 18:11
- Oct 28, 2020 18:16
My code is very similar to yours .. I am happy to see that we are on the same page 😄 - what happens if we don't disallow the leaves ?
Dylan McCurry - Oct 28, 2020 18:35
If I remember correctly- these pages will be able to insert anything beneath of them. I explicitly disallow them to just ensure that all page types are accommodated.
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.