private void UpdateSinglePart(ContentItem ci, KeyValuePair <string, string> prop, ContentField cf) { // var cf = profile.Fields.Where(x => x.Name == prop.Key).FirstOrDefault(); if (cf != null) { if (cf.GetType() == typeof(DateTime)) { ((dynamic)cf).DateTime = prop.Value; } else if (cf.GetType() == typeof(TaxonomyField)) { UpdateTaxonomyField(ci, cf as TaxonomyField, prop.Value); } else if ((cf.GetType() == typeof(MediaLibraryPickerField)) || (cf.GetType() == typeof(ContentPickerField))) { Errors.Add(string.Format("Property {0} of type {1} cannot be updated by CSV import.", prop.Key, cf.GetType().Name)); } else { ((dynamic)cf).Value = prop.Value; } } else { Errors.Add(string.Format("Field \"{0}\" not found in Contact.", prop.Key)); } }
public RenderFragment CreateFieldComponent(ContentField contentField, ContentFieldOptions options) { if (_cacheFieldView.TryGetValue(contentField.GetType(), out Type viewType)) { return(builder => { builder.OpenComponent(0, viewType); builder.AddAttribute(0, nameof(IFieldComponent.Field), contentField); builder.AddAttribute(1, nameof(IFieldComponent.Options), options); builder.CloseComponent(); }); } else { return(builder => { builder.OpenElement(0, "p"); builder.AddContent(0, "no field available"); builder.CloseElement(); }); } }
private JProperty SerializeObjectField(ContentField field, int actualLevel, int parentContentId, ContentItem item = null) { var fieldObject = new JObject(); fieldObject.Add("ContentFieldClassName", JToken.FromObject(field.FieldDefinition.Name)); fieldObject.Add("ContentFieldTechnicalName", JToken.FromObject(field.Name)); fieldObject.Add("ContentFieldDisplayName", JToken.FromObject(field.DisplayName)); if (field.FieldDefinition.Name == "EnumerationField") { var enumField = (EnumerationField)field; string[] selected = enumField.SelectedValues; string[] options = enumField.PartFieldDefinition.Settings["EnumerationFieldSettings.Options"].Split(new string[] { Environment.NewLine }, StringSplitOptions.None); fieldObject.Add("Options", JToken.FromObject(options)); fieldObject.Add("SelectedValues", JToken.FromObject(selected)); } else { var properties = field.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(prop => !_skipFieldTypes.Contains(prop.Name) //skip ); foreach (var property in properties) { try { if (!_skipFieldProperties.Contains(property.Name) && !CustomAttributeData.GetCustomAttributes(property).Any(x => x.AttributeType == typeof(JsonIgnoreAttribute))) { object val = property.GetValue(field, BindingFlags.GetProperty, null, null, null); if (val != null) { PopulateJObject(ref fieldObject, property, val, _skipFieldProperties, actualLevel, item?.Id ?? 0); } } } catch { } } } return(new JProperty(field.Name + field.FieldDefinition.Name, fieldObject)); }
private ResourceType GetOrCreateContentField(ContentField contentField) { var contentFieldName = contentField.Name; if (this._resourceTypes.ContainsKey(contentFieldName)) { return(this._resourceTypes[contentFieldName]); } var contentFieldType = contentField.GetType(); var resourceTypeContentField = new ResourceType( contentFieldType, ResourceTypeKind.ComplexType, null, (this as IDataServiceMetadataProvider).ContainerNamespace, contentFieldName, false); resourceTypeContentField.CanReflectOnInstanceType = false; resourceTypeContentField.AddResourcePropertiesFromInstanceType(contentFieldType); contentFieldType .GetProperties() .Where(property => property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(IEnumerable <>)) .ToList() .ForEach(property => { var orchardType = property.PropertyType.GenericTypeArguments.Single(); if (orchardType.BaseType.IsGenericType && orchardType.BaseType.GetGenericTypeDefinition() == typeof(ContentPart <>)) { var resourceType = this.GetOrCreateContentPart(orchardType, orchardType.Name); resourceType.CustomState = property; resourceTypeContentField.AddResourcePropertyFromInstanceCollectionResourceType(resourceType); } }); this._resourceTypes[contentFieldName] = resourceTypeContentField; return(resourceTypeContentField); }
private JProperty SerializeField(ContentField field, int actualLevel, ContentItem item = null) { var fieldObject = new JObject(); if (field.FieldDefinition.Name == "EnumerationField") { var enumField = (EnumerationField)field; string[] selected = enumField.SelectedValues; string[] options = enumField.PartFieldDefinition.Settings["EnumerationFieldSettings.Options"].Split(new string[] { Environment.NewLine }, StringSplitOptions.None); fieldObject.Add("Options", JToken.FromObject(options)); fieldObject.Add("SelectedValues", JToken.FromObject(selected)); } else if (field.FieldDefinition.Name == "TaxonomyField") { SerializeTaxonomyField((TaxonomyField)field, actualLevel, ref fieldObject, item); } else if (field.FieldDefinition.Name == "NumericField") { var numericField = field as NumericField; object val = 0; if (numericField.Value.HasValue) { val = numericField.Value.Value; } FormatValue(ref val); return(new JProperty(field.Name + field.FieldDefinition.Name, val)); } else if (field.FieldDefinition.Name == "TextField") { var textField = field as TextField; object val = textField.Value; if (val != null) { if (textField.PartFieldDefinition.Settings.ContainsKey("TextFieldSettings.Flavor")) { var flavor = textField.PartFieldDefinition.Settings["TextFieldSettings.Flavor"]; // markdownFilter acts only if flavor is "markdown" val = _markdownFilter.ProcessContent(val.ToString(), flavor); } FormatValue(ref val); } return(new JProperty(field.Name + field.FieldDefinition.Name, val)); } else if (field.FieldDefinition.Name == "InputField") { var inputField = field as InputField; object val = inputField.Value; FormatValue(ref val); return(new JProperty(field.Name + field.FieldDefinition.Name, val)); } else if (field.FieldDefinition.Name == "BooleanField") { var booleanField = field as BooleanField; object val = false; if (booleanField.Value.HasValue) { val = booleanField.Value.Value; } FormatValue(ref val); return(new JProperty(field.Name + field.FieldDefinition.Name, val)); } else if (field.FieldDefinition.Name == "DateTimeField") { var dateTimeField = field as DateTimeField; object val = dateTimeField.DateTime; FormatValue(ref val); return(new JProperty(field.Name + field.FieldDefinition.Name, val)); } else { var properties = field.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(prop => !_skipFieldTypes.Contains(prop.Name) //skip ); foreach (var property in properties) { try { if (!_skipFieldProperties.Contains(property.Name)) { object val = property.GetValue(field, BindingFlags.GetProperty, null, null, null); if (val != null) { PopulateJObject(ref fieldObject, property, val, _skipFieldProperties, actualLevel); } } } catch { } } } return(new JProperty(field.Name + field.FieldDefinition.Name, fieldObject)); }
public static BsonValue ToBsonValue(this ContentField contentField) { IFieldSerializer fieldSerializer = MongoFieldManager.Default.GetByType(contentField.GetType()); return(fieldSerializer.Write(contentField)); }