示例#1
0
        public object GetPropertyValue(PropertyInfo property, JToken elementData, ResolvingContext context)
        {
            var element = (JObject)elementData;
            var str     = element.Property("value")?.Value?.ToObject <string>();

            return($"Hello {str}!");
        }
示例#2
0
        public object GetPropertyValue(PropertyInfo property, JToken elementData, ResolvingContext context)
        {
            var element = (JObject)elementData;
            var dt      = element.Property("value")?.Value?.ToObject <DateTime>();

            if (dt != null)
            {
                var           udt = DateTime.SpecifyKind(dt.Value, DateTimeKind.Utc);
                ZonedDateTime zdt = ZonedDateTime.FromDateTimeOffset(udt);
                return(zdt);
            }
            return(null);
        }
示例#3
0
        private async Task <object> GetPropertyValueAsync(JObject elementsData, PropertyInfo property, JObject linkedItems, ResolvingContext context, IContentItemSystemAttributes itemSystemAttributes, Dictionary <string, object> processedItems, List <PropertyInfo> richTextPropertiesToBeProcessed)
        {
            var elementDefinition = GetElementData(elementsData, property, itemSystemAttributes);

            var elementValue = elementDefinition?.Value;

            if (elementValue != null)
            {
                var valueConverter = GetValueConverter(property);
                if (valueConverter != null)
                {
                    return((elementValue["type"].ToString()) switch
                    {
                        "rich_text" => await GetElementModelAsync <RichTextElementValue, string>(property, context, elementValue, valueConverter),
                        "asset" => await GetElementModelAsync <ContentElementValue <Asset>, Asset>(property, context, elementValue, valueConverter),
                        "number" => await GetElementModelAsync <ContentElementValue <decimal?>, decimal?>(property, context, elementValue, valueConverter),
                        "date_time" => await GetElementModelAsync <ContentElementValue <DateTime>, DateTime>(property, context, elementValue, valueConverter),
                        "multiple_choice" => await GetElementModelAsync <ContentElementValue <List <MultipleChoiceOption> >, List <MultipleChoiceOption> >(property, context, elementValue, valueConverter),
                        "taxonomy" => await GetElementModelAsync <TaxonomyElementValue, IEnumerable <ITaxonomyTerm> >(property, context, elementValue, valueConverter),
                        // Custom element, text element, URL slug element
                        _ => await GetElementModelAsync <ContentElementValue <string>, string>(property, context, elementValue, valueConverter),
                    });
        private object GetPropertyValue(JObject elementsData, PropertyInfo property, JObject linkedItems, ResolvingContext context, ContentItemSystemAttributes itemSystemAttributes, ref Dictionary <string, object> processedItems, ref List <PropertyInfo> richTextPropertiesToBeProcessed)
        {
            var elementData = GetElementData(elementsData, property, itemSystemAttributes);

            var valueConverter = GetValueConverter(property);

            if (valueConverter != null)
            {
                return(valueConverter.GetPropertyValue(property, elementData, context));
            }

            if (property.PropertyType == typeof(string))
            {
                var(value, isRichText) = GetStringValue(elementData);

                if (isRichText)
                {
                    richTextPropertiesToBeProcessed.Add(property);
                }

                return(value);
            }

            if (IsNonHierarchicalField(property.PropertyType))
            {
                return(GetRawValue(elementData)?.ToObject(property.PropertyType));
            }

            if (IsGenericHierarchicalField(property.PropertyType))
            {
                return(GetLinkedItemsValue(elementData, linkedItems, property.PropertyType, ref processedItems));
            }

            return(null);
        }
        private object GetRichTextValue(string value, JObject elementsData, PropertyInfo property, JObject linkedItems, ResolvingContext context, ContentItemSystemAttributes itemSystemAttributes, ref Dictionary <string, object> processedItems, ref HashSet <RichTextContentElements> currentlyResolvedRichStrings)
        {
            var currentlyProcessedString = new RichTextContentElements(itemSystemAttributes?.Codename, property.Name);

            if (currentlyResolvedRichStrings.Contains(currentlyProcessedString))
            {
                // If this element is already being processed it's necessary to use it as is (with removed inline content items)
                // otherwise resolving would be stuck in an infinite loop
                return(RemoveInlineContentItems(value));
            }

            currentlyResolvedRichStrings.Add(currentlyProcessedString);

            var elementData           = GetElementData(elementsData, property, itemSystemAttributes);
            var linkedItemsInRichText = GetLinkedItemsInRichText(elementData);

            value = ProcessInlineContentItems(linkedItems, processedItems, value, linkedItemsInRichText, currentlyResolvedRichStrings);

            currentlyResolvedRichStrings.Remove(currentlyProcessedString);

            return(value);
        }
示例#6
0
        public Task <object> GetPropertyValueAsync <TElement>(PropertyInfo property, TElement element, ResolvingContext context) where TElement : IContentElementValue <DateTime>
        {
            var udt = DateTime.SpecifyKind(element.Value, DateTimeKind.Utc);

            return(Task.FromResult((object)ZonedDateTime.FromDateTimeOffset(udt)));
        }
示例#7
0
 public Task <object> GetPropertyValueAsync <TElement>(PropertyInfo property, TElement element, ResolvingContext context) where TElement : IContentElementValue <string>
 {
     return(Task.FromResult((object)$"Hello {element.Value}!"));
 }
        public object GetPropertyValue(PropertyInfo property, JToken elementData, ResolvingContext context)
        {
            if (!typeof(IRichTextContent).IsAssignableFrom(property.PropertyType))
            {
                throw new InvalidOperationException($"Type of property {property.Name} must implement {nameof(IRichTextContent)} in order to receive rich text content.");
            }

            var element = ((JObject)elementData);

            if (element == null)
            {
                return(null);
            }

            var links  = element.Property("links")?.Value;
            var images = element.Property("images").Value;
            var value  = element.Property("value")?.Value?.ToObject <string>();

            // Handle rich_text link resolution
            if (links != null && elementData != null && context.ContentLinkUrlResolver != null)
            {
                value = new ContentLinkResolver(context.ContentLinkUrlResolver).ResolveContentLinks(value, links);
            }

            var blocks = new List <IRichTextBlock>();

            var htmlInput = new HtmlParser().Parse(value);

            foreach (var block in htmlInput.Body.Children)
            {
                if (block.TagName?.Equals("object", StringComparison.OrdinalIgnoreCase) == true && block.GetAttribute("type") == "application/kenticocloud" && block.GetAttribute("data-type") == "item")
                {
                    var codename = block.GetAttribute("data-codename");
                    blocks.Add(new InlineContentItem {
                        ContentItem = context.GetLinkedItem(codename)
                    });
                }
                else if (block.TagName?.Equals("figure", StringComparison.OrdinalIgnoreCase) == true)
                {
                    var img = block.Children.FirstOrDefault(child => child.TagName?.Equals("img", StringComparison.OrdinalIgnoreCase) == true);
                    if (img != null)
                    {
                        var assetId = img.GetAttribute("data-asset-id");
                        var asset   = images[assetId];
                        blocks.Add(new InlineImage
                        {
                            Url         = asset.Value <string>("url"),
                            Description = asset.Value <string>("description"),
                            Height      = asset.Value <int>("height"),
                            Width       = asset.Value <int>("width")
                        });
                    }
                }
                else
                {
                    blocks.Add(new HtmlContent {
                        Html = block.OuterHtml
                    });
                }
            }

            return(new RichTextContent
            {
                Blocks = blocks
            });
        }
示例#9
0
        public async Task <object> GetPropertyValueAsync <TElement>(PropertyInfo property, TElement contentElement, ResolvingContext context) where TElement : IContentElementValue <string>
        {
            if (!typeof(IRichTextContent).IsAssignableFrom(property.PropertyType))
            {
                throw new InvalidOperationException($"Type of property {property.Name} must implement {nameof(IRichTextContent)} in order to receive rich text content.");
            }

            if (!(contentElement is IRichTextElementValue element))
            {
                return(null);
            }

            var links = element.Links;
            var value = element.Value;

            // Handle rich_text link resolution
            if (links != null && context.ContentLinkUrlResolver != null)
            {
                value = await new ContentLinkResolver(context.ContentLinkUrlResolver).ResolveContentLinksAsync(value, links);
            }

            var blocks    = new List <IRichTextBlock>();
            var htmlInput = await Parser.ParseDocumentAsync(value);

            foreach (var block in htmlInput.Body.Children)
            {
                if (block.TagName?.Equals("object", StringComparison.OrdinalIgnoreCase) == true && block.GetAttribute("type") == "application/kenticocloud" && block.GetAttribute("data-type") == "item")
                {
                    var codename = block.GetAttribute("data-codename");
                    blocks.Add(new InlineContentItem(await context.GetLinkedItem(codename)));
                }
                else if (block.TagName?.Equals("figure", StringComparison.OrdinalIgnoreCase) == true)
                {
                    var img = block.Children.FirstOrDefault(child => child.TagName?.Equals("img", StringComparison.OrdinalIgnoreCase) == true);
                    if (img != null)
                    {
                        var assetId = Guid.Parse(img.GetAttribute("data-asset-id"));
                        blocks.Add(element.Images[assetId]);
                    }
                }
                else
                {
                    blocks.Add(new HtmlContent {
                        Html = block.OuterHtml
                    });
                }
            }

            return(new RichTextContent(blocks));
        }