Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
b76419b
Code tidy - XML header comments, method ordering, warning resolution.
AndyButland Mar 7, 2025
78a139c
Add extension method for retrieving all URL segments for a document.
AndyButland Mar 7, 2025
9e097bb
Cache and persist multiple URL segments per document.
AndyButland Mar 7, 2025
4df942e
Allowed segment providers to terminate or allow additional segments.
AndyButland Mar 7, 2025
9e5f5cc
Resolved cache issue to ensure passing new integration tests.
AndyButland Mar 7, 2025
119c368
Fixed failing integration test.
AndyButland Mar 7, 2025
6a4e20c
Test class naming tidy up.
AndyButland Mar 8, 2025
8d371f2
Added resolution and persistance of a primary segment, to retain prev…
AndyButland Mar 8, 2025
af32173
Further integration tests.
AndyButland Mar 8, 2025
6b11f02
Resolved backward compatibility of interface.
AndyButland Mar 10, 2025
252ac7e
Supress amends made to integration tests.
AndyButland Mar 10, 2025
022abc3
Aligned naming of integration tests.
AndyButland Mar 10, 2025
d2d97d1
Removed unused using, added XML header comment.
AndyButland Mar 10, 2025
ca24a0a
Throw on missing table in migration.
AndyButland Mar 10, 2025
991b0f3
Code clean-up.
AndyButland Mar 10, 2025
9514ff9
Fix multiple enumeration
nikolajlauridsen Mar 11, 2025
9a0dba6
Merge branch 'v15/dev' into v15/feature/multiple-segments-per-document
AndyButland Mar 12, 2025
b175e2d
Used default on migrated column.
AndyButland Mar 12, 2025
be14ed4
Use 1 over true for default value.
AndyButland Mar 12, 2025
c48b313
Remove unused logger
nikolajlauridsen Mar 13, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 58 additions & 14 deletions src/Umbraco.Core/Models/ContentBaseExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,81 @@ public static class ContentBaseExtensions
private static DefaultUrlSegmentProvider? _defaultUrlSegmentProvider;

/// <summary>
/// Gets the URL segment for a specified content and culture.
/// Gets a single URL segment for a specified content and culture.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="shortStringHelper"></param>
/// <param name="urlSegmentProviders"></param>
/// <param name="culture">The culture.</param>
/// <param name="published">Whether to get the published or draft.</param>
/// <returns>The URL segment.</returns>
/// <remarks>
/// If more than one URL segment provider is available, the first one that returns a non-null value will be returned.
/// </remarks>
public static string? GetUrlSegment(this IContentBase content, IShortStringHelper shortStringHelper, IEnumerable<IUrlSegmentProvider> urlSegmentProviders, string? culture = null, bool published = true)
{
if (content == null)
{
throw new ArgumentNullException(nameof(content));
}
var urlSegment = GetUrlSegments(content, urlSegmentProviders, culture, published).FirstOrDefault();

// Ensure we have at least the segment from the default URL provider returned.
urlSegment ??= GetDefaultUrlSegment(shortStringHelper, content, culture, published);

if (urlSegmentProviders == null)
return urlSegment;
}

/// <summary>
/// Gets all URL segments for a specified content and culture.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="shortStringHelper"></param>
/// <param name="urlSegmentProviders"></param>
/// <param name="culture">The culture.</param>
/// <param name="published">Whether to get the published or draft.</param>
/// <returns>The collection of URL segments.</returns>
public static IEnumerable<string> GetUrlSegments(this IContentBase content, IShortStringHelper shortStringHelper, IEnumerable<IUrlSegmentProvider> urlSegmentProviders, string? culture = null, bool published = true)
{
var urlSegments = GetUrlSegments(content, urlSegmentProviders, culture, published).Distinct().ToList();

// Ensure we have at least the segment from the default URL provider returned.
if (urlSegments.Count == 0)
{
throw new ArgumentNullException(nameof(urlSegmentProviders));
var defaultUrlSegment = GetDefaultUrlSegment(shortStringHelper, content, culture, published);
if (defaultUrlSegment is not null)
{
urlSegments.Add(defaultUrlSegment);
}
}

var url = urlSegmentProviders.Select(p => p.GetUrlSegment(content, published, culture)).FirstOrDefault(u => u != null);
if (url == null)
return urlSegments;
}

private static IEnumerable<string> GetUrlSegments(
IContentBase content,
IEnumerable<IUrlSegmentProvider> urlSegmentProviders,
string? culture,
bool published)
{
foreach (IUrlSegmentProvider urlSegmentProvider in urlSegmentProviders)
{
if (_defaultUrlSegmentProvider == null)
var segment = urlSegmentProvider.GetUrlSegment(content, published, culture);
if (string.IsNullOrEmpty(segment) == false)
{
_defaultUrlSegmentProvider = new DefaultUrlSegmentProvider(shortStringHelper);
}
yield return segment;

url = _defaultUrlSegmentProvider.GetUrlSegment(content, culture); // be safe
if (urlSegmentProvider.AllowAdditionalSegments is false)
{
yield break;
}
}
}
}

return url;
private static string? GetDefaultUrlSegment(
IShortStringHelper shortStringHelper,
IContentBase content,
string? culture,
bool published)
{
_defaultUrlSegmentProvider ??= new DefaultUrlSegmentProvider(shortStringHelper);
return _defaultUrlSegmentProvider.GetUrlSegment(content, published, culture);
}
}
23 changes: 23 additions & 0 deletions src/Umbraco.Core/Models/PublishedDocumentUrlSegment.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,32 @@
namespace Umbraco.Cms.Core.Models;

/// <summary>
/// Represents a URL segment for a published document.
/// </summary>
public class PublishedDocumentUrlSegment
{
/// <summary>
/// Gets or sets the document key.
/// </summary>
public required Guid DocumentKey { get; set; }

/// <summary>
/// Gets or sets the language Id.
/// </summary>
public required int LanguageId { get; set; }

/// <summary>
/// Gets or sets the URL segment string.
/// </summary>
public required string UrlSegment { get; set; }

/// <summary>
/// Gets or sets a value indicating whether the URL segment is for a draft.
/// </summary>
public required bool IsDraft { get; set; }

/// <summary>
/// Gets or sets a value indicating whether the URL segment is the primary one (first resolved from the collection of URL providers).
/// </summary>
public required bool IsPrimary { get; set; }
}
Loading