protected override EditorResult OnBuildEditor(FormElement element, ElementEditorContext context)
        {
            var contentType           = element.FormBindingContentType;
            var contentTypeDefinition = !String.IsNullOrWhiteSpace(contentType) ? _contentDefinitionManager.GetTypeDefinition(contentType) : default(ContentTypeDefinition);

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

            var viewModel = element.Data.GetModel <FormBindingSettings>() ?? new FormBindingSettings();

            viewModel.AvailableBindings = _bindingManager.DescribeBindingsFor(contentTypeDefinition).ToArray();

            if (context.Updater != null)
            {
                context.Updater.TryUpdateModel(viewModel, null, null, new[] { "AvailableBindings" });
                viewModel.Store(element.Data);
            }

            var bindingsEditor = context.ShapeFactory.EditorTemplate(TemplateName: "FormBindings", Model: viewModel);

            bindingsEditor.Metadata.Position = "Bindings:20";

            return(Editor(context, bindingsEditor));
        }
Exemplo n.º 2
0
        public ContentItem CreateContentItem(Form form, IValueProvider valueProvider)
        {
            var contentTypeDefinition = _contentDefinitionManager.GetTypeDefinition(form.FormBindingContentType);

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

            var contentItem = _contentManager.New(contentTypeDefinition.Name);

            // Create the version record before updating fields to prevent those field values from being lost when invoking Create.
            // If Create is invoked while VersionRecord is null, a new VersionRecord will be created, wiping out our field values.
            contentItem.VersionRecord = new ContentItemVersionRecord {
                ContentItemRecord = new ContentItemRecord(),
                Number            = 1,
                Latest            = true,
                Published         = true
            };

            var lookup       = _bindingManager.DescribeBindingsFor(contentTypeDefinition);
            var formElements = GetFormElements(form);

            foreach (var element in formElements)
            {
                var context = new ReadElementValuesContext {
                    ValueProvider = valueProvider
                };
                ReadElementValues(element, context);

                var value           = context.Output[element.Name];
                var bindingSettings = element.Data.GetModel <FormBindingSettings>();

                if (bindingSettings != null)
                {
                    foreach (var partBindingSettings in bindingSettings.Parts)
                    {
                        InvokePartBindings(contentItem, lookup, partBindingSettings, value);

                        foreach (var fieldBindingSettings in partBindingSettings.Fields)
                        {
                            InvokeFieldBindings(contentItem, lookup, partBindingSettings, fieldBindingSettings, value);
                        }
                    }
                }
            }

            var contentTypeSettings = contentTypeDefinition.Settings.GetModel <ContentTypeSettings>();

            _contentManager.Create(contentItem, VersionOptions.Draft);

            if (form.Publication == "Publish" || !contentTypeSettings.Draftable)
            {
                _contentManager.Publish(contentItem);
            }

            return(contentItem);
        }