From 69c6eae7a22edc1cb5c76ce5f0022a0b1142ef5a Mon Sep 17 00:00:00 2001 From: Kenn Jacobsen Date: Tue, 8 Nov 2022 13:34:12 +0100 Subject: [PATCH] Move block grid single area rendering to its own dedicated view (#13359) --- .../EmbeddedResources/BlockGrid/area.cshtml | 10 +++++++ .../EmbeddedResources/BlockGrid/areas.cshtml | 8 +---- .../Migrations/Upgrade/UmbracoPlan.cs | 8 +++-- .../V_10_4_0/AddBlockGridPartialViews.cs | 29 +++++++++++++++++++ .../Extensions/BlockGridTemplateExtensions.cs | 26 +++++++++++++++++ .../Views/Partials/blockgrid/area.cshtml | 10 +++++++ .../Views/Partials/blockgrid/areas.cshtml | 8 +---- 7 files changed, 82 insertions(+), 17 deletions(-) create mode 100644 src/Umbraco.Core/EmbeddedResources/BlockGrid/area.cshtml create mode 100644 src/Umbraco.Infrastructure/Migrations/Upgrade/V_10_4_0/AddBlockGridPartialViews.cs create mode 100644 src/Umbraco.Web.UI/Views/Partials/blockgrid/area.cshtml diff --git a/src/Umbraco.Core/EmbeddedResources/BlockGrid/area.cshtml b/src/Umbraco.Core/EmbeddedResources/BlockGrid/area.cshtml new file mode 100644 index 000000000000..361484757c3f --- /dev/null +++ b/src/Umbraco.Core/EmbeddedResources/BlockGrid/area.cshtml @@ -0,0 +1,10 @@ +@using Umbraco.Extensions +@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage + +
+ @await Html.GetBlockGridItemsHtmlAsync(Model) +
diff --git a/src/Umbraco.Core/EmbeddedResources/BlockGrid/areas.cshtml b/src/Umbraco.Core/EmbeddedResources/BlockGrid/areas.cshtml index 1afd941a76d4..cf65b36a3a2a 100644 --- a/src/Umbraco.Core/EmbeddedResources/BlockGrid/areas.cshtml +++ b/src/Umbraco.Core/EmbeddedResources/BlockGrid/areas.cshtml @@ -8,12 +8,6 @@ style="--umb-block-grid--area-grid-columns: @(Model.AreaGridColumns?.ToString() ?? Model.GridColumns?.ToString() ?? "12");"> @foreach (var area in Model.Areas) { -
- @await Html.GetBlockGridItemsHtmlAsync(area) -
+ @await Html.GetBlockGridItemAreaHtmlAsync(area) } diff --git a/src/Umbraco.Infrastructure/Migrations/Upgrade/UmbracoPlan.cs b/src/Umbraco.Infrastructure/Migrations/Upgrade/UmbracoPlan.cs index 58dfacb9aeb2..c80d70fa1fd7 100644 --- a/src/Umbraco.Infrastructure/Migrations/Upgrade/UmbracoPlan.cs +++ b/src/Umbraco.Infrastructure/Migrations/Upgrade/UmbracoPlan.cs @@ -5,7 +5,6 @@ using Umbraco.Cms.Infrastructure.Migrations.Upgrade.Common; using Umbraco.Cms.Infrastructure.Migrations.Upgrade.V_10_0_0; using Umbraco.Cms.Infrastructure.Migrations.Upgrade.V_10_2_0; -using Umbraco.Cms.Infrastructure.Migrations.Upgrade.V_10_3_0; using Umbraco.Cms.Infrastructure.Migrations.Upgrade.V_8_0_0; using Umbraco.Cms.Infrastructure.Migrations.Upgrade.V_8_0_1; using Umbraco.Cms.Infrastructure.Migrations.Upgrade.V_8_1_0; @@ -295,7 +294,10 @@ protected void DefinePlan() To("{D0B3D29D-F4D5-43E3-BA67-9D49256F3266}"); To("{79D8217B-5920-4C0E-8E9A-3CF8FA021882}"); - // To 11.0.0/10.4.0 - To("{56833770-3B7E-4FD5-A3B6-3416A26A7A3F}"); + // To 10.3.0 + To("{56833770-3B7E-4FD5-A3B6-3416A26A7A3F}"); + + // To 10.4.0 + To("{3F5D492A-A3DB-43F9-A73E-9FEE3B180E6C}"); } } diff --git a/src/Umbraco.Infrastructure/Migrations/Upgrade/V_10_4_0/AddBlockGridPartialViews.cs b/src/Umbraco.Infrastructure/Migrations/Upgrade/V_10_4_0/AddBlockGridPartialViews.cs new file mode 100644 index 000000000000..fafd5b1a6fd8 --- /dev/null +++ b/src/Umbraco.Infrastructure/Migrations/Upgrade/V_10_4_0/AddBlockGridPartialViews.cs @@ -0,0 +1,29 @@ +using Umbraco.Cms.Infrastructure.Templates.PartialViews; + +namespace Umbraco.Cms.Infrastructure.Migrations.Upgrade.V_10_4_0; + +public class AddBlockGridPartialViews : MigrationBase +{ + private readonly IPartialViewPopulator _partialViewPopulator; + private const string FolderPath = "/Views/Partials/blockgrid"; + private static readonly string[] _filesToAdd = + { + "area.cshtml", + }; + + public AddBlockGridPartialViews(IMigrationContext context, IPartialViewPopulator partialViewPopulator) : base(context) + => _partialViewPopulator = partialViewPopulator; + + protected override void Migrate() + { + var embeddedBasePath = _partialViewPopulator.CoreEmbeddedPath + ".BlockGrid"; + + foreach (var fileName in _filesToAdd) + { + _partialViewPopulator.CopyPartialViewIfNotExists( + _partialViewPopulator.GetCoreAssembly(), + $"{embeddedBasePath}.{fileName}", + $"{FolderPath}/{fileName}"); + } + } +} diff --git a/src/Umbraco.Web.Common/Extensions/BlockGridTemplateExtensions.cs b/src/Umbraco.Web.Common/Extensions/BlockGridTemplateExtensions.cs index ee0375da4f1c..5951c6e0092d 100644 --- a/src/Umbraco.Web.Common/Extensions/BlockGridTemplateExtensions.cs +++ b/src/Umbraco.Web.Common/Extensions/BlockGridTemplateExtensions.cs @@ -14,6 +14,7 @@ public static class BlockGridTemplateExtensions public const string DefaultTemplate = "default"; public const string DefaultItemsTemplate = "items"; public const string DefaultItemAreasTemplate = "areas"; + public const string DefaultItemAreaTemplate = "area"; #region Async @@ -60,6 +61,20 @@ public static async Task GetBlockGridItemsHtmlAsync(this IHtmlHelp public static async Task GetBlockGridItemAreasHtmlAsync(this IHtmlHelper html, BlockGridItem item, string template = DefaultItemAreasTemplate) => await html.PartialAsync(DefaultFolderTemplate(template), item); + public static async Task GetBlockGridItemAreaHtmlAsync(this IHtmlHelper html, BlockGridArea area, string template = DefaultItemAreaTemplate) + => await html.PartialAsync(DefaultFolderTemplate(template), area); + + public static async Task GetBlockGridItemAreaHtmlAsync(this IHtmlHelper html, BlockGridItem item, string areaAlias, string template = DefaultItemAreaTemplate) + { + BlockGridArea? area = item.Areas.FirstOrDefault(a => a.Alias == areaAlias); + if (area == null) + { + return new HtmlString(string.Empty); + } + + return await GetBlockGridItemAreaHtmlAsync(html, area, template); + } + #endregion #region Sync @@ -95,6 +110,17 @@ public static IHtmlContent GetBlockGridItemsHtml(this IHtmlHelper html, IEnumera public static IHtmlContent GetBlockGridItemAreasHtml(this IHtmlHelper html, BlockGridItem item, string template = DefaultItemAreasTemplate) => html.Partial(DefaultFolderTemplate(template), item); + public static IHtmlContent GetBlockGridItemAreaHtml(this IHtmlHelper html, BlockGridArea area, string template = DefaultItemAreaTemplate) + => html.Partial(DefaultFolderTemplate(template), area); + + public static IHtmlContent GetBlockGridItemAreaHtml(this IHtmlHelper html, BlockGridItem item, string areaAlias, string template = DefaultItemAreaTemplate) + { + BlockGridArea? area = item.Areas.FirstOrDefault(a => a.Alias == areaAlias); + return area != null + ? GetBlockGridItemAreaHtml(html, area, template) + : new HtmlString(string.Empty); + } + #endregion private static string DefaultFolderTemplate(string template) => $"{DefaultFolder}{template}"; diff --git a/src/Umbraco.Web.UI/Views/Partials/blockgrid/area.cshtml b/src/Umbraco.Web.UI/Views/Partials/blockgrid/area.cshtml new file mode 100644 index 000000000000..361484757c3f --- /dev/null +++ b/src/Umbraco.Web.UI/Views/Partials/blockgrid/area.cshtml @@ -0,0 +1,10 @@ +@using Umbraco.Extensions +@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage + +
+ @await Html.GetBlockGridItemsHtmlAsync(Model) +
diff --git a/src/Umbraco.Web.UI/Views/Partials/blockgrid/areas.cshtml b/src/Umbraco.Web.UI/Views/Partials/blockgrid/areas.cshtml index 94eef55ad8b7..30f987ce1ebd 100644 --- a/src/Umbraco.Web.UI/Views/Partials/blockgrid/areas.cshtml +++ b/src/Umbraco.Web.UI/Views/Partials/blockgrid/areas.cshtml @@ -8,12 +8,6 @@ style="--umb-block-grid--area-grid-columns: @(Model.AreaGridColumns?.ToString() ?? Model.GridColumns?.ToString() ?? "12");"> @foreach (var area in Model.Areas) { -
- @await Html.GetBlockGridItemsHtmlAsync(area) -
+ @await Html.GetBlockGridItemAreaHtmlAsync(area) }