public IEnumerable <EditPartViewModel> LoadParts(bool metadataPartsOnly)
        {
            var typeNames = new HashSet <string>(LoadTypes().Select(ctd => ctd.Name));

            // user-defined parts
            // except for those parts with the same name as a type (implicit type's part or a mistake)
            var userContentParts = _contentDefinitionManager.LoadPartDefinitions()
                                   .Where(cpd => !typeNames.Contains(cpd.Name))
                                   .Select(cpd => new EditPartViewModel(cpd))
                                   .ToDictionary(
                k => k.Name,
                v => v);

            // code-defined parts
            var codeDefinedParts = metadataPartsOnly
                ? Enumerable.Empty <EditPartViewModel>()
                : _contentPartTypes
                                   .Where(cpd => !userContentParts.ContainsKey(cpd.Name))
                                   .Select(cpi => new EditPartViewModel {
                Name = cpi.Name, DisplayName = cpi.Name
            })
                                   .ToList();

            // Order by display name
            return(codeDefinedParts
                   .Union(userContentParts.Values)
                   .OrderBy(m => m.DisplayName));
        }
Beispiel #2
0
        // Migration content part definitions.
        // This code can be removed in a later version.
        public int UpdateFrom4()
        {
            var partDefinitions = _contentDefinitionManager.LoadPartDefinitions();

            foreach (var partDefinition in partDefinitions)
            {
                var existingPartSettings = partDefinition.Settings.ToObject <ContentPartSettings>();

                // Do this before creating builder, so settings are removed from the builder settings object.
                // Remove existing properties from JObject
                var contentTypeSettingsProperties = existingPartSettings.GetType().GetProperties();
                foreach (var property in contentTypeSettingsProperties)
                {
                    partDefinition.Settings.Remove(property.Name);
                }

                _contentDefinitionManager.AlterPartDefinition(partDefinition.Name, partBuilder =>
                {
                    partBuilder.WithSettings(existingPartSettings);
                    foreach (var fieldDefinition in partDefinition.Fields)
                    {
                        var existingFieldSettings = fieldDefinition.Settings.ToObject <ContentPartFieldSettings>();

                        // Do this before creating builder, so settings are removed from the builder settings object.
                        // Remove existing properties from JObject
                        var fieldSettingsProperties = existingFieldSettings.GetType().GetProperties();
                        foreach (var property in fieldSettingsProperties)
                        {
                            fieldDefinition.Settings.Remove(property.Name);
                        }

                        partBuilder.WithField(fieldDefinition.Name, fieldBuilder =>
                        {
                            fieldBuilder.WithSettings(existingFieldSettings);
                        });
                    }
                });
            }

            return(5);
        }
        /// <summary>
        /// Migrate existing ContentField settings to WithSettings<typeparamref name="TSettings"/>
        /// This method will be removed in a future release.
        /// </summary>
        /// <typeparam name="TField"></typeparam>
        /// <typeparam name="TSettings"></typeparam>
        /// <param name="manager"></param>
        public static void MigrateFieldSettings <TField, TSettings>(this IContentDefinitionManager manager)
            where TField : ContentField where TSettings : class
        {
            var partDefinitions = manager.LoadPartDefinitions();

            foreach (var partDefinition in partDefinitions)
            {
                manager.AlterPartDefinition(partDefinition.Name, partBuilder =>
                {
                    foreach (var fieldDefinition in partDefinition.Fields.Where(x => x.FieldDefinition.Name == typeof(TField).Name))
                    {
                        var existingFieldSettings = fieldDefinition.Settings.ToObject <TSettings>();

                        // Do this before creating builder, so settings are removed from the builder settings object.
                        // Remove existing properties from JObject
                        var fieldSettingsProperties = existingFieldSettings.GetType().GetProperties();
                        var hasSetting = false;
                        foreach (var property in fieldSettingsProperties)
                        {
                            if (fieldDefinition.Settings.ContainsKey(property.Name))
                            {
                                hasSetting = true;
                                fieldDefinition.Settings.Remove(property.Name);
                            }
                        }

                        // Only include settings if the definition already has at least one of these settings.
                        if (hasSetting)
                        {
                            partBuilder.WithField(fieldDefinition.Name, fieldBuilder =>
                            {
                                fieldBuilder.WithSettings(existingFieldSettings);
                            });
                        }
                    }
                });
            }
        }
Beispiel #4
0
        // This code can be removed in a later version.
        public int UpdateFrom3()
        {
            // For backwards compatability with liquid filters we disable html sanitization on existing field definitions.
            var partDefinitions = _contentDefinitionManager.LoadPartDefinitions();

            foreach (var partDefinition in partDefinitions)
            {
                if (partDefinition.Fields.Any(x => x.FieldDefinition.Name == "MarkdownField"))
                {
                    _contentDefinitionManager.AlterPartDefinition(partDefinition.Name, partBuilder =>
                    {
                        foreach (var fieldDefinition in partDefinition.Fields.Where(x => x.FieldDefinition.Name == "MarkdownField"))
                        {
                            partBuilder.WithField(fieldDefinition.Name, fieldBuilder =>
                            {
                                fieldBuilder.MergeSettings <MarkdownFieldSettings>(s => s.SanitizeHtml = false);
                            });
                        }
                    });
                }
            }

            return(4);
        }