public IPublishedElement ConvertToElement( BlockItemData data, PropertyCacheLevel referenceCacheLevel, bool preview) { // hack! we need to cast, we have no choice beacuse we cannot make breaking changes. var publishedContentCache = _publishedSnapshotAccessor.PublishedSnapshot.Content as IPublishedContentCache2; if (publishedContentCache == null) { throw new InvalidOperationException("The published content cache is not " + typeof(IPublishedContentCache2)); } // only convert element types - content types will cause an exception when PublishedModelFactory creates the model var publishedContentType = publishedContentCache.GetContentType(data.ContentTypeKey); if (publishedContentType == null || publishedContentType.IsElement == false) { return(null); } var propertyValues = data.RawPropertyValues; // Get the udi from the deserialized object. If this is empty we can fallback to checking the 'key' if there is one var key = (data.Udi is GuidUdi gudi) ? gudi.Guid : Guid.Empty; if (propertyValues.TryGetValue("key", out var keyo)) { Guid.TryParse(keyo.ToString(), out key); } IPublishedElement element = new PublishedElement(publishedContentType, key, propertyValues, preview, referenceCacheLevel, _publishedSnapshotAccessor); element = _publishedModelFactory.CreateModel(element); return(element); }
private bool ResolveBlockItemData(BlockItemData block, Dictionary<string, Dictionary<string, PropertyType>> contentTypePropertyTypes) { var contentType = GetElementType(block); if (contentType == null) return false; // get the prop types for this content type but keep a dictionary of found ones so we don't have to keep re-looking and re-creating // objects on each iteration. if (!contentTypePropertyTypes.TryGetValue(contentType.Alias, out var propertyTypes)) propertyTypes = contentTypePropertyTypes[contentType.Alias] = contentType.CompositionPropertyTypes.ToDictionary(x => x.Alias, x => x); var propValues = new Dictionary<string, BlockPropertyValue>(); // find any keys that are not real property types and remove them foreach (var prop in block.RawPropertyValues.ToList()) { // doesn't exist so remove it if (!propertyTypes.TryGetValue(prop.Key, out var propType)) { block.RawPropertyValues.Remove(prop.Key); _logger.Warn<BlockEditorValues>("The property {PropertyKey} for block {BlockKey} was removed because the property type {PropertyTypeAlias} was not found on {ContentTypeAlias}", prop.Key, block.Key, prop.Key, contentType.Alias); } else { // set the value to include the resolved property type propValues[prop.Key] = new BlockPropertyValue(prop.Value, propType); } } block.ContentTypeAlias = contentType.Alias; block.PropertyValues = propValues; return true; }
public IPublishedElement ConvertToElement(BlockItemData data, PropertyCacheLevel referenceCacheLevel, bool preview) { var publishedContentType = GetContentType(data.ContentTypeKey); // Only convert element types if (publishedContentType == null || publishedContentType.IsElement == false) { return(null); } var propertyValues = data.RawPropertyValues; // Get the UDI from the deserialized object. If this is empty, we can fallback to checking the 'key' if there is one var key = (data.Udi is GuidUdi gudi) ? gudi.Guid : Guid.Empty; if (key == Guid.Empty && propertyValues.TryGetValue("key", out var keyo)) { Guid.TryParse(keyo.ToString(), out key); } IPublishedElement element = new PublishedElement(publishedContentType, key, propertyValues, preview, referenceCacheLevel, _publishedSnapshotAccessor); element = _publishedModelFactory.CreateModel(element); return(element); }
public IPublishedElement?ConvertToElement(BlockItemData data, PropertyCacheLevel referenceCacheLevel, bool preview) { IPublishedContentCache?publishedContentCache = _publishedSnapshotAccessor.GetRequiredPublishedSnapshot().Content; // Only convert element types - content types will cause an exception when PublishedModelFactory creates the model IPublishedContentType?publishedContentType = publishedContentCache?.GetContentType(data.ContentTypeKey); if (publishedContentType == null || publishedContentType.IsElement == false) { return(null); } Dictionary <string, object?> propertyValues = data.RawPropertyValues; // Get the UDI from the deserialized object. If this is empty, we can fallback to checking the 'key' if there is one Guid key = data.Udi is GuidUdi gudi ? gudi.Guid : Guid.Empty; if (key == Guid.Empty && propertyValues.TryGetValue("key", out var keyo)) { Guid.TryParse(keyo !.ToString(), out key); } IPublishedElement element = new PublishedElement(publishedContentType, key, propertyValues, preview, referenceCacheLevel, _publishedSnapshotAccessor); element = _publishedModelFactory.CreateModel(element); return(element); }
private IContentType?GetElementType(BlockItemData item) { _contentTypes.Value.TryGetValue(item.ContentTypeKey, out var contentType); return(contentType); }