Changed field drivers to not export empty elements.

This results in much cleaner recipes.
This commit is contained in:
Daniel Stolt
2015-09-18 02:42:40 +02:00
parent d310a56c38
commit 1edf94756c
8 changed files with 27 additions and 16 deletions

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Web; using System.Web;
using Orchard.ContentManagement; using Orchard.ContentManagement;
@@ -32,7 +33,7 @@ namespace Orchard.Core.Common.Drivers {
} }
protected override DriverResult Display(ContentPart part, TextField field, string displayType, dynamic shapeHelper) { protected override DriverResult Display(ContentPart part, TextField field, string displayType, dynamic shapeHelper) {
return ContentShape("Fields_Common_Text", GetDifferentiator(field, part), return ContentShape("Fields_Common_Text", GetDifferentiator(field, part),
() => { () => {
var settings = field.PartFieldDefinition.Settings.GetModel<TextFieldSettings>(); var settings = field.PartFieldDefinition.Settings.GetModel<TextFieldSettings>();
@@ -56,7 +57,7 @@ namespace Orchard.Core.Common.Drivers {
} }
protected override DriverResult Editor(ContentPart part, TextField field, IUpdateModel updater, dynamic shapeHelper) { protected override DriverResult Editor(ContentPart part, TextField field, IUpdateModel updater, dynamic shapeHelper) {
var viewModel = new TextFieldDriverViewModel { var viewModel = new TextFieldDriverViewModel {
Field = field, Field = field,
Text = field.Value, Text = field.Value,
@@ -64,7 +65,7 @@ namespace Orchard.Core.Common.Drivers {
ContentItem = part.ContentItem ContentItem = part.ContentItem
}; };
if(updater.TryUpdateModel(viewModel, GetPrefix(field, part), null, null)) { if (updater.TryUpdateModel(viewModel, GetPrefix(field, part), null, null)) {
if (viewModel.Settings.Required && string.IsNullOrWhiteSpace(viewModel.Text)) { if (viewModel.Settings.Required && string.IsNullOrWhiteSpace(viewModel.Text)) {
updater.AddModelError("Text", T("The field {0} is mandatory", T(field.DisplayName))); updater.AddModelError("Text", T("The field {0} is mandatory", T(field.DisplayName)));
return ContentShape("Fields_Common_Text_Edit", GetDifferentiator(field, part), return ContentShape("Fields_Common_Text_Edit", GetDifferentiator(field, part),
@@ -85,7 +86,8 @@ namespace Orchard.Core.Common.Drivers {
} }
protected override void Exporting(ContentPart part, TextField field, ExportContentContext context) { protected override void Exporting(ContentPart part, TextField field, ExportContentContext context) {
context.Element(field.FieldDefinition.Name + "." + field.Name).SetAttributeValue("Text", field.Value); if (!String.IsNullOrEmpty(field.Value))
context.Element(field.FieldDefinition.Name + "." + field.Name).SetAttributeValue("Text", field.Value);
} }
protected override void Describe(DescribeMembersContext context) { protected override void Describe(DescribeMembersContext context) {

View File

@@ -61,7 +61,8 @@ namespace Orchard.Fields.Drivers {
} }
protected override void Exporting(ContentPart part, BooleanField field, ExportContentContext context) { protected override void Exporting(ContentPart part, BooleanField field, ExportContentContext context) {
context.Element(field.FieldDefinition.Name + "." + field.Name).SetAttributeValue("Value", field.Value); if (field.Value.HasValue)
context.Element(field.FieldDefinition.Name + "." + field.Name).SetAttributeValue("Value", field.Value);
} }
protected override void Describe(DescribeMembersContext context) { protected override void Describe(DescribeMembersContext context) {

View File

@@ -167,7 +167,9 @@ namespace Orchard.Fields.Drivers {
} }
protected override void Exporting(ContentPart part, DateTimeField field, ExportContentContext context) { protected override void Exporting(ContentPart part, DateTimeField field, ExportContentContext context) {
context.Element(GetPrefix(field, part)).SetAttributeValue("Value", XmlConvert.ToString(field.Storage.Get<DateTime>(null), XmlDateTimeSerializationMode.Utc)); var value = field.Storage.Get<DateTime>(null);
if (value != DateTime.MinValue)
context.Element(GetPrefix(field, part)).SetAttributeValue("Value", XmlConvert.ToString(value, XmlDateTimeSerializationMode.Utc));
} }
protected override void Describe(DescribeMembersContext context) { protected override void Describe(DescribeMembersContext context) {

View File

@@ -4,6 +4,7 @@ using Orchard.ContentManagement.Handlers;
using Orchard.Fields.Settings; using Orchard.Fields.Settings;
using Orchard.Fields.Fields; using Orchard.Fields.Fields;
using Orchard.Localization; using Orchard.Localization;
using System;
namespace Orchard.Fields.Drivers { namespace Orchard.Fields.Drivers {
public class EnumerationFieldDriver : ContentFieldDriver<EnumerationField> { public class EnumerationFieldDriver : ContentFieldDriver<EnumerationField> {
@@ -51,7 +52,8 @@ namespace Orchard.Fields.Drivers {
} }
protected override void Exporting(ContentPart part, EnumerationField field, ExportContentContext context) { protected override void Exporting(ContentPart part, EnumerationField field, ExportContentContext context) {
context.Element(field.FieldDefinition.Name + "." + field.Name).SetAttributeValue("Value", field.Value); if (!String.IsNullOrEmpty(field.Value))
context.Element(field.FieldDefinition.Name + "." + field.Name).SetAttributeValue("Value", field.Value);
} }
protected override void Describe(DescribeMembersContext context) { protected override void Describe(DescribeMembersContext context) {

View File

@@ -56,7 +56,8 @@ namespace Orchard.Fields.Drivers {
} }
protected override void Exporting(ContentPart part, InputField field, ExportContentContext context) { protected override void Exporting(ContentPart part, InputField field, ExportContentContext context) {
context.Element(field.FieldDefinition.Name + "." + field.Name).SetAttributeValue("Value", field.Value); if (!String.IsNullOrEmpty(field.Value))
context.Element(field.FieldDefinition.Name + "." + field.Name).SetAttributeValue("Value", field.Value);
} }
protected override void Describe(DescribeMembersContext context) { protected override void Describe(DescribeMembersContext context) {

View File

@@ -62,9 +62,11 @@ namespace Orchard.Fields.Drivers {
} }
protected override void Exporting(ContentPart part, LinkField field, ExportContentContext context) { protected override void Exporting(ContentPart part, LinkField field, ExportContentContext context) {
context.Element(field.FieldDefinition.Name + "." + field.Name).SetAttributeValue("Text", field.Text); if (!String.IsNullOrEmpty(field.Text) && !String.IsNullOrEmpty(field.Value) && !String.IsNullOrEmpty(field.Target)) {
context.Element(field.FieldDefinition.Name + "." + field.Name).SetAttributeValue("Url", field.Value); context.Element(field.FieldDefinition.Name + "." + field.Name).SetAttributeValue("Text", field.Text);
context.Element(field.FieldDefinition.Name + "." + field.Name).SetAttributeValue("Target", field.Target); context.Element(field.FieldDefinition.Name + "." + field.Name).SetAttributeValue("Url", field.Value);
context.Element(field.FieldDefinition.Name + "." + field.Name).SetAttributeValue("Target", field.Target);
}
} }
protected override void Describe(DescribeMembersContext context) { protected override void Describe(DescribeMembersContext context) {

View File

@@ -104,7 +104,8 @@ namespace Orchard.Fields.Drivers {
} }
protected override void Exporting(ContentPart part, NumericField field, ExportContentContext context) { protected override void Exporting(ContentPart part, NumericField field, ExportContentContext context) {
context.Element(field.FieldDefinition.Name + "." + field.Name).SetAttributeValue("Value", !field.Value.HasValue ? String.Empty : field.Value.Value.ToString(CultureInfo.InvariantCulture)); if (field.Value.HasValue)
context.Element(field.FieldDefinition.Name + "." + field.Name).SetAttributeValue("Value", field.Value.Value.ToString(CultureInfo.InvariantCulture));
} }
protected override void Describe(DescribeMembersContext context) { protected override void Describe(DescribeMembersContext context) {

View File

@@ -108,10 +108,10 @@ namespace Orchard.Taxonomies.Drivers {
var appliedTerms = _taxonomyService.GetTermsForContentItem(part.ContentItem.Id, field.Name); var appliedTerms = _taxonomyService.GetTermsForContentItem(part.ContentItem.Id, field.Name);
// stores all content items associated to this field // stores all content items associated to this field
var termIdentities = appliedTerms.Select(x => Services.ContentManager.GetItemMetadata(x).Identity.ToString()) var termIdentities = appliedTerms.Select(x => Services.ContentManager.GetItemMetadata(x).Identity.ToString()).ToArray();
.ToArray();
context.Element(XmlConvert.EncodeLocalName(field.FieldDefinition.Name + "." + field.Name)).SetAttributeValue("Terms", String.Join(",", termIdentities)); if (termIdentities.Any())
context.Element(XmlConvert.EncodeLocalName(field.FieldDefinition.Name + "." + field.Name)).SetAttributeValue("Terms", String.Join(",", termIdentities));
} }
protected override void Importing(ContentPart part, TaxonomyField field, ImportContentContext context) { protected override void Importing(ContentPart part, TaxonomyField field, ImportContentContext context) {