Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate w/Content Delivery API & maintain backwards compatibility #97

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 3 additions & 3 deletions src/UmbNav.Core/Models/UmbNavItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ namespace UmbNav.Core.Models
{
public class UmbNavItem
{
[JsonProperty("url")]
public string Url { get; set; }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is necessary to make this property public in order for the property to be returned via the Content Delivery API. Without this property an UmvNavItem's content is not useful via the Content Delivery API.

Copy link
Owner

@AaronSadlerUK AaronSadlerUK Jan 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs thinking about, this is private as the extension is the correct way to return the URL due to the culture property.

How would you normally handle links within the core? (When you would normally use the .Url() extension?

I haven't used the content API myself


[JsonProperty("udi")]
public GuidUdi Udi { get; set; }

Expand Down Expand Up @@ -63,9 +66,6 @@ public class UmbNavItem
[JsonProperty("hideLoggedOut")]
internal bool HideLoggedOut { get; set; }

[JsonProperty("url")]
internal string Url { get; set; }

[JsonProperty("includeChildNodes")]
internal bool IncludeChildNodes { get; set; }

Expand Down
128 changes: 125 additions & 3 deletions src/UmbNav.Core/ValueConverters/UmbNavValueConverter.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,139 @@
using System;
using Newtonsoft.Json;
using Serilog;
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using UmbNav.Core.Interfaces;
using UmbNav.Core.Models;
using UmbNav.Core.PropertyEditors;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.PropertyEditors;
using Serilog;

namespace UmbNav.Core.ValueConverters
{
#if NET8_0_OR_GREATER
#nullable enable

using Umbraco.Cms.Core.DeliveryApi;
using Umbraco.Cms.Core.PropertyEditors.DeliveryApi;
using Umbraco.Cms.Core.PublishedCache;

public class CustomUmbNavValueConverter : PropertyValueConverterBase, IDeliveryApiPropertyValueConverter
{
private readonly IPublishedSnapshotAccessor _publishedSnapshotAccessor;
private readonly IApiContentRouteBuilder _apiContentRouteBuilder;
private readonly ILogger _logger;
private readonly IUmbNavMenuBuilderService _umbNavMenuBuilderService;

public CustomUmbNavValueConverter(
IPublishedSnapshotAccessor publishedSnapshotAccessor,
IApiContentRouteBuilder apiContentRouteBuilder,
ILogger logger,
IUmbNavMenuBuilderService umbNavMenuBuilderService)
{
_publishedSnapshotAccessor = publishedSnapshotAccessor;
_apiContentRouteBuilder = apiContentRouteBuilder;
_logger = logger;
_umbNavMenuBuilderService = umbNavMenuBuilderService;
}

public override bool IsConverter(IPublishedPropertyType propertyType)
=> propertyType.EditorAlias.Equals("AaronSadler.UmbNav");

public override Type GetPropertyValueType(IPublishedPropertyType propertyType)
=> typeof(IEnumerable<UmbNavItem>);

public PropertyCacheLevel GetDeliveryApiPropertyCacheLevel(IPublishedPropertyType propertyType)
=> PropertyCacheLevel.Elements;

public PropertyCacheLevel GetDeliveryApiPropertyCacheLevelForExpansion(IPublishedPropertyType propertyType)
=> PropertyCacheLevel.Snapshot;

public Type GetDeliveryApiPropertyValueType(IPublishedPropertyType propertyType)
=> typeof(IEnumerable<UmbNavItem>);

public override object? ConvertIntermediateToObject(
IPublishedElement owner,
IPublishedPropertyType propertyType,
PropertyCacheLevel referenceCacheLevel,
object? inter,
bool preview)
{
if (inter is null)
{
_logger.Warning("No intermediate value found for property {PropertyAlias}.", propertyType.Alias);
return Enumerable.Empty<UmbNavItem>();
}

try
{
var items = JsonConvert.DeserializeObject<IEnumerable<UmbNavItem>>(inter.ToString() ?? string.Empty);
if (items == null)
{
_logger.Warning("Failed to deserialize UmbNav items for property {PropertyAlias}.", propertyType.Alias);
return Enumerable.Empty<UmbNavItem>();
}

// Build the menu using the UmbNavMenuBuilderService
var configuration = propertyType.DataType.ConfigurationAs<UmbNavConfiguration>();
if (configuration != null)
{
return _umbNavMenuBuilderService.BuildMenu(
items,
0,
configuration.RemoveNaviHideItems,
configuration.HideNoopener,
configuration.HideNoreferrer,
configuration.HideIncludeChildren,
configuration.AllowMenuItemDescriptions);
}

return items;
}
catch (Exception ex)
{
_logger.Error(ex, "Error converting UmbNav intermediate value for property {PropertyAlias}.", propertyType.Alias);
return Enumerable.Empty<UmbNavItem>();
}
}

public object? ConvertIntermediateToDeliveryApiObject(
IPublishedElement owner,
IPublishedPropertyType propertyType,
PropertyCacheLevel referenceCacheLevel,
object? inter,
bool preview,
bool expanding)
{
if (inter is null)
{
_logger.Warning("No intermediate value found for Delivery API conversion on property {PropertyAlias}.", propertyType.Alias);
return null;
}

try
{
var items = JsonConvert.DeserializeObject<IEnumerable<UmbNavItem>>(inter.ToString() ?? string.Empty);
if (items == null)
{
_logger.Warning("Failed to deserialize UmbNav items for Delivery API on property {PropertyAlias}.", propertyType.Alias);
return null;
}

return items;
}
catch (Exception ex)
{
_logger.Error(ex, "Error converting UmbNav intermediate value for Delivery API on property {PropertyAlias}.", propertyType.Alias);
return null;
}
}
}
#else
public class UmbNavValueConverter : PropertyValueConverterBase
{


private readonly IUmbNavMenuBuilderService _umbNavMenuBuilderService;
private readonly ILogger _logger;

Expand Down Expand Up @@ -67,4 +188,5 @@ public override object ConvertIntermediateToObject(IPublishedElement owner, IPub
return Enumerable.Empty<UmbNavItem>();
}
}
#endif
}