Skip to content

Commit

Permalink
Merge pull request #88 from Lombiq/issue/OSOE-682
Browse files Browse the repository at this point in the history
OSOE-682: Upgrading Orchard-Base-Theme to Orchard Core 1.7
  • Loading branch information
Psichorex authored Sep 11, 2023
2 parents d9f32a5 + 114ab89 commit e3f4e20
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 31 deletions.
4 changes: 2 additions & 2 deletions Lombiq.BaseTheme.Samples/Lombiq.BaseTheme.Samples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="OrchardCore.Admin" Version="1.6.0" />
<PackageReference Include="OrchardCore.Theme.Targets" Version="1.6.0" />
<PackageReference Include="OrchardCore.Admin" Version="1.7.0" />
<PackageReference Include="OrchardCore.Theme.Targets" Version="1.7.0" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion Lombiq.BaseTheme.Tests.UI/Lombiq.BaseTheme.Tests.UI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</ItemGroup>

<ItemGroup Condition="'$(NuGetBuild)' == 'true'">
<PackageReference Include="Lombiq.Tests.UI" Version="7.1.0" />
<PackageReference Include="Lombiq.Tests.UI" Version="7.1.3-alpha.2.osoe-682" />
</ItemGroup>

<ItemGroup>
Expand Down
16 changes: 8 additions & 8 deletions Lombiq.BaseTheme/Lombiq.BaseTheme.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="OrchardCore.Theme.Targets" Version="1.6.0" />
<PackageReference Include="OrchardCore.Menu" Version="1.6.0" />
<PackageReference Include="OrchardCore.Media" Version="1.6.0" />
<PackageReference Include="OrchardCore.ContentManagement" Version="1.6.0" />
<PackageReference Include="OrchardCore.DisplayManagement" Version="1.6.0" />
<PackageReference Include="OrchardCore.ResourceManagement" Version="1.6.0" />
<PackageReference Include="OrchardCore.Theme.Targets" Version="1.7.0" />
<PackageReference Include="OrchardCore.Menu" Version="1.7.0" />
<PackageReference Include="OrchardCore.Media" Version="1.7.0" />
<PackageReference Include="OrchardCore.ContentManagement" Version="1.7.0" />
<PackageReference Include="OrchardCore.DisplayManagement" Version="1.7.0" />
<PackageReference Include="OrchardCore.ResourceManagement" Version="1.7.0" />
</ItemGroup>

<ItemGroup Condition="'$(NuGetBuild)' != 'true'">
Expand All @@ -47,8 +47,8 @@
</ItemGroup>

<ItemGroup Condition="'$(NuGetBuild)' == 'true'">
<PackageReference Include="Lombiq.HelpfulLibraries" Version="7.0.0" />
<PackageReference Include="Lombiq.HelpfulExtensions" Version="6.1.1" />
<PackageReference Include="Lombiq.HelpfulLibraries" Version="7.0.1-alpha.9.osoe-682" />
<PackageReference Include="Lombiq.HelpfulExtensions" Version="6.1.2-alpha.3.osoe-682" />
<PackageReference Include="Lombiq.NodeJs.Extensions" Version="1.3.0" />

<Content Include="Assets/Styles/**/*.*">
Expand Down
30 changes: 10 additions & 20 deletions Lombiq.BaseTheme/Services/MainMenuNavigationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
using AngleSharp.Html.Parser;
using Lombiq.HelpfulLibraries.OrchardCore.Navigation;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.Extensions.Localization;
using Newtonsoft.Json.Linq;
using OrchardCore.ContentManagement;
using OrchardCore.Menu.Models;
using OrchardCore.Navigation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand All @@ -21,8 +19,8 @@ public class MainMenuNavigationProvider : MainMenuNavigationProviderBase
{
private readonly IContentHandleManager _contentHandleManager;
private readonly IContentManager _contentManager;

private readonly Lazy<IUrlHelper> _urlHelperLazy;
private readonly IUrlHelperFactory _urlHelperFactory;
private readonly IActionContextAccessor _actionContextAccessor;

public MainMenuNavigationProvider(
IHttpContextAccessor hca,
Expand All @@ -35,8 +33,8 @@ public MainMenuNavigationProvider(
{
_contentHandleManager = contentHandleManager;
_contentManager = contentManager;

_urlHelperLazy = new(() => urlHelperFactory.GetUrlHelper(actionContextAccessor.ActionContext!));
_urlHelperFactory = urlHelperFactory;
_actionContextAccessor = actionContextAccessor;
}

protected override async Task BuildAsync(NavigationBuilder builder)
Expand All @@ -60,7 +58,7 @@ private async Task AddAsync(NavigationBuilder builder, ContentItem menuItem)

if (menuItem.As<LinkMenuItemPart>() is { } linkMenuItemPart)
{
builder.Add(text, menu => menu.Url(GetUrl(linkMenuItemPart.Url)));
builder.Add(text, menu => menu.Url(linkMenuItemPart.Url));
}
else if (menuItem.As<ContentMenuItemPart>() is { } contentMenuItemPart)
{
Expand All @@ -75,9 +73,7 @@ private async Task AddAsync(NavigationBuilder builder, ContentItem menuItem)
{
var nodeList = new HtmlParser().ParseFragment($"<div>{htmlMenuItemPart.Html}</div>", contextElement: null!);
var textContent = string.Concat(nodeList.Select(x => x.Text()));
var url = string.IsNullOrEmpty(htmlMenuItemPart.Url) ? "#" : GetUrl(htmlMenuItemPart.Url);

builder.Add(T[textContent], menu => menu.Url(url).LocalNav());
builder.Add(new LocalizedString(textContent, textContent), menu => menu.Url("#").LocalNav());
}
else if (menuItem.As<MenuItemsListPart>() is { } menuItemsListPart)
{
Expand All @@ -89,32 +85,26 @@ await builder.AddAsync(text, menu =>
private async Task AddContentMenuItemPartAsync(NavigationBuilder builder, LocalizedString text, IEnumerable<string> ids)
{
var contentItems = (await _contentManager.GetAsync(ids)).AsList();
var urlHelper = _urlHelperFactory.GetUrlHelper(_actionContextAccessor.ActionContext!);

if (contentItems.Count == 1)
{
var contentItem = contentItems.Single();
if (string.IsNullOrEmpty(text.Value)) text = GetTitle(contentItem);
builder.Add(text, menu => UseDisplayUrl(menu, contentItem));
builder.Add(text, menu => menu.Url(urlHelper.DisplayContentItem(contentItem)));
}
else
{
builder.Add(text, menu =>
{
foreach (var contentItem in contentItems)
{
menu.Add(GetTitle(contentItem), child => UseDisplayUrl(child, contentItem));
menu.Add(GetTitle(contentItem), child => child.Url(urlHelper.DisplayContentItem(contentItem)));
}
});
}
}

// This and UseDisplayUrl(), and the other changes to this class under
// https://github.com/Lombiq/Orchard-Base-Theme/pull/74/files#diff-6dca81c4abae780b06f901d2ab84eb1e6d369e8d842da264a89199dfbaf11071
// may be reverted once https://github.com/OrchardCMS/OrchardCore/issues/13943 is fixed by an Orchard Core upgrade.
private string GetUrl(string contentPath) => _urlHelperLazy.Value.Content(contentPath);

private void UseDisplayUrl(NavigationItemBuilder menu, IContent content) =>
menu.Url(_urlHelperLazy.Value.DisplayContentItem(content));

private static LocalizedString GetTitle(ContentItem contentItem) =>
new(contentItem.DisplayText, contentItem.DisplayText);
}

0 comments on commit e3f4e20

Please sign in to comment.