public object ConvertValue(Type type, string value) { if (GuidUdi.TryParse(value, out var udi) == true && ContentTypeCacheHelper.TryGetAlias(udi.Guid, out var alias, _contentTypeService) == true) { return(_umbracoContextAccessor.UmbracoContext.Content.GetContentType(alias)); } return(default);
public override object ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object inter, bool preview) { if (inter is IEnumerable <ContentBlock> items) { var elements = new List <IPublishedElement>(); foreach (var item in items) { if (item == null || item.ElementType.Equals(Guid.Empty)) { continue; } // NOTE: [LK:2019-09-03] Why `IPublishedCache` doesn't support Guids or UDIs, I do not know!? // Thought v8 was meant to be "GUID ALL THE THINGS!!1"? ¯\_(ツ)_/¯ if (ContentTypeCacheHelper.TryGetAlias(item.ElementType, out var alias, _contentTypeService) == false) { continue; } var contentType = _publishedSnapshotAccessor.PublishedSnapshot.Content.GetContentType(alias); if (contentType == null || contentType.IsElement == false) { continue; } var properties = new List <IPublishedProperty>(); foreach (var thing in item.Value) { var propType = contentType.GetPropertyType(thing.Key); if (propType != null) { properties.Add(new DetachedPublishedProperty(propType, owner, thing.Value, preview)); } } elements.Add(_publishedModelFactory.CreateModel(new DetachedPublishedElement(item.Key, contentType, properties))); } return(elements); } return(base.ConvertIntermediateToObject(owner, propertyType, referenceCacheLevel, inter, preview)); }
public HttpResponseMessage GetPreviewMarkup([FromBody] JObject item, int elementIndex, Guid elementKey, int contentId) { var preview = true; var content = UmbracoContext.Content.GetById(true, contentId); if (content == null) { _logger.Debug <ContentBlocksApiController>($"Unable to retrieve content for ID '{contentId}', it is most likely a new unsaved page."); } var element = default(IPublishedElement); var block = item.ToObject <ContentBlock>(); if (block != null && block.ElementType.Equals(Guid.Empty) == false) { if (ContentTypeCacheHelper.TryGetAlias(block.ElementType, out var alias, Services.ContentTypeService) == true) { var contentType = UmbracoContext.PublishedSnapshot.Content.GetContentType(alias); if (contentType != null && contentType.IsElement == true) { var properties = new List <IPublishedProperty>(); foreach (var thing in block.Value) { var propType = contentType.GetPropertyType(thing.Key); if (propType != null) { properties.Add(new DetachedPublishedProperty(propType, null, thing.Value, preview)); } } element = _publishedModelFactory.CreateModel(new DetachedPublishedElement(block.Key, contentType, properties)); } } } var viewData = new System.Web.Mvc.ViewDataDictionary(element) { { nameof(content), content }, { nameof(element), element }, { nameof(elementIndex), elementIndex }, }; if (ContentTypeCacheHelper.TryGetIcon(content.ContentType.Alias, out var contentIcon, Services.ContentTypeService) == true) { viewData.Add(nameof(contentIcon), contentIcon); } if (ContentTypeCacheHelper.TryGetIcon(element.ContentType.Alias, out var elementIcon, Services.ContentTypeService) == true) { viewData.Add(nameof(elementIcon), elementIcon); } var markup = default(string); try { markup = ContentBlocksViewHelper.RenderPartial(element.ContentType.Alias, viewData); } catch (InvalidCastException icex) { // NOTE: This type of exception happens on a new (unsaved) page, when the context becomes the parent page, // and the preview view is strongly typed to the current page's model type. markup = "<p class=\"text-center mt4\">Unable to render the preview until the page has been saved.</p>"; _logger.Error <ContentBlocksApiController>(icex, "Error rendering preview view."); } catch (Exception ex) { markup = $"<pre class=\"error\"><code>{ex}</code></pre>"; _logger.Error <ContentBlocksApiController>(ex, "Error rendering preview view."); } return(Request.CreateResponse(HttpStatusCode.OK, new { elementKey, markup })); }