public override async Task <IDisplayResult> UpdateAsync(WidgetsListPart part, UpdatePartEditorContext context)
        {
            var contentItemDisplayManager = _serviceProvider.GetRequiredService <IContentItemDisplayManager>();

            var model = new WidgetsListPartEditViewModel {
                WidgetsListPart = part
            };

            await context.Updater.TryUpdateModelAsync(model, Prefix);

            var zonedContentItems = new Dictionary <string, List <ContentItem> >();

            // Remove any content or the zones would be merged and not be cleared
            part.Content.Widgets.RemoveAll();

            for (var i = 0; i < model.Prefixes.Length; i++)
            {
                var contentType = model.ContentTypes[i];
                var zone        = model.Zones[i];
                var prefix      = model.Prefixes[i];

                var contentItem = await _contentManager.NewAsync(contentType);

                if (part.Widgets.ContainsKey(zone))
                {
                    var existingContentItem = part.Widgets[zone].FirstOrDefault(x => String.Equals(x.ContentItemId, model.Prefixes[i], StringComparison.OrdinalIgnoreCase));
                    // When the content item already exists merge its elements to preverse nested content item ids.
                    // All of the data for these merged items is then replaced by the model values on update, while a nested content item id is maintained.
                    // This prevents nested items which rely on the content item id, i.e. the media attached field, losing their reference point.
                    if (existingContentItem != null)
                    {
                        contentItem.ContentItemId = model.Prefixes[i];
                        contentItem.Merge(existingContentItem);
                    }
                }

                contentItem.Weld(new WidgetMetadata());

                var widgetModel = await contentItemDisplayManager.UpdateEditorAsync(contentItem, context.Updater, context.IsNew, htmlFieldPrefix : prefix);

                if (!zonedContentItems.ContainsKey(zone))
                {
                    zonedContentItems.Add(zone, new List <ContentItem>());
                }

                zonedContentItems[zone].Add(contentItem);
            }

            part.Widgets = zonedContentItems;

            return(Edit(part, context));
        }
        public override async Task <IDisplayResult> UpdateAsync(WidgetsListPart part, UpdatePartEditorContext context)
        {
            var contentItemDisplayManager = _serviceProvider.GetRequiredService <IContentItemDisplayManager>();

            var model = new WidgetsListPartEditViewModel {
                WidgetsListPart = part
            };

            await context.Updater.TryUpdateModelAsync(model, Prefix);

            var zonedContentItems = new Dictionary <string, List <ContentItem> >();

            // Remove any content or the zones would be merged and not be cleared
            part.Content.Widgets.RemoveAll();

            for (var i = 0; i < model.Prefixes.Length; i++)
            {
                var contentType = model.ContentTypes[i];
                var zone        = model.Zones[i];
                var prefix      = model.Prefixes[i];

                var contentItem = await _contentManager.NewAsync(contentType);

                if (part.Widgets.ContainsKey(zone))
                {
                    var existing = part.Widgets[zone].FirstOrDefault(x => String.Equals(x.ContentItemId, model.Prefixes[i], StringComparison.OrdinalIgnoreCase));
                    if (existing != null)
                    {
                        contentItem.ContentItemId = model.Prefixes[i];
                    }
                }

                contentItem.Weld(new WidgetMetadata());

                var widgetModel = await contentItemDisplayManager.UpdateEditorAsync(contentItem, context.Updater, context.IsNew, htmlFieldPrefix : prefix);

                if (!zonedContentItems.ContainsKey(zone))
                {
                    zonedContentItems.Add(zone, new List <ContentItem>());
                }

                zonedContentItems[zone].Add(contentItem);
            }

            part.Widgets = zonedContentItems;

            return(Edit(part, context));
        }
Ejemplo n.º 3
0
        public override async Task <IDisplayResult> UpdateAsync(WidgetsListPart part, BuildPartEditorContext context)
        {
            var contentItemDisplayManager = _serviceProvider.GetRequiredService <IContentItemDisplayManager>();

            var model = new WidgetsListPartEditViewModel {
                WidgetsListPart = part
            };

            await context.Updater.TryUpdateModelAsync(model, Prefix);

            part.Widgets.Clear();

            // Remove any content or the zones would be merged and not be cleared
            part.Content.Widgets.RemoveAll();

            for (var i = 0; i < model.Prefixes.Length; i++)
            {
                var contentType = model.ContentTypes[i];
                var zone        = model.Zones[i];
                var prefix      = model.Prefixes[i];

                var contentItem = _contentManager.New(contentType);

                contentItem.Weld(new WidgetMetadata());

                var widgetModel = await contentItemDisplayManager.UpdateEditorAsync(contentItem, context.Updater, htmlFieldPrefix : prefix);

                if (!part.Widgets.ContainsKey(zone))
                {
                    part.Widgets.Add(zone, new List <ContentItem>());
                }

                part.Widgets[zone].Add(contentItem);
            }

            return(Edit(part, context));
        }