Skip to content

Commit e6a1696

Browse files
author
Ronald Barendse
committed
Added Cogworks.Meganav connector
1 parent 469d819 commit e6a1696

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ This project offers Umbraco Deploy connectors for the following community packag
1616
- [Content List](https://github.com/umco/umbraco-content-list)
1717
- [DocType Grid Editor](https://our.umbraco.org/projects/backoffice-extensions/doc-type-grid-editor/)
1818
- [LeBlender](https://our.umbraco.org/projects/backoffice-extensions/leblender)
19+
- [Meganav](https://our.umbraco.com/projects/website-utilities/meganav/)
1920
- [Multi Url Picker](https://our.umbraco.com/projects/backoffice-extensions/multi-url-picker)
2021
- [Nested Content](https://our.umbraco.org/projects/backoffice-extensions/nested-content/)
2122
- [nuPickers](https://our.umbraco.org/projects/backoffice-extensions/nupickers/)

src/Umbraco.Deploy.Contrib.Connectors/Umbraco.Deploy.Contrib.Connectors.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
<Compile Include="Properties\AssemblyInfo.cs" />
7272
<Compile Include="Properties\VersionInfo.cs" />
7373
<Compile Include="ValueConnectors\ArchetypeValueConnector.cs" />
74+
<Compile Include="ValueConnectors\MeganavValueConnector.cs" />
7475
<Compile Include="ValueConnectors\MultiUrlPickerValueConnector.cs" />
7576
<Compile Include="ValueConnectors\NuPickersValueConnector.cs" />
7677
<Compile Include="ValueConnectors\PropertyListValueConnector.cs" />
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Newtonsoft.Json;
4+
using Newtonsoft.Json.Linq;
5+
using Umbraco.Core;
6+
using Umbraco.Core.Deploy;
7+
using Umbraco.Core.Models;
8+
using Umbraco.Core.Services;
9+
10+
namespace Umbraco.Deploy.Contrib.Connectors.ValueConnectors
11+
{
12+
public class MeganavValueConnector : IValueConnector
13+
{
14+
/// <summary>
15+
/// The entity service.
16+
/// </summary>
17+
private readonly IEntityService entityService;
18+
19+
/// <summary>
20+
/// Gets the property editor aliases that the value converter supports by default.
21+
/// </summary>
22+
public IEnumerable<string> PropertyEditorAliases => new string[] { "Cogworks.Meganav" };
23+
24+
/// <summary>
25+
/// Initializes a new instance of the <see cref="MeganavValueConnector"/> class.
26+
/// </summary>
27+
/// <param name="entityService">The entity service.</param>
28+
/// <exception cref="ArgumentNullException">entityService</exception>
29+
public MeganavValueConnector(IEntityService entityService)
30+
{
31+
this.entityService = entityService ?? throw new ArgumentNullException("entityService");
32+
}
33+
34+
/// <summary>
35+
/// Gets the deploy property corresponding to a content property.
36+
/// </summary>
37+
/// <param name="property">The content property.</param>
38+
/// <param name="dependencies">The content dependencies.</param>
39+
/// <returns>
40+
/// The deploy property value.
41+
/// </returns>
42+
public string GetValue(Property property, ICollection<ArtifactDependency> dependencies)
43+
{
44+
var value = property.Value as string;
45+
if (String.IsNullOrWhiteSpace(value) || !StringExtensions.DetectIsJson(value))
46+
{
47+
return null;
48+
}
49+
50+
// Parse links and convert ID to UDI
51+
var links = JArray.Parse(value);
52+
foreach (var link in links)
53+
{
54+
GuidUdi guidUdi = null;
55+
int id = link.Value<int>("id");
56+
if (id != 0)
57+
{
58+
Attempt<Guid> keyForId = this.entityService.GetKeyForId(id, UmbracoObjectTypes.Document);
59+
if (keyForId.Success)
60+
{
61+
guidUdi = new GuidUdi("document", keyForId.Result);
62+
dependencies.Add(new ArtifactDependency(guidUdi, false, ArtifactDependencyMode.Exist));
63+
}
64+
}
65+
66+
link["id"] = guidUdi?.ToString();
67+
}
68+
69+
return links.ToString(Formatting.None);
70+
}
71+
72+
/// <summary>
73+
/// Sets a content property value using a deploy property.
74+
/// </summary>
75+
/// <param name="content">The content item.</param>
76+
/// <param name="alias">The property alias.</param>
77+
/// <param name="value">The deploy property value.</param>
78+
public void SetValue(IContentBase content, string alias, string value)
79+
{
80+
if (String.IsNullOrWhiteSpace(value))
81+
{
82+
// Save empty value
83+
content.SetValue(alias, value);
84+
return;
85+
}
86+
87+
if (!StringExtensions.DetectIsJson(value))
88+
{
89+
// Skip invalid value (probably shoudn't be saved, right?)
90+
return;
91+
}
92+
93+
// Parse links and convert UDI back to local ID
94+
var links = JArray.Parse(value);
95+
foreach (var link in links)
96+
{
97+
int id = 0;
98+
if (GuidUdi.TryParse(link.Value<string>("id"), out GuidUdi guidUdi))
99+
{
100+
Attempt<int> idForUdi = this.entityService.GetIdForUdi(guidUdi);
101+
if (idForUdi.Success)
102+
{
103+
id = idForUdi.Result;
104+
}
105+
}
106+
107+
link["id"] = id;
108+
}
109+
110+
content.SetValue(alias, links.ToString(Formatting.None));
111+
}
112+
}
113+
}

0 commit comments

Comments
 (0)