Skip to content

Instantly share code, notes, and snippets.

@davidknipe
Created May 6, 2020 09:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidknipe/14243810e1cb23096cee5b3c8d998059 to your computer and use it in GitHub Desktop.
Save davidknipe/14243810e1cb23096cee5b3c8d998059 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using EPiServer.Core;
using EPiServer.Framework;
using EPiServer.Framework.Initialization;
using EPiServer.Shell.ObjectEditing;
namespace Foundation.Features
{
public class ReadonlyBlockMetadataExtender : IMetadataExtender
{
public void ModifyMetadata(ExtendedMetadata metadata, IEnumerable<Attribute> attributes)
{
// Check it's a block
if (metadata.Model is BlockData)
{
// Logic to decide if the user should be able to edit or not lives here
foreach (var modelMetadata in metadata.Properties)
{
var property = (ExtendedMetadata)modelMetadata;
property.IsReadOnly = true;
}
}
}
}
[InitializableModule]
[ModuleDependency(typeof(EPiServer.Cms.Shell.InitializableModule))]
public class SiteMetadataExtenderInitialization : IInitializableModule
{
public void Initialize(InitializationEngine context)
{
if (context.HostType == HostType.WebApplication)
{
var registry = context.Locate.Advanced.GetInstance<MetadataHandlerRegistry>();
registry.RegisterMetadataHandler(typeof(ContentData), new ReadonlyBlockMetadataExtender());
}
}
public void Uninitialize(InitializationEngine context) { }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment