Files
Umbraco-CMS/src/Umbraco.Web/PropertyEditors/ValueConverters/TextStringValueConverter.cs

62 lines
2.5 KiB
C#
Raw Normal View History

2018-06-29 19:52:40 +02:00
using System;
using System.Linq;
using Umbraco.Core;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Core.PropertyEditors;
2019-02-14 09:49:45 +01:00
using Umbraco.Web.Composing;
2018-06-29 19:52:40 +02:00
using Umbraco.Web.Templates;
namespace Umbraco.Web.PropertyEditors.ValueConverters
{
[DefaultPropertyValueConverter]
public class TextStringValueConverter : PropertyValueConverterBase
{
2019-10-21 23:53:14 +11:00
public TextStringValueConverter(InternalLinkParser internalLinkParser, UrlParser urlParser)
{
_internalLinkParser = internalLinkParser;
2019-10-21 23:53:14 +11:00
_urlParser = urlParser;
}
2018-06-29 19:52:40 +02:00
private static readonly string[] PropertyTypeAliases =
{
Constants.PropertyEditors.Aliases.TextBox,
Constants.PropertyEditors.Aliases.TextArea
};
private readonly InternalLinkParser _internalLinkParser;
2019-10-21 23:53:14 +11:00
private readonly UrlParser _urlParser;
2018-06-29 19:52:40 +02:00
2019-04-15 17:14:45 +02:00
public override bool IsConverter(IPublishedPropertyType propertyType)
2018-06-29 19:52:40 +02:00
=> PropertyTypeAliases.Contains(propertyType.EditorAlias);
2019-04-15 17:14:45 +02:00
public override Type GetPropertyValueType(IPublishedPropertyType propertyType)
2018-06-29 19:52:40 +02:00
=> typeof (string);
2019-04-15 17:14:45 +02:00
public override PropertyCacheLevel GetPropertyCacheLevel(IPublishedPropertyType propertyType)
2018-06-29 19:52:40 +02:00
=> PropertyCacheLevel.Snapshot;
2019-04-15 17:14:45 +02:00
public override object ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPropertyType propertyType, object source, bool preview)
2018-06-29 19:52:40 +02:00
{
if (source == null) return null;
var sourceString = source.ToString();
// ensures string is parsed for {localLink} and urls are resolved correctly
2019-10-21 23:53:14 +11:00
sourceString = _internalLinkParser.EnsureInternalLinks(sourceString, preview);
sourceString = _urlParser.EnsureUrls(sourceString);
2018-06-29 19:52:40 +02:00
return sourceString;
}
2019-04-15 17:14:45 +02:00
public override object ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object inter, bool preview)
2018-06-29 19:52:40 +02:00
{
// source should come from ConvertSource and be a string (or null) already
return inter ?? string.Empty;
}
2019-04-15 17:14:45 +02:00
public override object ConvertIntermediateToXPath(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object inter, bool preview)
2018-06-29 19:52:40 +02:00
{
// source should come from ConvertSource and be a string (or null) already
return inter;
}
}
}