Skip to content

Commit

Permalink
Support Content Delivery API
Browse files Browse the repository at this point in the history
  • Loading branch information
PerplexDaniel committed Sep 30, 2024
1 parent c024a8a commit ec7e433
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Perplex.ContentBlocks.DeliveryApi/ApiContentBlockViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Umbraco.Cms.Core.Models.DeliveryApi;

namespace Perplex.ContentBlocks.DeliveryApi;

public class ApiContentBlockViewModel : IApiContentBlockViewModel
{
public Guid Id { get; init; }

public Guid DefinitionId { get; init; }

public Guid LayoutId { get; init; }

public IApiElement? Content { get; init; }
}
8 changes: 8 additions & 0 deletions src/Perplex.ContentBlocks.DeliveryApi/ApiContentBlocks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Perplex.ContentBlocks.DeliveryApi;
public class ApiContentBlocks
{
public static readonly ApiContentBlocks Empty = new();

public IApiContentBlockViewModel? Header { get; init; }
public IEnumerable<IApiContentBlockViewModel> Blocks { get; init; } = Array.Empty<IApiContentBlockViewModel>();
}
19 changes: 19 additions & 0 deletions src/Perplex.ContentBlocks.DeliveryApi/Composer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Perplex.ContentBlocks.PropertyEditor;
using Umbraco.Cms.Core.Composing;
using Umbraco.Cms.Core.DependencyInjection;

namespace Perplex.ContentBlocks.DeliveryApi;
public class Composer : IComposer
{
public void Compose(IUmbracoBuilder builder)
{
builder.PropertyValueConverters().Remove<ContentBlocksValueConverter>();

// We use the original value converter in the Delivery API value converter so it needs to be registered.
builder.Services
.RemoveAll<ContentBlocksValueConverter>()
.AddSingleton<ContentBlocksValueConverter>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using Perplex.ContentBlocks.PropertyEditor;
using Perplex.ContentBlocks.Rendering;
using Umbraco.Cms.Core.DeliveryApi;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.PropertyEditors;
using Umbraco.Cms.Core.PropertyEditors.DeliveryApi;

namespace Perplex.ContentBlocks.DeliveryApi;

public class ContentBlocksApiValueConverter : IDeliveryApiPropertyValueConverter
{
private readonly ContentBlocksValueConverter _contentBlocksValueConverter;
private readonly IApiElementBuilder _apiElementBuilder;

public ContentBlocksApiValueConverter(
ContentBlocksValueConverter contentBlocksValueConverter,
IApiElementBuilder apiElementBuilder)
{
_contentBlocksValueConverter = contentBlocksValueConverter;
_apiElementBuilder = apiElementBuilder;
}

public bool IsConverter(IPublishedPropertyType propertyType)
=> propertyType.EditorAlias == Constants.PropertyEditor.Alias;

public PropertyCacheLevel GetDeliveryApiPropertyCacheLevel(IPublishedPropertyType propertyType)
=> GetPropertyCacheLevel(propertyType);

public Type GetDeliveryApiPropertyValueType(IPublishedPropertyType propertyType)
=> typeof(IApiContentBlocks);

public object? ConvertIntermediateToDeliveryApiObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object? inter, bool preview, bool expanding)
{
var modelValue = _contentBlocksValueConverter.ConvertIntermediateToObject(owner, propertyType, referenceCacheLevel, inter, preview);
if (modelValue is not IContentBlocks contentBlocks)
{
return null;
}

return new ApiContentBlocks
{
Header = Map(contentBlocks.Header),
Blocks = contentBlocks.Blocks.Select(Map).OfType<IApiContentBlockViewModel>().ToArray(),
};

ApiContentBlockViewModel? Map(IContentBlockViewModel? vm)
{
if (vm is null)
{
return null;
}

return new ApiContentBlockViewModel
{
Id = vm.Id,
DefinitionId = vm.DefinitionId,
LayoutId = vm.LayoutId,
Content = _apiElementBuilder.Build(vm.Content),
};
}
}

#region Forwarded to ContentBlocksValueConverter
public object? ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object? inter, bool preview) => _contentBlocksValueConverter.ConvertIntermediateToObject(owner, propertyType, referenceCacheLevel, inter, preview);
public object? ConvertIntermediateToXPath(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object? inter, bool preview) => _contentBlocksValueConverter.ConvertIntermediateToXPath(owner, propertyType, referenceCacheLevel, inter, preview);
public object? ConvertSourceToIntermediate(IPublishedElement owner, IPublishedPropertyType propertyType, object? source, bool preview) => _contentBlocksValueConverter.ConvertSourceToIntermediate(owner, propertyType, source, preview);
public PropertyCacheLevel GetPropertyCacheLevel(IPublishedPropertyType propertyType) => _contentBlocksValueConverter.GetPropertyCacheLevel(propertyType);
public Type GetPropertyValueType(IPublishedPropertyType propertyType) => _contentBlocksValueConverter.GetPropertyValueType(propertyType);
public bool? IsValue(object? value, PropertyValueLevel level) => _contentBlocksValueConverter.IsValue(value, level);
#endregion
}
14 changes: 14 additions & 0 deletions src/Perplex.ContentBlocks.DeliveryApi/IApiContentBlockViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Umbraco.Cms.Core.Models.DeliveryApi;

namespace Perplex.ContentBlocks.DeliveryApi;

public interface IApiContentBlockViewModel
{
Guid Id { get; }

Guid DefinitionId { get; }

Guid LayoutId { get; }

IApiElement? Content { get; }
}
6 changes: 6 additions & 0 deletions src/Perplex.ContentBlocks.DeliveryApi/IApiContentBlocks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Perplex.ContentBlocks.DeliveryApi;
public interface IApiContentBlocks
{
IApiContentBlockViewModel Header { get; }
IEnumerable<IApiContentBlockViewModel> Blocks { get; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<ProduceReferenceAssembly>False</ProduceReferenceAssembly>
<NoWarn>$(NoWarn);NU1902</NoWarn>
</PropertyGroup>

<PropertyGroup Label="NuGet package">
<PackageId>Perplex.ContentBlocks.DeliveryApi</PackageId>
<Description>Adds support for the Content Delivery API in Umbraco 12+</Description>
<PackageTags>umbraco</PackageTags>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Umbraco.Cms.Web.BackOffice" Version="12.0.0" />
<ProjectReference Include="..\Perplex.ContentBlocks.Core\Perplex.ContentBlocks.Core.csproj" />
</ItemGroup>

</Project>
6 changes: 6 additions & 0 deletions src/Perplex.ContentBlocks.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Perplex.ContentBlocks.Core"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Perplex.ContentBlocks.StaticAssets", "Perplex.ContentBlocks.StaticAssets\Perplex.ContentBlocks.StaticAssets.csproj", "{B8986C6A-0F31-4963-A439-441844FD262A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Perplex.ContentBlocks.DeliveryApi", "Perplex.ContentBlocks.DeliveryApi\Perplex.ContentBlocks.DeliveryApi.csproj", "{36578E2A-1833-459B-A573-CFC7AB141AAA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -27,6 +29,10 @@ Global
{B8986C6A-0F31-4963-A439-441844FD262A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B8986C6A-0F31-4963-A439-441844FD262A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B8986C6A-0F31-4963-A439-441844FD262A}.Release|Any CPU.Build.0 = Release|Any CPU
{36578E2A-1833-459B-A573-CFC7AB141AAA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{36578E2A-1833-459B-A573-CFC7AB141AAA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{36578E2A-1833-459B-A573-CFC7AB141AAA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{36578E2A-1833-459B-A573-CFC7AB141AAA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit ec7e433

Please sign in to comment.