2015-12-19 20:49:13 +01:00
|
|
|
|
using Orchard.ContentManagement;
|
|
|
|
|
using Orchard.ContentManagement.Drivers;
|
|
|
|
|
using Orchard.Core.Common.Fields;
|
|
|
|
|
using Orchard.Core.Common.Settings;
|
2016-03-25 03:29:19 +01:00
|
|
|
|
using Orchard.Localization;
|
2015-12-19 20:49:13 +01:00
|
|
|
|
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;
|
2016-03-25 03:29:19 +01:00
|
|
|
|
T = NullLocalizer.Instance;
|
2015-12-19 20:49:13 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-25 03:29:19 +01:00
|
|
|
|
public Localizer T { get; set; }
|
|
|
|
|
|
2015-12-19 20:49:13 +01:00
|
|
|
|
protected override DriverResult Editor(ContentPart part, TextField field, IUpdateModel updater, dynamic shapeHelper) {
|
|
|
|
|
var settings = field.PartFieldDefinition.Settings.GetModel<TextFieldSettings>();
|
|
|
|
|
|
2016-03-25 03:29:19 +01:00
|
|
|
|
if (!String.IsNullOrWhiteSpace(settings.DefaultValue) && (String.IsNullOrWhiteSpace(field.Value) || field.Value.Equals(settings.DefaultValue))) {
|
2015-12-19 20:49:13 +01:00
|
|
|
|
field.Value = _tokenizer.Replace(settings.DefaultValue, new Dictionary<string, object> { { "Content", part.ContentItem } });
|
2016-03-25 03:29:19 +01:00
|
|
|
|
|
|
|
|
|
if (settings.Required && String.IsNullOrWhiteSpace(field.Value)) {
|
|
|
|
|
updater.AddModelError("Text", T("The field {0} is mandatory", T(field.DisplayName)));
|
|
|
|
|
}
|
2015-12-19 20:49:13 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-03-09 01:49:13 +01:00
|
|
|
|
}
|