Skip to content

Commit

Permalink
Added import/export functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
svenrog committed Sep 6, 2023
1 parent b83034d commit c8ad9fa
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 5 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@

All notable changes to this project will be documented in this file.

## [1.8.0]

### Added

- Import/Export functionality (#22).

### Fixed

- Potential issue in `DefaultLinkHtmlSerializer` where data would be lost if no link url was present.

## [1.7.0]

### Added

- Default value handling for `LinkData` properties (#21).

## [1.6.2]

### Fixed
Expand Down
12 changes: 12 additions & 0 deletions docs/example-link-data-collection.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,15 @@ public virtual LinkDataCollection<ThumbnailLinkData> Thumbnails { get; set; }
```

![Property looks like this](./images/thumbnal-links.png)

## Register type for import/export

In Startup.cs

```
public void ConfigureServices(IServiceCollection services)
{
...
services.AddLinkDataExportTransform<ThumbnailLinkData>();
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using EPiServer.Core.Transfer;
using Geta.Optimizely.GenericLinks;
using Geta.Optimizely.GenericLinks.Transfer;

namespace Microsoft.Extensions.DependencyInjection
{
public static class DependencyInjectionExtensions
{
public static void AddLinkDataExportTransform<TLinkData>(this IServiceCollection services)
where TLinkData : LinkData, new()
{
services.AddSingleton<PropertyLinkDataCollectionTransform<TLinkData>>().Forward<PropertyLinkDataCollectionTransform<TLinkData>, IPropertyTransform>();
services.AddSingleton<PropertyLinkDataTransform<TLinkData>>().Forward<PropertyLinkDataTransform<TLinkData>, IPropertyTransform>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@ public virtual string Serialize<TLinkData>(LinkDataCollection<TLinkData>? links,

public virtual string? CreateLink(string? hrefValue, ILinkData linkData)
{
if (string.IsNullOrEmpty(hrefValue))
{
return WebUtility.HtmlEncode(linkData.Text);
}

var stringBuilder = new StringBuilder();
stringBuilder.Append("<a");
foreach (var attribute in linkData.Attributes)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (c) Geta Digital. All rights reserved.
// Licensed under Apache-2.0. See the LICENSE file in the project root for more information

using EPiServer.Core.Transfer;
using EPiServer.Core;
using EPiServer;
using EPiServer.Core.Transfer.Internal;
using System.Linq;
using Geta.Optimizely.GenericLinks.Html;

namespace Geta.Optimizely.GenericLinks.Transfer
{
public sealed class PropertyLinkDataCollectionTransform<TLinkData> : PropertyTransform<PropertyLinkDataCollection<TLinkData>>
where TLinkData : ILinkData, new()
{
private readonly IContentLoader _contentLoader;
private readonly ILinkHtmlSerializer _linkHtmlSerializer;
private readonly IImplicitContentExporter _implicitContentExporter;

public PropertyLinkDataCollectionTransform(
IImplicitContentExporter implicitContentExporter,
ILinkHtmlSerializer linkHtmlSerializer,
IContentLoader contentLoader)
{
_implicitContentExporter = implicitContentExporter;
_linkHtmlSerializer = linkHtmlSerializer;
_contentLoader = contentLoader;
}

protected override bool TransformForExport(
PropertyLinkDataCollection<TLinkData> source,
RawProperty output,
PropertyExportContext context)
{
if (source.Value is null)
{
output.Value = null;
return true;
}

var sourceLinks = source.Links!.Select(l => l.ReferencedPermanentLinkIds);

foreach (var guids in sourceLinks)
{
foreach (var contentGuid in guids)
{
if (_contentLoader.TryGet<IContent>(contentGuid, out var content))
_implicitContentExporter.ExportDependentContent(content, context.TransferContext);
}
}

output.Value = _linkHtmlSerializer.Serialize(source.Links, StringMode.InternalMode);
return true;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) Geta Digital. All rights reserved.
// Licensed under Apache-2.0. See the LICENSE file in the project root for more information

using EPiServer.Core.Transfer;
using EPiServer;
using EPiServer.Core.Transfer.Internal;
using EPiServer.Core;
using Geta.Optimizely.GenericLinks.Html;

namespace Geta.Optimizely.GenericLinks.Transfer
{
public sealed class PropertyLinkDataTransform<TLinkData> : PropertyTransform<PropertyLinkData<TLinkData>>
where TLinkData : LinkData, new()
{
private readonly IContentLoader _contentLoader;
private readonly IImplicitContentExporter _implicitContentExporter;
private readonly ILinkHtmlSerializer _linkHtmlSerializer;

public PropertyLinkDataTransform(
IImplicitContentExporter implicitContentExporter,
ILinkHtmlSerializer linkHtmlSerializer,
IContentLoader contentLoader)
{
_implicitContentExporter = implicitContentExporter;
_linkHtmlSerializer = linkHtmlSerializer;
_contentLoader = contentLoader;
}

protected override bool TransformForExport(PropertyLinkData<TLinkData> source, RawProperty output, PropertyExportContext context)
{
if (source.Value is null)
{
output.Value = null;
return true;
}

foreach (var referencedPermanentLinkId in source.Link!.ReferencedPermanentLinkIds)
{
if (_contentLoader.TryGet(referencedPermanentLinkId, out IContent content))
_implicitContentExporter.ExportDependentContent(content, context.TransferContext);
}

output.Value = _linkHtmlSerializer.Serialize(source.Link, StringMode.InternalMode);
return true;
}
}
}

0 comments on commit c8ad9fa

Please sign in to comment.