mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-21 03:14:10 +08:00
38 lines
1.5 KiB
C#
38 lines
1.5 KiB
C#
using Orchard.ContentManagement;
|
|
using Orchard.ContentManagement.Drivers;
|
|
using Orchard.Core.Common.Fields;
|
|
using Orchard.Core.Common.Settings;
|
|
using Orchard.Localization;
|
|
using Orchard.Tokens;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace Orchard.Fields.Drivers {
|
|
// The original driver of the TextField is in Orchard.Core, where tokenization can not be used.
|
|
// This driver was added so the default value of the TextField can be tokenized.
|
|
public class TextFieldDriver : ContentFieldDriver<TextField> {
|
|
private readonly ITokenizer _tokenizer;
|
|
|
|
public TextFieldDriver(ITokenizer tokenizer) {
|
|
_tokenizer = tokenizer;
|
|
T = NullLocalizer.Instance;
|
|
}
|
|
|
|
public Localizer T { get; set; }
|
|
|
|
protected override DriverResult Editor(ContentPart part, TextField field, IUpdateModel updater, dynamic shapeHelper) {
|
|
var settings = field.PartFieldDefinition.Settings.GetModel<TextFieldSettings>();
|
|
|
|
if (!String.IsNullOrWhiteSpace(settings.DefaultValue) && (String.IsNullOrWhiteSpace(field.Value) || field.Value.Equals(settings.DefaultValue))) {
|
|
field.Value = _tokenizer.Replace(settings.DefaultValue, new Dictionary<string, object> { { "Content", part.ContentItem } });
|
|
|
|
if (settings.Required && String.IsNullOrWhiteSpace(field.Value)) {
|
|
updater.AddModelError("Text", T("The field {0} is mandatory", T(field.DisplayName)));
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|
|
}
|