using System.Globalization;
using System.Xml.XPath;
using Serilog.Events;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Dictionary;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.Strings;
using Umbraco.Cms.Core.Templates;
using Umbraco.Cms.Core.Xml;
using Umbraco.Extensions;
namespace Umbraco.Cms.Web.Common;
///
/// A helper class that provides many useful methods and functionality for using Umbraco in templates
///
///
/// This object is a request based lifetime
///
public class UmbracoHelper
{
private readonly IUmbracoComponentRenderer _componentRenderer;
private readonly ICultureDictionaryFactory _cultureDictionaryFactory;
private readonly IPublishedContentQuery _publishedContentQuery;
private ICultureDictionary? _cultureDictionary;
private IPublishedContent? _currentPage;
#region Constructors
///
/// Initializes a new instance of .
///
///
///
///
/// Sets the current page to the context's published content request's content item.
public UmbracoHelper(
ICultureDictionaryFactory cultureDictionary,
IUmbracoComponentRenderer componentRenderer,
IPublishedContentQuery publishedContentQuery)
{
_cultureDictionaryFactory = cultureDictionary ?? throw new ArgumentNullException(nameof(cultureDictionary));
_componentRenderer = componentRenderer ?? throw new ArgumentNullException(nameof(componentRenderer));
_publishedContentQuery =
publishedContentQuery ?? throw new ArgumentNullException(nameof(publishedContentQuery));
}
///
/// Initializes a new empty instance of .
///
/// For tests - nothing is initialized.
#pragma warning disable CS8618
internal UmbracoHelper()
#pragma warning restore CS8618
{
}
#endregion
///
/// Gets (or sets) the current item assigned to the UmbracoHelper.
///
///
///
/// Note that this is the assigned IPublishedContent item to the
/// UmbracoHelper, this is not necessarily the Current IPublishedContent
/// item being rendered that is assigned to the UmbracoContext.
/// This IPublishedContent object is contextual to the current UmbracoHelper instance.
///
///
/// In some cases accessing this property will throw an exception if
/// there is not IPublishedContent assigned to the Helper this will
/// only ever happen if the Helper is constructed via DI during a non front-end request.
///
///
///
/// Thrown if the
/// UmbracoHelper is constructed with an UmbracoContext and it is not a
/// front-end request.
///
public IPublishedContent AssignedContentItem
{
get
{
if (_currentPage != null)
{
return _currentPage;
}
throw new InvalidOperationException(
$"Cannot return the {nameof(IPublishedContent)} because the {nameof(UmbracoHelper)} was not constructed with an {nameof(IPublishedContent)}.");
}
set => _currentPage = value;
}
///
/// Renders the template for the specified pageId and an optional altTemplateId
///
/// The content id
/// If not specified, will use the template assigned to the node
public async Task RenderTemplateAsync(int contentId, int? altTemplateId = null)
=> await _componentRenderer.RenderTemplateAsync(contentId, altTemplateId);
#region RenderMacro
///
/// Renders the macro with the specified alias.
///
/// The alias.
///
public async Task RenderMacroAsync(string alias)
=> await _componentRenderer.RenderMacroAsync(AssignedContentItem.Id, alias, null);
///
/// Renders the macro with the specified alias, passing in the specified parameters.
///
/// The alias.
/// The parameters.
///
public async Task RenderMacroAsync(string alias, object parameters)
=> await _componentRenderer.RenderMacroAsync(AssignedContentItem.Id, alias, parameters.ToDictionary());
///
/// Renders the macro with the specified alias, passing in the specified parameters.
///
/// The alias.
/// The parameters.
///
public async Task RenderMacroAsync(string alias, IDictionary parameters)
=> await _componentRenderer.RenderMacroAsync(AssignedContentItem.Id, alias, parameters);
#endregion
#region Dictionary
///
/// Returns the dictionary value for the key specified
///
///
///
public string? GetDictionaryValue(string key) => CultureDictionary[key];
///
/// Returns the dictionary value for the key specified, and if empty returns the specified default fall back value
///
/// key of dictionary item
/// the specific culture on which the result well be back upon
///
public string? GetDictionaryValue(string key, CultureInfo specificCulture)
{
_cultureDictionary = _cultureDictionaryFactory.CreateDictionary(specificCulture);
return GetDictionaryValue(key);
}
///
/// Returns the dictionary value for the key specified, and if empty returns the specified default fall back value
///
/// key of dictionary item
/// fall back text if dictionary item is empty - Name altText to match Umbraco.Field
///
public string GetDictionaryValueOrDefault(string key, string defaultValue)
{
var dictionaryValue = GetDictionaryValue(key);
if (string.IsNullOrWhiteSpace(dictionaryValue))
{
dictionaryValue = defaultValue;
}
return dictionaryValue;
}
///
/// Returns the dictionary value for the key specified, and if empty returns the specified default fall back value
///
/// key of dictionary item
/// fall back text if dictionary item is empty - Name altText to match Umbraco.Field
///
[Obsolete("Use GetDictionaryValueOrDefault instead, scheduled for removal in v14.")]
public string GetDictionaryValue(string key, string altText)
{
var dictionaryValue = GetDictionaryValue(key);
if (string.IsNullOrWhiteSpace(dictionaryValue))
{
dictionaryValue = altText;
}
return dictionaryValue;
}
///
/// Returns the dictionary value for the key specified, and if empty returns the specified default fall back value
///
/// key of dictionary item
/// the specific culture on which the result well be back upon
/// fall back text if dictionary item is empty - Name altText to match Umbraco.Field
///
public string GetDictionaryValueOrDefault(string key, CultureInfo specificCulture, string defaultValue)
{
_cultureDictionary = _cultureDictionaryFactory.CreateDictionary(specificCulture);
var dictionaryValue = GetDictionaryValue(key);
if (string.IsNullOrWhiteSpace(dictionaryValue))
{
dictionaryValue = defaultValue;
}
return dictionaryValue;
}
///
/// Returns the ICultureDictionary for access to dictionary items
///
public ICultureDictionary CultureDictionary => _cultureDictionary ??= _cultureDictionaryFactory.CreateDictionary();
#endregion
#region Content
///
/// Gets a content item from the cache.
///
/// The unique identifier, or the key, of the content item.
/// The content, or null of the content item is not in the cache.
public IPublishedContent? Content(object id) => ContentForObject(id);
private IPublishedContent? ContentForObject(object id) => _publishedContentQuery.Content(id);
[Obsolete("The current implementation of XPath is suboptimal and will be removed entirely in a future version. Scheduled for removal in v14")]
public IPublishedContent? ContentSingleAtXPath(string xpath, params XPathVariable[] vars) =>
_publishedContentQuery.ContentSingleAtXPath(xpath, vars);
///
/// Gets a content item from the cache.
///
/// The unique identifier of the content item.
/// The content, or null of the content item is not in the cache.
public IPublishedContent? Content(int id) => _publishedContentQuery.Content(id);
///
/// Gets a content item from the cache.
///
/// The key of the content item.
/// The content, or null of the content item is not in the cache.
public IPublishedContent? Content(Guid id) => _publishedContentQuery.Content(id);
///
/// Gets a content item from the cache.
///
/// The unique identifier, or the key, of the content item.
/// The content, or null of the content item is not in the cache.
public IPublishedContent? Content(string id) => _publishedContentQuery.Content(id);
public IPublishedContent? Content(Udi id) => _publishedContentQuery.Content(id);
///
/// Gets content items from the cache.
///
/// The unique identifiers, or the keys, of the content items.
/// The content items that were found in the cache.
/// Does not support mixing identifiers and keys.
public IEnumerable Content(params object[] ids) => _publishedContentQuery.Content(ids);
///
/// Gets the contents corresponding to the identifiers.
///
/// The content identifiers.
/// The existing contents corresponding to the identifiers.
/// If an identifier does not match an existing content, it will be missing in the returned value.
public IEnumerable Content(params Udi[] ids) => _publishedContentQuery.Content(ids);
///
/// Gets the contents corresponding to the identifiers.
///
/// The content identifiers.
/// The existing contents corresponding to the identifiers.
/// If an identifier does not match an existing content, it will be missing in the returned value.
public IEnumerable Content(params GuidUdi[] ids) => _publishedContentQuery.Content(ids);
///
/// Gets content items from the cache.
///
/// The unique identifiers of the content items.
/// The content items that were found in the cache.
public IEnumerable Content(params int[] ids) => _publishedContentQuery.Content(ids);
///
/// Gets content items from the cache.
///
/// The keys of the content items.
/// The content items that were found in the cache.
public IEnumerable Content(params Guid[] ids) => _publishedContentQuery.Content(ids);
///
/// Gets content items from the cache.
///
/// The unique identifiers, or the keys, of the content items.
/// The content items that were found in the cache.
/// Does not support mixing identifiers and keys.
public IEnumerable Content(params string[] ids) => _publishedContentQuery.Content(ids);
///
/// Gets the contents corresponding to the identifiers.
///
/// The content identifiers.
/// The existing contents corresponding to the identifiers.
/// If an identifier does not match an existing content, it will be missing in the returned value.
public IEnumerable Content(IEnumerable ids) => _publishedContentQuery.Content(ids);
///
/// Gets the contents corresponding to the identifiers.
///
/// The content identifiers.
/// The existing contents corresponding to the identifiers.
/// If an identifier does not match an existing content, it will be missing in the returned value.
public IEnumerable Content(IEnumerable ids) => _publishedContentQuery.Content(ids);
///
/// Gets the contents corresponding to the identifiers.
///
/// The content identifiers.
/// The existing contents corresponding to the identifiers.
/// If an identifier does not match an existing content, it will be missing in the returned value.
public IEnumerable Content(IEnumerable ids) => _publishedContentQuery.Content(ids);
///
/// Gets the contents corresponding to the identifiers.
///
/// The content identifiers.
/// The existing contents corresponding to the identifiers.
/// If an identifier does not match an existing content, it will be missing in the returned value.
public IEnumerable Content(IEnumerable ids) => _publishedContentQuery.Content(ids);
///
/// Gets the contents corresponding to the identifiers.
///
/// The content identifiers.
/// The existing contents corresponding to the identifiers.
/// If an identifier does not match an existing content, it will be missing in the returned value.
public IEnumerable Content(IEnumerable ids) => _publishedContentQuery.Content(ids);
[Obsolete("The current implementation of this method is suboptimal and will be removed entirely in a future version. Scheduled for removal in v14")]
public IEnumerable ContentAtXPath(string xpath, params XPathVariable[] vars) =>
_publishedContentQuery.ContentAtXPath(xpath, vars);
[Obsolete("The current implementation of this method is suboptimal and will be removed entirely in a future version. Scheduled for removal in v14")]
public IEnumerable ContentAtXPath(XPathExpression xpath, params XPathVariable[] vars) =>
_publishedContentQuery.ContentAtXPath(xpath, vars);
public IEnumerable ContentAtRoot() => _publishedContentQuery.ContentAtRoot();
#endregion
#region Media
public IPublishedContent? Media(Udi id) => _publishedContentQuery.Media(id);
public IPublishedContent? Media(Guid id) => _publishedContentQuery.Media(id);
///
/// Overloaded method accepting an 'object' type
///
///
///
///
/// We accept an object type because GetPropertyValue now returns an 'object', we still want to allow people to pass
/// this result in to this method.
/// This method will throw an exception if the value is not of type int or string.
///
public IPublishedContent? Media(object id) => MediaForObject(id);
private IPublishedContent? MediaForObject(object id) => _publishedContentQuery.Media(id);
public IPublishedContent? Media(int id) => _publishedContentQuery.Media(id);
public IPublishedContent? Media(string id) => _publishedContentQuery.Media(id);
///
/// Gets the medias corresponding to the identifiers.
///
/// The media identifiers.
/// The existing medias corresponding to the identifiers.
/// If an identifier does not match an existing media, it will be missing in the returned value.
public IEnumerable Media(params object[] ids) => _publishedContentQuery.Media(ids);
///
/// Gets the medias corresponding to the identifiers.
///
/// The media identifiers.
/// The existing medias corresponding to the identifiers.
/// If an identifier does not match an existing media, it will be missing in the returned value.
public IEnumerable Media(params int[] ids) => _publishedContentQuery.Media(ids);
///
/// Gets the medias corresponding to the identifiers.
///
/// The media identifiers.
/// The existing medias corresponding to the identifiers.
/// If an identifier does not match an existing media, it will be missing in the returned value.
public IEnumerable Media(params string[] ids) => _publishedContentQuery.Media(ids);
///
/// Gets the medias corresponding to the identifiers.
///
/// The media identifiers.
/// The existing medias corresponding to the identifiers.
/// If an identifier does not match an existing media, it will be missing in the returned value.
public IEnumerable Media(params Udi[] ids) => _publishedContentQuery.Media(ids);
///
/// Gets the medias corresponding to the identifiers.
///
/// The media identifiers.
/// The existing medias corresponding to the identifiers.
/// If an identifier does not match an existing media, it will be missing in the returned value.
public IEnumerable Media(params GuidUdi[] ids) => _publishedContentQuery.Media(ids);
///
/// Gets the medias corresponding to the identifiers.
///
/// The media identifiers.
/// The existing medias corresponding to the identifiers.
/// If an identifier does not match an existing media, it will be missing in the returned value.
public IEnumerable Media(IEnumerable ids) => _publishedContentQuery.Media(ids);
///
/// Gets the medias corresponding to the identifiers.
///
/// The media identifiers.
/// The existing medias corresponding to the identifiers.
/// If an identifier does not match an existing media, it will be missing in the returned value.
public IEnumerable Media(IEnumerable ids) => _publishedContentQuery.Media(ids);
///
/// Gets the medias corresponding to the identifiers.
///
/// The media identifiers.
/// The existing medias corresponding to the identifiers.
/// If an identifier does not match an existing media, it will be missing in the returned value.
public IEnumerable Media(IEnumerable ids) => _publishedContentQuery.Media(ids);
///
/// Gets the medias corresponding to the identifiers.
///
/// The media identifiers.
/// The existing medias corresponding to the identifiers.
/// If an identifier does not match an existing media, it will be missing in the returned value.
public IEnumerable Media(IEnumerable ids) => _publishedContentQuery.Media(ids);
///
/// Gets the medias corresponding to the identifiers.
///
/// The media identifiers.
/// The existing medias corresponding to the identifiers.
/// If an identifier does not match an existing media, it will be missing in the returned value.
public IEnumerable Media(IEnumerable ids) => _publishedContentQuery.Media(ids);
public IEnumerable MediaAtRoot() => _publishedContentQuery.MediaAtRoot();
#endregion
}