2021-07-07 14:36:52 -06:00
|
|
|
using System;
|
2018-06-29 19:52:40 +02:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2021-02-18 11:06:02 +01:00
|
|
|
using Umbraco.Cms.Core.Models;
|
|
|
|
|
using Umbraco.Cms.Core.Strings;
|
2018-06-29 19:52:40 +02:00
|
|
|
|
2021-02-18 11:06:02 +01:00
|
|
|
namespace Umbraco.Extensions
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
2020-10-05 20:48:38 +02:00
|
|
|
/// Provides extension methods to IContentBase to get URL segments.
|
2018-06-29 19:52:40 +02:00
|
|
|
/// </summary>
|
2019-12-09 14:12:06 +01:00
|
|
|
public static class ContentBaseExtensions
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
2020-10-05 20:48:38 +02:00
|
|
|
/// Gets the URL segment for a specified content and culture.
|
2018-06-29 19:52:40 +02:00
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="content">The content.</param>
|
2020-11-17 20:27:10 +01:00
|
|
|
/// <param name="shortStringHelper"></param>
|
2018-06-29 19:52:40 +02:00
|
|
|
/// <param name="urlSegmentProviders"></param>
|
2020-12-01 11:34:13 +01:00
|
|
|
/// <param name="culture">The culture.</param>
|
2020-10-05 20:48:38 +02:00
|
|
|
/// <returns>The URL segment.</returns>
|
2022-02-09 13:24:35 +01:00
|
|
|
public static string? GetUrlSegment(this IContentBase content, IShortStringHelper shortStringHelper, IEnumerable<IUrlSegmentProvider> urlSegmentProviders, string? culture = null)
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
|
|
|
|
if (content == null) throw new ArgumentNullException(nameof(content));
|
|
|
|
|
if (urlSegmentProviders == null) throw new ArgumentNullException(nameof(urlSegmentProviders));
|
|
|
|
|
|
|
|
|
|
var url = urlSegmentProviders.Select(p => p.GetUrlSegment(content, culture)).FirstOrDefault(u => u != null);
|
2021-07-07 14:36:52 -06:00
|
|
|
if (url == null)
|
|
|
|
|
{
|
|
|
|
|
if (s_defaultUrlSegmentProvider == null)
|
|
|
|
|
{
|
|
|
|
|
s_defaultUrlSegmentProvider = new DefaultUrlSegmentProvider(shortStringHelper);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
url = s_defaultUrlSegmentProvider.GetUrlSegment(content, culture); // be safe
|
|
|
|
|
}
|
2021-12-16 13:44:20 +01:00
|
|
|
|
2018-06-29 19:52:40 +02:00
|
|
|
return url;
|
|
|
|
|
}
|
2021-07-07 14:36:52 -06:00
|
|
|
|
2022-02-09 13:24:35 +01:00
|
|
|
private static DefaultUrlSegmentProvider? s_defaultUrlSegmentProvider;
|
2018-06-29 19:52:40 +02:00
|
|
|
}
|
|
|
|
|
}
|