using System; using System.Collections.Generic; using System.Linq; using Umbraco.Core; using Umbraco.Web.Composing; using Umbraco.Core.Models.PublishedContent; namespace Umbraco.Web { /// /// Provides extension methods for IPublishedElement. /// public static class PublishedElementExtensions { // lots of debates about accessing dependencies (IPublishedValueFallback) from extension methods, ranging // from "don't do it" i.e. if an extension method is relying on dependencies, a proper service should be // created instead, to discussing method injection vs service locator vs other subtleties, see for example // this post http://marisks.net/2016/12/19/dependency-injection-with-extension-methods/ // // point is, we do NOT want a service, we DO want to write model.Value("alias", "fr-FR") and hit // fallback somehow - which pretty much rules out method injection, and basically anything but service // locator - bah, let's face it, it works // // besides, for tests, Current support setting a fallback without even a container // // Update to this comment 8/2/2020: issue as been ameliorated by creating extensions methods in Umbraco.Core // that accept the dependencies as arguments for many of these extension methods, and can be used within the Umbraco code-base. // For site developers, the "friendly" extension methods using service location have been maintained, delegating to the ones that // take the dependencies as parameters. private static IPublishedValueFallback PublishedValueFallback => Current.PublishedValueFallback; #region Value /// /// Gets the value of a content's property identified by its alias. /// /// The content. /// The property alias. /// The variation language. /// The variation segment. /// Optional fallback strategy. /// The default value. /// The value of the content's property identified by the alias, if it exists, otherwise a default value. /// /// The value comes from IPublishedProperty field Value ie it is suitable for use when rendering content. /// If no property with the specified alias exists, or if the property has no value, returns . /// If eg a numeric property wants to default to 0 when value source is empty, this has to be done in the converter. /// The alias is case-insensitive. /// public static object Value(this IPublishedElement content, string alias, string culture = null, string segment = null, Fallback fallback = default, object defaultValue = default) { return content.Value(PublishedValueFallback, alias, culture, segment, fallback, defaultValue); } #endregion #region Value /// /// Gets the value of a content's property identified by its alias, converted to a specified type. /// /// The target property type. /// The content. /// The property alias. /// The variation language. /// The variation segment. /// Optional fallback strategy. /// The default value. /// The value of the content's property identified by the alias, converted to the specified type. /// /// The value comes from IPublishedProperty field Value ie it is suitable for use when rendering content. /// If no property with the specified alias exists, or if the property has no value, or if it could not be converted, returns default(T). /// If eg a numeric property wants to default to 0 when value source is empty, this has to be done in the converter. /// The alias is case-insensitive. /// public static T Value(this IPublishedElement content, string alias, string culture = null, string segment = null, Fallback fallback = default, T defaultValue = default) { return content.Value(PublishedValueFallback, alias, culture, segment, fallback, defaultValue); } #endregion #region IsSomething /// /// Gets a value indicating whether the content is visible. /// /// The content. /// A value indicating whether the content is visible. /// A content is not visible if it has an umbracoNaviHide property with a value of "1". Otherwise, /// the content is visible. public static bool IsVisible(this IPublishedElement content) { return content.IsVisible(PublishedValueFallback); } #endregion } }