using System; using Umbraco.Core.Exceptions; namespace Umbraco.Core.PropertyEditors { /// /// Marks a ConfigurationEditor property as a configuration field, and a class as a configuration field type. /// /// Properties marked with this attribute are discovered as fields. [AttributeUsage(AttributeTargets.Property | AttributeTargets.Class)] public class ConfigurationFieldAttribute : Attribute { private Type _type; /// /// Initializes a new instance of the class. /// public ConfigurationFieldAttribute(Type type) { Type = type; } /// /// Initializes a new instance of the class. /// /// The unique identifier of the field. /// The friendly name of the field. /// The view to use to render the field editor. public ConfigurationFieldAttribute(string key, string name, string view) { if (string.IsNullOrWhiteSpace(key)) throw new ArgumentNullOrEmptyException(nameof(key)); Key = key; if (string.IsNullOrWhiteSpace(name)) throw new ArgumentNullOrEmptyException(nameof(name)); Name = name; if (string.IsNullOrWhiteSpace(view)) throw new ArgumentNullOrEmptyException(nameof(view)); View = view; } /// /// Initializes a new instance of the class. /// /// The friendly name of the field. /// The view to use to render the field editor. /// When no key is specified, the will derive a key /// from the name of the property marked with this attribute. public ConfigurationFieldAttribute(string name, string view) { if (string.IsNullOrWhiteSpace(name)) throw new ArgumentNullOrEmptyException(nameof(name)); Name = name; if (string.IsNullOrWhiteSpace(view)) throw new ArgumentNullOrEmptyException(nameof(view)); View = view; } /// /// Gets or sets the key of the field. /// /// When null or empty, the should derive a key /// from the name of the property marked with this attribute. public string Key { get; } /// /// Gets the friendly name of the field. /// public string Name { get; } /// /// Gets or sets the view to use to render the field editor. /// public string View { get; } /// /// Gets or sets the description of the field. /// public string Description { get; set; } /// /// Gets or sets a value indicating whether the field editor should be displayed without its label. /// public bool HideLabel { get => HideLabelSettable.ValueOrDefault(false); set => HideLabelSettable.Set(value); } /// /// Gets the settable underlying . /// public Settable HideLabelSettable { get; } = new Settable(); /// /// Gets or sets the type of the field. /// /// /// By default, fields are created as instances, /// unless specified otherwise through this property. /// The specified type must inherit from . /// public Type Type { get => _type; set { if (!typeof(ConfigurationField).IsAssignableFrom(value)) throw new ArgumentException("Type must inherit from ConfigurationField.", nameof(value)); _type = value; } } } }