Skip to content

Commit

Permalink
Merge pull request #244 from stevetemple/feature/gibe-link-picker
Browse files Browse the repository at this point in the history
Add migrator for Gibe.LinkPicker
  • Loading branch information
KevinJump authored Nov 23, 2023
2 parents f1e5188 + eed721a commit 18519b9
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using Newtonsoft.Json;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.PropertyEditors;
using Umbraco.Extensions;
using uSync.Migrations.Migrators.Community.GibeLinkPicker.Models;

namespace uSync.Migrations.Migrators.Community.GibeLinkPicker
{
[SyncMigrator("Gibe.LinkPicker")]
public class GibeLinkPickerToMultiUrlPickerMigrator : SyncPropertyMigratorBase
{
public override string GetEditorAlias(SyncMigrationDataTypeProperty dataTypeProperty, SyncMigrationContext context)
=> Constants.PropertyEditors.Aliases.MultiUrlPicker;

public override string GetContentValue(SyncMigrationContentProperty contentProperty, SyncMigrationContext context)
{
if (contentProperty?.Value == null)
{
return contentProperty?.Value;

Check warning on line 19 in uSync.Migrations.Migrators/Community/GibeLinkPicker/GibeLinkPickerToMultiUrlPickerMigrator.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.

Check warning on line 19 in uSync.Migrations.Migrators/Community/GibeLinkPicker/GibeLinkPickerToMultiUrlPickerMigrator.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.
}

var gibeLinkPickers = GetPickerValues(contentProperty.Value).ToArray();

if (!gibeLinkPickers.Any())
{
return contentProperty.Value;
}

var links = new List<MultiUrlPickerValueEditor.LinkDto>();

foreach (var picker in gibeLinkPickers)
{
var link = new MultiUrlPickerValueEditor.LinkDto
{
Name = picker?.Name ?? string.Empty,
Url = picker?.Url ?? string.Empty,
};

if (picker?.Target == "_blank")
{
link.Target = picker.Target;
}

links.Add(link);
}
return JsonConvert.SerializeObject(links, Formatting.Indented);
}

private IEnumerable<GibeLinkPickerData> GetPickerValues(string? contentValue)
{
if (contentValue == null)
{
return Enumerable.Empty<GibeLinkPickerData>();
}

if (contentValue.StartsWith("["))
{
return JsonConvert.DeserializeObject<IEnumerable<GibeLinkPickerData>>(contentValue)
?? Enumerable.Empty<GibeLinkPickerData>();
}
return JsonConvert.DeserializeObject<GibeLinkPickerData>(contentValue)?.AsEnumerableOfOne()
?? Enumerable.Empty<GibeLinkPickerData>();
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace uSync.Migrations.Migrators.Community.GibeLinkPicker.Models
{
public class GibeLinkPickerData
{
public int Id { get; set; }
public string Uid { get; set; }

Check warning on line 6 in uSync.Migrations.Migrators/Community/GibeLinkPicker/Models/GibeLinkPickerData.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Uid' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 6 in uSync.Migrations.Migrators/Community/GibeLinkPicker/Models/GibeLinkPickerData.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Uid' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public string Name { get; set; }

Check warning on line 7 in uSync.Migrations.Migrators/Community/GibeLinkPicker/Models/GibeLinkPickerData.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 7 in uSync.Migrations.Migrators/Community/GibeLinkPicker/Models/GibeLinkPickerData.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public string Url { get; set; }

Check warning on line 8 in uSync.Migrations.Migrators/Community/GibeLinkPicker/Models/GibeLinkPickerData.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Url' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 8 in uSync.Migrations.Migrators/Community/GibeLinkPicker/Models/GibeLinkPickerData.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Url' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public string Target { get; set; }

Check warning on line 9 in uSync.Migrations.Migrators/Community/GibeLinkPicker/Models/GibeLinkPickerData.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Target' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 9 in uSync.Migrations.Migrators/Community/GibeLinkPicker/Models/GibeLinkPickerData.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Target' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
}
}

0 comments on commit 18519b9

Please sign in to comment.