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

Support for Umbraco 12.3.6 and above, including Delivery Api and QueryString #76

Open
wants to merge 2 commits into
base: v9/dev
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
2 changes: 2 additions & 0 deletions src/Our.Umbraco.Meganav/Models/IMeganavEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ internal interface IMeganavEntity

string Url { get; }

string QueryString { get; }

string Target { get; }

bool Visible { get; }
Expand Down
2 changes: 2 additions & 0 deletions src/Our.Umbraco.Meganav/Models/IMeganavItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public interface IMeganavItem

string Url { get; }

string QueryString { get; }

string Target { get; }

int Level { get; }
Expand Down
51 changes: 51 additions & 0 deletions src/Our.Umbraco.Meganav/Models/MeganavApiItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.DeliveryApi;

namespace Our.Umbraco.Meganav.Models
{
internal class MeganavApiItem
{

public static MeganavApiItem Content(string title, string? queryString, string? target, Guid destinationId, string destinationType, IApiContentRoute route, int level, IEnumerable<MeganavApiItem>? children)
=> new(LinkType.Content, url: null, queryString, title, target, destinationId, destinationType, route, level, children);

public static MeganavApiItem External(string? title, string url, string? queryString, string? target, int level, IEnumerable<MeganavApiItem>? children)
=> new(LinkType.External, url, queryString, title, target, null, null, null, level, children);

private MeganavApiItem(LinkType linkType, string? url, string? queryString, string? title, string? target, Guid? destinationId, string? destinationType, IApiContentRoute? route, int level, IEnumerable<MeganavApiItem>? children)
{
LinkType = linkType;
Url = url;
QueryString = queryString;
Title = title;
Target = target;
DestinationId = destinationId;
DestinationType = destinationType;
Route = route;
Level = level;
Children = children;
}

public string? Url { get; }

public string? QueryString { get; }

public string? Title { get; }

public string? Target { get; }

public Guid? DestinationId { get; }

public string? DestinationType { get; }

public IApiContentRoute? Route { get; }

public LinkType LinkType { get; }

public int Level { get; }

public IEnumerable<MeganavApiItem> Children { get; set; }
}
}
5 changes: 4 additions & 1 deletion src/Our.Umbraco.Meganav/Models/MeganavEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ internal class MeganavEntity : IMeganavEntity
[DataMember(Name = "url")]
public string Url { get; set; }

[DataMember(Name = "target")]
[DataMember(Name = "queryString")]
public string QueryString { get; set; }

[DataMember(Name = "target")]
public string Target { get; set; }

[DataMember(Name = "visible")]
Expand Down
7 changes: 5 additions & 2 deletions src/Our.Umbraco.Meganav/Models/MeganavItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ internal class MeganavItem : IMeganavItem
[DataMember(Name = "url")]
public string Url { get; set; }

[DataMember(Name = "target")]
[DataMember(Name = "querystring")]
public string QueryString { get; set; }

[DataMember(Name = "target")]
public string Target { get; set; }

[DataMember(Name = "level")]
Expand All @@ -31,6 +34,6 @@ internal class MeganavItem : IMeganavItem
[DataMember(Name = "children")]
public IEnumerable<MeganavItem> Children { get; set; } = new List<MeganavItem>();

IEnumerable<IMeganavItem> IMeganavItem.Children => Children;
IEnumerable<IMeganavItem> IMeganavItem.Children => Children;
}
}
19 changes: 8 additions & 11 deletions src/Our.Umbraco.Meganav/Our.Umbraco.Meganav.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net5.0;net6.0;net7.0</TargetFrameworks>
<TargetFrameworks>net7.0;net8.0</TargetFrameworks>
<Version Condition="'$(BUILD_BUILDNUMBER)' == ''">1.0.0.0</Version>
<Version Condition="'$(BUILD_BUILDNUMBER)' != ''">$(BUILD_BUILDNUMBER)</Version>
<Description>A flexible, draggable link picker for constructing site navigation menus in Umbraco</Description>
<Authors>Callum Whyte</Authors>
<Copyright>Copyright © 2023 Callum Whyte, and other contributors</Copyright>
<Copyright>Copyright © 2024 Callum Whyte, and other contributors</Copyright>
<PackageIcon>docs\logo.png</PackageIcon>
<PackageLicenseFile>docs\LICENSE.md</PackageLicenseFile>
<PackageReadmeFile>docs\README.md</PackageReadmeFile>
Expand All @@ -22,13 +22,10 @@
<None Include="..\..\README.md" Pack="true" PackagePath="docs" />
<None Include="..\..\docs\img\logo.png" Pack="true" PackagePath="docs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
<PackageReference Include="Umbraco.Cms.Infrastructure" Version="[9.0.0,10.0.0)" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="Umbraco.Cms.Infrastructure" Version="[10.0.0,11.0.0)" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
<PackageReference Include="Umbraco.Cms.Infrastructure" Version="[11.0.0,13.0.0)" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
<PackageReference Include="Umbraco.Cms.Infrastructure" Version="[12.3.6,13.0.0)" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageReference Include="Umbraco.Cms.Infrastructure" Version="[13.0.0,14.0.0)" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public PublishedElementProperty(IPublishedPropertyType propertyType, IPublishedE

public object GetXPathValue(string culture = null, string segment = null) => _xpathValue.Value;

public bool HasValue(string culture = null, string segment = null) => _sourceValue != null;
public object GetDeliveryApiValue(bool expanding, string culture = null, string segment = null) => _objectValue.Value;

public bool HasValue(string culture = null, string segment = null) => _sourceValue != null;
}
}
94 changes: 82 additions & 12 deletions src/Our.Umbraco.Meganav/ValueConverters/MeganavValueConverter.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,45 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Our.Umbraco.Meganav.Models;
using Our.Umbraco.Meganav.PropertyEditors;
using Our.Umbraco.Meganav.PublishedContent;
using Umbraco.Cms.Core.DeliveryApi;
using Umbraco.Cms.Core.Logging;
using Umbraco.Cms.Core.Models.DeliveryApi;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.PropertyEditors;
using Umbraco.Cms.Core.PropertyEditors.DeliveryApi;
using Umbraco.Cms.Core.PublishedCache;
using Umbraco.Cms.Core.Routing;
using Umbraco.Cms.Core.Serialization;
using Umbraco.Cms.Core.Web;
using Umbraco.Cms.Core;
using Umbraco.Extensions;
using static Umbraco.Cms.Core.Constants;

namespace Our.Umbraco.Meganav.ValueConverters
{
internal class MeganavValueConverter : PropertyValueConverterBase
{
private readonly IPublishedUrlProvider _publishedUrlProvider;
private readonly IVariationContextAccessor _variationContextAccessor;
private readonly IUmbracoContextFactory _umbracoContextFactory;
private readonly PublishedElementFactory _publishedElementFactory;
internal class MeganavValueConverter : PropertyValueConverterBase, IDeliveryApiPropertyValueConverter
{
private readonly IPublishedSnapshotAccessor _publishedSnapshotAccessor;
private readonly IApiContentNameProvider _apiContentNameProvider;
private readonly IApiContentRouteBuilder _apiContentRouteBuilder;
private readonly IPublishedUrlProvider _publishedUrlProvider;
private readonly IVariationContextAccessor _variationContextAccessor;
private readonly IUmbracoContextFactory _umbracoContextFactory;
private readonly PublishedElementFactory _publishedElementFactory;

private MeganavConfiguration _config;

public MeganavValueConverter(IPublishedUrlProvider publishedUrlProvider, IVariationContextAccessor variationContextAccessor, IUmbracoContextFactory umbracoContextFactory, PublishedElementFactory publishedElementFactory)
private MeganavConfiguration _config;

public MeganavValueConverter(IPublishedSnapshotAccessor publishedSnapshotAccessor, IApiContentNameProvider apiContentNameProvider, IApiContentRouteBuilder apiContentRouteBuilder, IPublishedUrlProvider publishedUrlProvider, IVariationContextAccessor variationContextAccessor, IUmbracoContextFactory umbracoContextFactory, PublishedElementFactory publishedElementFactory)
{
_publishedSnapshotAccessor = publishedSnapshotAccessor;
_apiContentNameProvider = apiContentNameProvider;
_apiContentRouteBuilder = apiContentRouteBuilder;
_publishedElementFactory = publishedElementFactory;
_publishedUrlProvider = publishedUrlProvider;
_variationContextAccessor = variationContextAccessor;
_umbracoContextFactory = umbracoContextFactory;
Expand Down Expand Up @@ -76,7 +92,8 @@ private IEnumerable<MeganavItem> BuildItems(IUmbracoContext umbracoContext, IEnu
var item = new MeganavItem
{
Title = entity.Title,
Url = entity.Url,
Url = $"{entity.Url}{entity.QueryString}",
QueryString = entity.QueryString,
Target = entity.Target,
Level = level
};
Expand Down Expand Up @@ -148,5 +165,58 @@ private void EnsureConfiguration(IPublishedPropertyType propertyType)
_config = propertyType.DataType.ConfigurationAs<MeganavConfiguration>();
}
}
}
}

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

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

public object ConvertIntermediateToDeliveryApiObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object inter, bool preview, bool expanding)
{

IEnumerable<MeganavApiItem> DefaultValue() => Array.Empty<MeganavApiItem>();

if (inter is not string value || value.IsNullOrWhiteSpace())
{
return DefaultValue();
}

var entities = JsonConvert.DeserializeObject<IEnumerable<MeganavEntity>>(value);
if (entities == null || entities.Any() == false)
{
return DefaultValue();
}

IPublishedSnapshot publishedSnapshot = _publishedSnapshotAccessor.GetRequiredPublishedSnapshot();

MeganavApiItem? BuildApiItems(IMeganavEntity item, int level)
{
if (item.Visible)
{
switch (item.Udi?.EntityType)
{
case UdiEntityType.Document:
IPublishedContent? content = publishedSnapshot.Content?.GetById(item.Udi.Guid);
IApiContentRoute? route = content != null ? _apiContentRouteBuilder.Build(content) : null;
return content == null || route == null
? null
: MeganavApiItem.Content(
item.Title.IfNullOrWhiteSpace(_apiContentNameProvider.GetName(content)),
item.QueryString,
item.Target,
content.Key,
content.ContentType.Alias,
route,
level,
item.Children.Select(entity => BuildApiItems(entity, level + 1)).WhereNotNull().ToArray());
default:
return MeganavApiItem.External(item.Title, $"{item.Url}{item.QueryString}", item.QueryString, item.Target, level, item.Children.Select(entity => BuildApiItems(entity, level + 1)).WhereNotNull().ToArray());
}
}
return null;
}

return entities.Select(entity => BuildApiItems(entity, 0)).WhereNotNull().ToArray();

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
localize="placeholder"
placeholder="@placeholders_anchor"
class="umb-property-editor umb-textstring"
ng-model="model.target.anchor"
id="anchor" />
ng-model="model.target.queryString"
id="queryString" />

<datalist id="anchors">
<option value="{{a}}" ng-repeat="a in anchorValues"></option>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
name: item.name,
title: item.title,
url: item.url,
querystring: item.querystring,
target: item.target,
icon: item.icon,
udi: item.udi,
Expand Down