Beispiel #1
0
            public override IDictionary <string, PreValue> ConvertEditorToDb(IDictionary <string, object> editorValue, PreValueCollection currentValue)
            {
                if (editorValue.TryGetValue("embeddedContentConfig", out object value))
                {
                    List <IContentType>          contentTypes  = _contentTypeService.GetAllContentTypes().ToList();
                    EmbeddedContentConfigDisplay configDisplay = JsonConvert.DeserializeObject <EmbeddedContentConfigDisplay>(value.ToString());
                    var config = new EmbeddedContentConfig
                    {
                        EnableCollapsing = configDisplay.EnableCollapsing,
                        EnableFiltering  = configDisplay.EnableFiltering,
                        DocumentTypes    = from item in configDisplay.DocumentTypes
                                           let contentType = contentTypes.FirstOrDefault(x => x.Alias == item.DocumentTypeAlias)
                                                             where contentType != null
                                                             select new EmbeddedContentConfigDocumentType
                        {
                            AllowEditingName  = item.AllowEditingName,
                            DocumentTypeAlias = item.DocumentTypeAlias,
                            Group             = item.Group,
                            MaxInstances      = item.MaxInstances,
                            NameTemplate      = item.NameTemplate,
                            SettingsTabKey    = item.SettingsTab.HasValue ? contentType.CompositionPropertyGroups.FirstOrDefault(x => x.Id == item.SettingsTab)?.Key : null
                        },
                        Groups   = configDisplay.Groups,
                        MaxItems = configDisplay.MaxItems,
                        MinItems = configDisplay.MinItems
                    };

                    editorValue["embeddedContentConfig"] = JsonConvert.SerializeObject(config);
                }
                return(base.ConvertEditorToDb(editorValue, currentValue));
            }
Beispiel #2
0
        public Type GetPropertyValueType(PublishedPropertyType propertyType)
        {
            EmbeddedContentConfig config = GetConfig(propertyType.DataTypeId);

            if (config.MaxItems == 1)
            {
                return(typeof(IPublishedContent));
            }

            return(typeof(IEnumerable <IPublishedContent>));
        }
Beispiel #3
0
            public override IDictionary <string, object> ConvertDbToEditor(IDictionary <string, object> defaultPreVals, PreValueCollection persistedPreVals)
            {
                if (persistedPreVals.PreValuesAsDictionary.TryGetValue("embeddedContentConfig",
                                                                       out PreValue preValue) && false == string.IsNullOrEmpty(preValue.Value))
                {
                    List <IContentType>   contentTypes = _contentTypeService.GetAllContentTypes().ToList();
                    EmbeddedContentConfig config       = JsonConvert.DeserializeObject <EmbeddedContentConfig>(preValue.Value);
                    var configDisplay = new EmbeddedContentConfigDisplay
                    {
                        EnableCollapsing = config.EnableCollapsing,
                        EnableFiltering  = config.EnableFiltering,
                        DocumentTypes    = from item in config.DocumentTypes
                                           let contentType =
                            contentTypes.FirstOrDefault(x => x.Alias == item.DocumentTypeAlias)
                            where contentType != null
                            select new EmbeddedContentConfigDocumentTypeDisplay
                        {
                            AllowEditingName  = item.AllowEditingName,
                            Description       = UmbracoDictionaryTranslate(contentType.Description),
                            DocumentTypeAlias = item.DocumentTypeAlias,
                            Group             = item.Group,
                            Icon         = contentType.Icon,
                            MaxInstances = item.MaxInstances,
                            Name         = UmbracoDictionaryTranslate(contentType.Name),
                            NameTemplate = item.NameTemplate,
                            SettingsTab  =
                                item.SettingsTabKey.HasValue
                                                    ? contentType.CompositionPropertyGroups
                                .FirstOrDefault(x => x.Key == item.SettingsTabKey)?.Id
                                                    : null
                        },
                        Groups   = config.Groups,
                        MaxItems = config.MaxItems,
                        MinItems = config.MinItems
                    };

                    preValue.Value = JsonConvert.SerializeObject(configDisplay);
                }

                return(base.ConvertDbToEditor(defaultPreVals, persistedPreVals));
            }
Beispiel #4
0
        public override object ConvertSourceToObject(PublishedPropertyType propertyType, object source, bool preview)
        {
            EmbeddedContentConfig config = GetConfig(propertyType.DataTypeId);

            using (_profilingLogger.DebugDuration <EmbeddedContentValueConverter>($"ConvertSourceToObject({propertyType.PropertyTypeAlias})"))
            {
                if (source == null)
                {
                    if (config.MaxItems == 1)
                    {
                        return(null);
                    }

                    return(Enumerable.Empty <IPublishedContent>());
                }
                ;
                var items  = ((JArray)source).ToObject <EmbeddedContentItem[]>();
                var result = new List <IPublishedContent>(items.Length);
                PublishedContentSet <IPublishedContent> contentSet = result.ToContentSet();

                for (var i = 0; i < items.Length; i++)
                {
                    EmbeddedContentItem item = items[i];

                    if (!item.Published)
                    {
                        continue;
                    }

                    if (config.DocumentTypes.FirstOrDefault(x => x.DocumentTypeAlias == item.ContentTypeAlias) == null)
                    {
                        continue;
                    }

                    PublishedContentType contentType = null;
                    try
                    {
                        contentType = PublishedContentType.Get(PublishedItemType.Content, item.ContentTypeAlias);
                    }
                    catch (Exception ex)
                    {
                        _profilingLogger.Logger.Error <EmbeddedContentValueConverter>($"Error getting content type {item.ContentTypeAlias}.", ex);
                    }

                    if (contentType == null)
                    {
                        continue;
                    }

                    IPublishedContent content =
                        new PublishedEmbeddedContent(_userService, item, contentType, contentSet, i, preview);

                    if (_publishedContentModelFactory != null)
                    {
                        content = _publishedContentModelFactory.CreateModel(content);
                    }

                    result.Add(content);
                }

                if (config.MaxItems == 1)
                {
                    return(result.FirstOrDefault());
                }

                return(contentSet);
            }
        }