/// <summary> /// Update the existing content Type based on the data in the attributes /// </summary> /// <param name="contentTypeService"></param> /// <param name="fileService"></param> /// <param name="attribute"></param> /// <param name="mediaType"></param> /// <param name="type"></param> /// <param name="dataTypeService"></param> private static void UpdateMediaType(IContentTypeService contentTypeService, IFileService fileService, UmbracoMediaTypeAttribute attribute, IMediaType mediaType, Type type, IDataTypeService dataTypeService) { mediaType.Name = attribute.MediaTypeName; mediaType.Alias = attribute.MediaTypeAlias; mediaType.Icon = attribute.Icon; mediaType.Description = attribute.Description; mediaType.IsContainer = attribute.EnableListView; mediaType.AllowedContentTypes = FetchAllowedContentTypes(attribute.AllowedChildren, contentTypeService); mediaType.AllowedAsRoot = attribute.AllowedAtRoot; Type parentType = type.BaseType; if (parentType != null && parentType != typeof(UmbracoGeneratedBase) && parentType.GetBaseTypes(false).Any(x => x == typeof(UmbracoGeneratedBase))) { UmbracoMediaTypeAttribute parentAttribute = parentType.GetCustomAttribute<UmbracoMediaTypeAttribute>(); if (parentAttribute != null) { string parentAlias = parentAttribute.MediaTypeAlias; IMediaType parentContentType = contentTypeService.GetMediaType(parentAlias); mediaType.ParentId = parentContentType.Id; } else { throw new Exception("The given base class has no UmbracoMediaTypeAttribute"); } } VerifyProperties(mediaType, type, dataTypeService); //verify if a tab has no properties, if so remove var propertyGroups = mediaType.PropertyGroups.ToArray(); int length = propertyGroups.Length; for (int i = 0; i < length; i++) { if (propertyGroups[i].PropertyTypes.Count == 0) { //remove mediaType.RemovePropertyGroup(propertyGroups[i].Name); } } //persist contentTypeService.Save(mediaType, 0); }
/// <summary> /// This method is called when the Media Type declared in the attribute hasn't been found in Umbraco /// </summary> /// <param name="contentTypeService"></param> /// <param name="fileService"></param> /// <param name="attribute"></param> /// <param name="type"></param> /// <param name="dataTypeService"></param> private static void CreateMediaType(IContentTypeService contentTypeService, IFileService fileService, UmbracoMediaTypeAttribute attribute, Type type, IDataTypeService dataTypeService) { IMediaType newMediaType; Type parentType = type.BaseType; if (parentType != null && parentType != typeof(UmbracoGeneratedBase) && parentType.GetBaseTypes(false).Any(x => x == typeof(UmbracoGeneratedBase))) { UmbracoMediaTypeAttribute parentAttribute = parentType.GetCustomAttribute<UmbracoMediaTypeAttribute>(); if (parentAttribute != null) { string parentAlias = parentAttribute.MediaTypeAlias; IMediaType parentContentType = contentTypeService.GetMediaType(parentAlias); newMediaType = new MediaType(parentContentType); } else { throw new Exception("The given base class has no UmbracoMediaTypeAttribute"); } } else { newMediaType = new MediaType(-1); } newMediaType.Name = attribute.MediaTypeName; newMediaType.Alias = attribute.MediaTypeAlias; newMediaType.Icon = attribute.Icon; newMediaType.Description = attribute.Description; newMediaType.AllowedAsRoot = attribute.AllowedAtRoot; newMediaType.IsContainer = attribute.EnableListView; newMediaType.AllowedContentTypes = FetchAllowedContentTypes(attribute.AllowedChildren, contentTypeService); //create tabs CreateTabs(newMediaType, type, dataTypeService); //create properties on the generic tab var propertiesOfRoot = type.GetProperties().Where(x => x.GetCustomAttribute<UmbracoPropertyAttribute>() != null); foreach (var item in propertiesOfRoot) { CreateProperty(newMediaType, null, dataTypeService, true, item); } //Save and persist the media Type contentTypeService.Save(newMediaType, 0); }