Files
Umbraco-CMS/src/Umbraco.Core/Strings/DefaultUrlSegmentProvider.cs
Bjarke Berg 734b3cce2c Determine urls at save and publish time (#17033)
* Started work on service

* temp work

* temp commit

* Temp commit

* Added more routing logic

* Fixed tests

* Refactor and prepare for isdraft

* Work on drafts

* Fixed tests

* Move to enlistment to ensure caches is only updated on scope complete

* Clean up and handle null cultures

* Added functionality to the INavigationQueryService to get root keys

* Added migration

* Fixed issue with navigation

* Added migration

* Temp commit, move to cache refreshers.

* Fixed issues

* List urls

* fix build

* Fixed integration tests

* Refactor to create new content finder instead of changing the old

* rollback wrong commited line

* Clean up, and use docuemnt url service for index

* Fixed List endpoin

* Do not use Navigation service in methods intended by management api

* Fixed examine tests

* Make methods virtual

* Use domain from published request

* Use hybrid cache from new content finder

* Eliminate nucache usage

* Fixed issue with delivery api and url generation

* Fixed linux tests

* Added hybrid cache to all integration tests
2024-09-27 09:12:19 +02:00

48 lines
2.0 KiB
C#

using Umbraco.Cms.Core.Models;
using Umbraco.Extensions;
namespace Umbraco.Cms.Core.Strings;
/// <summary>
/// Default implementation of IUrlSegmentProvider.
/// </summary>
public class DefaultUrlSegmentProvider : IUrlSegmentProvider
{
private readonly IShortStringHelper _shortStringHelper;
public DefaultUrlSegmentProvider(IShortStringHelper shortStringHelper) => _shortStringHelper = shortStringHelper;
public virtual string? GetUrlSegment(IContentBase content, bool published, string? culture = null) =>
GetUrlSegmentSource(content, culture, published)?.ToUrlSegment(_shortStringHelper, culture);
/// <summary>
/// Gets the URL segment for a specified content and culture.
/// </summary>
/// <param name="content">The content.</param>
/// <param name="culture">The culture.</param>
/// <returns>The URL segment.</returns>
public virtual string? GetUrlSegment(IContentBase content, string? culture = null) =>
GetUrlSegmentSource(content, culture, true)?.ToUrlSegment(_shortStringHelper, culture);
private static string? GetUrlSegmentSource(IContentBase content, string? culture, bool published)
{
string? source = null;
if (content.HasProperty(Constants.Conventions.Content.UrlName))
{
source = (content.GetValue<string>(Constants.Conventions.Content.UrlName, culture, published: published) ?? string.Empty).Trim();
}
if (string.IsNullOrWhiteSpace(source))
{
// If the name of a node has been updated, but it has not been published, the url should use the published name, not the current node name
// If this node has never been published (GetPublishName is null), use the unpublished name
source = content is IContent document && document.Edited && document.GetPublishName(culture) != null
? document.GetPublishName(culture)
: content.GetCultureName(culture);
}
return source;
}
}