Last active
December 6, 2021 13:03
-
-
Save ronaldbarendse/6eda1a0bfee87bdd714409886ecf4219 to your computer and use it in GitHub Desktop.
Umbraco - create document type with all data editors/types to test readonly editors
This file contains hidden or 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.Linq; | |
using System.Web.Http.Filters; | |
using Umbraco.Core.Composing; | |
using Umbraco.Core.Models; | |
using Umbraco.Core.PropertyEditors; | |
using Umbraco.Core.Services; | |
using Umbraco.Web.Editors; | |
using Umbraco.Web.Models.ContentEditing; | |
namespace Umbraco.Cms.Web.UI | |
{ | |
public class DataEditorComposer : ComponentComposer<DataEditorComponent>, IUserComposer | |
{ } | |
public class DataEditorComponent : IComponent | |
{ | |
private readonly IContentTypeService _contentTypeService; | |
private readonly PropertyEditorCollection _propertyEditors; | |
private readonly IDataTypeService _dataTypeService; | |
public DataEditorComponent(IContentTypeService contentTypeService, PropertyEditorCollection propertyEditors, IDataTypeService dataTypeService) | |
{ | |
_contentTypeService = contentTypeService; | |
_propertyEditors = propertyEditors; | |
_dataTypeService = dataTypeService; | |
} | |
public void Initialize() | |
{ | |
EditorModelEventManager.SendingContentModel += EditorModelEventManager_SendingContentModel; | |
var contentType = _contentTypeService.Get("dataEditorTest"); | |
if (contentType != null) | |
return; | |
// Ensure data types for all editors | |
foreach (var propertyEditor in _propertyEditors) | |
{ | |
var dataTypes = _dataTypeService.GetByEditorAlias(propertyEditor.Alias); | |
if (dataTypes.Any() == false) | |
{ | |
var dataType = new DataType(propertyEditor) | |
{ | |
Name = propertyEditor.Name + " (auto-created)" | |
}; | |
_dataTypeService.Save(dataType); | |
} | |
} | |
// Create content type with all data types | |
contentType = new ContentType(-1) | |
{ | |
Name = "Data Editor test", | |
Alias = "dataEditorTest", | |
AllowedAsRoot = true | |
}; | |
var systemDataTypeIds = new[] | |
{ | |
Core.Constants.DataTypes.DefaultContentListView, | |
Core.Constants.DataTypes.DefaultMediaListView, | |
Core.Constants.DataTypes.DefaultMembersListView | |
}; | |
foreach (var dataType in _dataTypeService.GetAll()) | |
{ | |
if (systemDataTypeIds.Contains(dataType.Id)) | |
{ | |
continue; | |
} | |
var propertyType = new PropertyType(dataType) | |
{ | |
Name = dataType.Name, | |
Alias = dataType.EditorAlias + "_" + Guid.NewGuid() | |
}; | |
contentType.AddPropertyType(propertyType, "content", "Content"); | |
} | |
_contentTypeService.Save(contentType); | |
} | |
private void EditorModelEventManager_SendingContentModel(HttpActionExecutedContext sender, EditorModelEventArgs<ContentItemDisplay> e) | |
{ | |
foreach (var variant in e.Model.Variants) | |
{ | |
if (variant.Name == "Readonly") | |
{ | |
foreach (var property in variant.Tabs.SelectMany(x => x.Properties)) | |
{ | |
property.Readonly = true; | |
} | |
} | |
} | |
} | |
public void Terminate() | |
{ } | |
} | |
} |
This file contains hidden or 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.Linq; | |
using Umbraco.Cms.Core.Composing; | |
using Umbraco.Cms.Core.DependencyInjection; | |
using Umbraco.Cms.Core.Events; | |
using Umbraco.Cms.Core.Models; | |
using Umbraco.Cms.Core.Notifications; | |
using Umbraco.Cms.Core.PropertyEditors; | |
using Umbraco.Cms.Core.Serialization; | |
using Umbraco.Cms.Core.Services; | |
using Umbraco.Cms.Core.Strings; | |
namespace Umbraco.Cms.Web.UI | |
{ | |
public class DataEditorComposer : IComposer | |
{ | |
public void Compose(IUmbracoBuilder builder) | |
{ | |
builder.AddNotificationHandler<UmbracoApplicationStartingNotification, DataEditorUmbracoApplicationStartingNotificationHandler>(); | |
builder.AddNotificationHandler<SendingContentNotification, DataEditorSendingContentNotificationHandler>(); | |
} | |
} | |
public class DataEditorUmbracoApplicationStartingNotificationHandler : INotificationHandler<UmbracoApplicationStartingNotification> | |
{ | |
private readonly IContentTypeService _contentTypeService; | |
private readonly IShortStringHelper _shortStringHelper; | |
private readonly PropertyEditorCollection _propertyEditors; | |
private readonly IDataTypeService _dataTypeService; | |
private readonly IConfigurationEditorJsonSerializer _serializer; | |
public DataEditorUmbracoApplicationStartingNotificationHandler(IContentTypeService contentTypeService, IShortStringHelper shortStringHelper, PropertyEditorCollection propertyEditors, IDataTypeService dataTypeService, IConfigurationEditorJsonSerializer serializer) | |
{ | |
_contentTypeService = contentTypeService; | |
_shortStringHelper = shortStringHelper; | |
_propertyEditors = propertyEditors; | |
_dataTypeService = dataTypeService; | |
_serializer = serializer; | |
} | |
public void Handle(UmbracoApplicationStartingNotification notification) | |
{ | |
var contentType = _contentTypeService.Get("dataEditorTest"); | |
if (contentType != null) | |
return; | |
// Ensure data types for all editors | |
foreach (var propertyEditor in _propertyEditors) | |
{ | |
var dataTypes = _dataTypeService.GetByEditorAlias(propertyEditor.Alias); | |
if (dataTypes.Any() == false) | |
{ | |
var dataType = new DataType(propertyEditor, _serializer) | |
{ | |
Name = propertyEditor.Name + " (auto-created)" | |
}; | |
_dataTypeService.Save(dataType); | |
} | |
} | |
// Create content type with all data types | |
contentType = new ContentType(_shortStringHelper, -1) | |
{ | |
Name = "Data Editor test", | |
Alias = "dataEditorTest", | |
AllowedAsRoot = true | |
}; | |
var systemDataTypeIds = new[] | |
{ | |
Core.Constants.DataTypes.DefaultContentListView, | |
Core.Constants.DataTypes.DefaultMediaListView, | |
Core.Constants.DataTypes.DefaultMembersListView | |
}; | |
foreach (var dataType in _dataTypeService.GetAll()) | |
{ | |
if (systemDataTypeIds.Contains(dataType.Id)) | |
{ | |
continue; | |
} | |
var propertyType = new PropertyType(_shortStringHelper, dataType) | |
{ | |
Name = dataType.Name, | |
Alias = dataType.EditorAlias + "_" + Guid.NewGuid() | |
}; | |
contentType.AddPropertyType(propertyType, "content", "Content"); | |
} | |
_contentTypeService.Save(contentType); | |
} | |
} | |
public class DataEditorSendingContentNotificationHandler : INotificationHandler<SendingContentNotification> | |
{ | |
public void Handle(SendingContentNotification notification) | |
{ | |
foreach (var variant in notification.Content.Variants) | |
{ | |
if (variant.Name == "Readonly") | |
{ | |
foreach (var property in variant.Tabs.SelectMany(x => x.Properties)) | |
{ | |
property.Readonly = true; | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment