* Run code cleanup * Start manual cleanup after dotnet format * Finish up manual pass * Fix up missed warnings * Fix after merge * Update src/Umbraco.Core/Cache/ContentTypeCacheRefresher.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Cache/DataTypeCacheRefresher.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Cache/DeepCloneAppCache.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Cache/DomainCacheRefresher.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Cache/FastDictionaryAppCacheBase.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Cache/FastDictionaryAppCacheBase.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Cache/FastDictionaryAppCacheBase.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Cache/FastDictionaryAppCacheBase.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Cache/FastDictionaryAppCacheBase.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Cache/MacroCacheRefresher.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Cache/MediaCacheRefresher.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Cache/MemberCacheRefresher.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Cache/MemberGroupCacheRefresher.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Composing/OrderedCollectionBuilderBase.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Configuration/Models/RequestHandlerSettings.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Composing/SetCollectionBuilderBase.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Composing/WeightedCollectionBuilderBase.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/ContentApps/ContentAppFactoryCollectionBuilder.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Dashboards/DashboardCollectionBuilder.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Extensions/StringExtensions.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Extensions/StringExtensions.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Extensions/StringExtensions.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Extensions/StringExtensions.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Extensions/StringExtensions.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Extensions/StringExtensions.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Extensions/StringExtensions.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/ContentTypeRefreshedNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/ContentTypeSavedNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/ContentTypeSavingNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/DictionaryCacheRefresherNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/DataTypeCacheRefresherNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/ContentUnpublishingNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/DictionaryItemDeletingNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/DictionaryItemSavedNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/DictionaryItemSavingNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/DomainCacheRefresherNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/LanguageCacheRefresherNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MacroCacheRefresherNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MediaCacheRefresherNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MediaEmptiedRecycleBinNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MediaEmptyingRecycleBinNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MediaMovedNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MediaMovedToRecycleBinNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Extensions/StringExtensions.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Extensions/StringExtensions.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Extensions/StringExtensions.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Extensions/StringExtensions.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Extensions/StringExtensions.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Extensions/StringExtensions.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MediaMovedToRecycleBinNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MediaMovingNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MediaMovingToRecycleBinNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MediaTreeChangeNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MediaTreeChangeNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MediaTypeChangedNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MediaTypeDeletingNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MediaTypeMovedNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MediaTypeMovingNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MediaTypeMovingNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MediaTypeRefreshedNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MemberCacheRefresherNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MemberGroupCacheRefresherNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MemberGroupDeletingNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MemberGroupSavedNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MemberGroupSavingNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MemberTypeChangedNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MemberTypeDeletedNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MemberTypeDeletingNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MemberTypeMovedNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MemberTypeMovedNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MemberTypeMovingNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MemberTypeMovingNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MemberTypeRefreshedNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MemberTypeSavingNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Extensions/StringExtensions.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Extensions/UriExtensions.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MovedToRecycleBinNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MovedToRecycleBinNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MovingToRecycleBinNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MovingToRecycleBinNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/PartialViewDeletingNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Extensions/UriExtensions.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/IO/PhysicalFileSystem.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/IO/PhysicalFileSystem.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/IO/ShadowWrapper.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Logging/DisposableTimer.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Install/InstallSteps/UpgradeStep.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Mapping/MapDefinitionCollectionBuilder.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Media/EmbedProviders/EmbedProvidersCollectionBuilder.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/PartialViewSavedNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Models/ContentEditing/ContentVariationDisplay.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/PartialViewSavingNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/PublicAccessCacheRefresherNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/PublicAccessEntryDeletedNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/PublicAccessEntryDeletingNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/PublicAccessEntryDeletingNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/PublicAccessEntrySavedNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/PublicAccessEntrySavingNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/PublicAccessEntrySavingNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/RelationTypeCacheRefresherNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/RelationTypeDeletingNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/RelationTypeSavedNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/RelationTypeSavingNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/StatefulNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/StylesheetDeletingNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/StylesheetSavingNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Models/DeepCloneHelper.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Models/File.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Models/PropertyGroupExtensions.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Models/PropertyGroupExtensions.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Fix based on review * Fix after merge Signed-off-by: Zeegaan <nge@umbraco.dk> Co-authored-by: Nikolaj Geisle <niko737@edu.ucl.dk> Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> Co-authored-by: Zeegaan <nge@umbraco.dk>
268 lines
10 KiB
C#
268 lines
10 KiB
C#
// Copyright (c) Umbraco.
|
|
// See LICENSE for more details.
|
|
|
|
using Umbraco.Cms.Core;
|
|
using Umbraco.Cms.Core.Models.PublishedContent;
|
|
using Umbraco.Cms.Core.Routing;
|
|
|
|
namespace Umbraco.Extensions;
|
|
|
|
/// <summary>
|
|
/// Provides extension methods for <c>IPublishedElement</c>.
|
|
/// </summary>
|
|
public static class PublishedElementExtensions
|
|
{
|
|
#region OfTypes
|
|
|
|
// the .OfType<T>() filter is nice when there's only one type
|
|
// this is to support filtering with multiple types
|
|
public static IEnumerable<T> OfTypes<T>(this IEnumerable<T> contents, params string[] types)
|
|
where T : IPublishedElement
|
|
{
|
|
if (types == null || types.Length == 0)
|
|
{
|
|
return Enumerable.Empty<T>();
|
|
}
|
|
|
|
return contents.Where(x => types.InvariantContains(x.ContentType.Alias));
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region IsComposedOf
|
|
|
|
/// <summary>
|
|
/// Gets a value indicating whether the content is of a content type composed of the given alias
|
|
/// </summary>
|
|
/// <param name="content">The content.</param>
|
|
/// <param name="alias">The content type alias.</param>
|
|
/// <returns>
|
|
/// A value indicating whether the content is of a content type composed of a content type identified by the
|
|
/// alias.
|
|
/// </returns>
|
|
public static bool IsComposedOf(this IPublishedElement content, string alias) =>
|
|
content.ContentType.CompositionAliases.InvariantContains(alias);
|
|
|
|
#endregion
|
|
|
|
#region HasProperty
|
|
|
|
/// <summary>
|
|
/// Gets a value indicating whether the content has a property identified by its alias.
|
|
/// </summary>
|
|
/// <param name="content">The content.</param>
|
|
/// <param name="alias">The property alias.</param>
|
|
/// <returns>A value indicating whether the content has the property identified by the alias.</returns>
|
|
/// <remarks>The content may have a property, and that property may not have a value.</remarks>
|
|
public static bool HasProperty(this IPublishedElement content, string alias) =>
|
|
content.ContentType.GetPropertyType(alias) != null;
|
|
|
|
#endregion
|
|
|
|
#region HasValue
|
|
|
|
/// <summary>
|
|
/// Gets a value indicating whether the content has a value for a property identified by its alias.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Returns true if <c>GetProperty(alias)</c> is not <c>null</c> and <c>GetProperty(alias).HasValue</c> is
|
|
/// <c>true</c>.
|
|
/// </remarks>
|
|
public static bool HasValue(this IPublishedElement content, string alias, string? culture = null, string? segment = null)
|
|
{
|
|
IPublishedProperty? prop = content.GetProperty(alias);
|
|
return prop != null && prop.HasValue(culture, segment);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Value
|
|
|
|
/// <summary>
|
|
/// Gets the value of a content's property identified by its alias.
|
|
/// </summary>
|
|
/// <param name="content">The content.</param>
|
|
/// <param name="publishedValueFallback">The published value fallback implementation.</param>
|
|
/// <param name="alias">The property alias.</param>
|
|
/// <param name="culture">The variation language.</param>
|
|
/// <param name="segment">The variation segment.</param>
|
|
/// <param name="fallback">Optional fallback strategy.</param>
|
|
/// <param name="defaultValue">The default value.</param>
|
|
/// <returns>The value of the content's property identified by the alias, if it exists, otherwise a default value.</returns>
|
|
/// <remarks>
|
|
/// <para>
|
|
/// The value comes from <c>IPublishedProperty</c> field <c>Value</c> ie it is suitable for use when rendering
|
|
/// content.
|
|
/// </para>
|
|
/// <para>
|
|
/// If no property with the specified alias exists, or if the property has no value, returns
|
|
/// <paramref name="defaultValue" />.
|
|
/// </para>
|
|
/// <para>
|
|
/// If eg a numeric property wants to default to 0 when value source is empty, this has to be done in the
|
|
/// converter.
|
|
/// </para>
|
|
/// <para>The alias is case-insensitive.</para>
|
|
/// </remarks>
|
|
public static object? Value(
|
|
this IPublishedElement content,
|
|
IPublishedValueFallback publishedValueFallback,
|
|
string alias,
|
|
string? culture = null,
|
|
string? segment = null,
|
|
Fallback fallback = default,
|
|
object? defaultValue = default)
|
|
{
|
|
IPublishedProperty? property = content.GetProperty(alias);
|
|
|
|
// if we have a property, and it has a value, return that value
|
|
if (property != null && property.HasValue(culture, segment))
|
|
{
|
|
return property.GetValue(culture, segment);
|
|
}
|
|
|
|
// else let fallback try to get a value
|
|
if (publishedValueFallback.TryGetValue(content, alias, culture, segment, fallback, defaultValue, out var value))
|
|
{
|
|
return value;
|
|
}
|
|
|
|
// else... if we have a property, at least let the converter return its own
|
|
// vision of 'no value' (could be an empty enumerable) - otherwise, default
|
|
return property?.GetValue(culture, segment);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Value<T>
|
|
|
|
/// <summary>
|
|
/// Gets the value of a content's property identified by its alias, converted to a specified type.
|
|
/// </summary>
|
|
/// <typeparam name="T">The target property type.</typeparam>
|
|
/// <param name="content">The content.</param>
|
|
/// <param name="publishedValueFallback">The published value fallback implementation.</param>
|
|
/// <param name="alias">The property alias.</param>
|
|
/// <param name="culture">The variation language.</param>
|
|
/// <param name="segment">The variation segment.</param>
|
|
/// <param name="fallback">Optional fallback strategy.</param>
|
|
/// <param name="defaultValue">The default value.</param>
|
|
/// <returns>The value of the content's property identified by the alias, converted to the specified type.</returns>
|
|
/// <remarks>
|
|
/// <para>
|
|
/// The value comes from <c>IPublishedProperty</c> field <c>Value</c> ie it is suitable for use when rendering
|
|
/// content.
|
|
/// </para>
|
|
/// <para>
|
|
/// If no property with the specified alias exists, or if the property has no value, or if it could not be
|
|
/// converted, returns <c>default(T)</c>.
|
|
/// </para>
|
|
/// <para>
|
|
/// If eg a numeric property wants to default to 0 when value source is empty, this has to be done in the
|
|
/// converter.
|
|
/// </para>
|
|
/// <para>The alias is case-insensitive.</para>
|
|
/// </remarks>
|
|
public static T? Value<T>(
|
|
this IPublishedElement content,
|
|
IPublishedValueFallback publishedValueFallback,
|
|
string alias,
|
|
string? culture = null,
|
|
string? segment = null,
|
|
Fallback fallback = default,
|
|
T? defaultValue = default)
|
|
{
|
|
IPublishedProperty? property = content.GetProperty(alias);
|
|
|
|
// if we have a property, and it has a value, return that value
|
|
if (property != null && property.HasValue(culture, segment))
|
|
{
|
|
return property.Value<T>(publishedValueFallback, culture, segment);
|
|
}
|
|
|
|
// else let fallback try to get a value
|
|
if (publishedValueFallback.TryGetValue(content, alias, culture, segment, fallback, defaultValue, out T? value))
|
|
{
|
|
return value;
|
|
}
|
|
|
|
// else... if we have a property, at least let the converter return its own
|
|
// vision of 'no value' (could be an empty enumerable) - otherwise, default
|
|
return property == null ? default : property.Value<T>(publishedValueFallback, culture, segment);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region ToIndexedArray
|
|
|
|
public static IndexedArrayItem<TContent>[] ToIndexedArray<TContent>(this IEnumerable<TContent> source)
|
|
where TContent : class, IPublishedElement
|
|
{
|
|
IndexedArrayItem<TContent>[] set =
|
|
source.Select((content, index) => new IndexedArrayItem<TContent>(content, index)).ToArray();
|
|
foreach (IndexedArrayItem<TContent> setItem in set)
|
|
{
|
|
setItem.TotalCount = set.Length;
|
|
}
|
|
|
|
return set;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region IsSomething
|
|
|
|
/// <summary>
|
|
/// Gets a value indicating whether the content is visible.
|
|
/// </summary>
|
|
/// <param name="content">The content.</param>
|
|
/// <param name="publishedValueFallback">The published value fallback implementation.</param>
|
|
/// <returns>A value indicating whether the content is visible.</returns>
|
|
/// <remarks>
|
|
/// A content is not visible if it has an umbracoNaviHide property with a value of "1". Otherwise,
|
|
/// the content is visible.
|
|
/// </remarks>
|
|
public static bool IsVisible(this IPublishedElement content, IPublishedValueFallback publishedValueFallback) =>
|
|
|
|
// rely on the property converter - will return default bool value, ie false, if property
|
|
// is not defined, or has no value, else will return its value.
|
|
content.Value<bool>(publishedValueFallback, Constants.Conventions.Content.NaviHide) == false;
|
|
|
|
#endregion
|
|
|
|
#region MediaUrl
|
|
|
|
/// <summary>
|
|
/// Gets the url for a media.
|
|
/// </summary>
|
|
/// <param name="content">The content item.</param>
|
|
/// <param name="publishedUrlProvider">The published url provider.</param>
|
|
/// <param name="culture">The culture (use current culture by default).</param>
|
|
/// <param name="mode">The url mode (use site configuration by default).</param>
|
|
/// <param name="propertyAlias">The alias of the property (use 'umbracoFile' by default).</param>
|
|
/// <returns>The url for the media.</returns>
|
|
/// <remarks>
|
|
/// <para>
|
|
/// The value of this property is contextual. It depends on the 'current' request uri,
|
|
/// if any. In addition, when the content type is multi-lingual, this is the url for the
|
|
/// specified culture. Otherwise, it is the invariant url.
|
|
/// </para>
|
|
/// </remarks>
|
|
public static string MediaUrl(
|
|
this IPublishedContent content,
|
|
IPublishedUrlProvider publishedUrlProvider,
|
|
string? culture = null,
|
|
UrlMode mode = UrlMode.Default,
|
|
string propertyAlias = Constants.Conventions.Media.File)
|
|
{
|
|
if (publishedUrlProvider == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(publishedUrlProvider));
|
|
}
|
|
|
|
return publishedUrlProvider.GetMediaUrl(content, mode, culture, propertyAlias);
|
|
}
|
|
|
|
#endregion
|
|
}
|