Skip to content

Commit

Permalink
Tracking Meganav item references as relations
Browse files Browse the repository at this point in the history
  • Loading branch information
callumbwhyte committed Feb 3, 2021
1 parent cad98b1 commit 50152c2
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Click and drag an item to change it's position within the navigation; drop it wh

## Getting started

This package is supported on Umbraco 8+.
This package is supported on Umbraco 8.7+.

### Installation

Expand Down
2 changes: 1 addition & 1 deletion src/Our.Umbraco.Meganav/Our.Umbraco.Meganav.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="UmbracoCms.Web" Version="8.1.0" />
<PackageReference Include="UmbracoCms.Web" Version="8.7.0" />
</ItemGroup>
</Project>
3 changes: 3 additions & 0 deletions src/Our.Umbraco.Meganav/Startup/MeganavComposer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Our.Umbraco.Meganav.PublishedContent;
using Our.Umbraco.Meganav.ValueReferences;
using Umbraco.Core;
using Umbraco.Core.Composing;

Expand All @@ -10,6 +11,8 @@ public class MeganavComposer : IUserComposer
public void Compose(Composition composition)
{
composition.Register<PublishedElementFactory>();

composition.DataValueReferenceFactories().Append<MeganavValueReferenceFactory>();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.Collections.Generic;
using Newtonsoft.Json;
using Our.Umbraco.Meganav.Models;
using Umbraco.Core.Models.Editors;
using Umbraco.Core.PropertyEditors;

namespace Our.Umbraco.Meganav.ValueReferences
{
internal class MeganavValueReferenceFactory : IDataValueReferenceFactory, IDataValueReference
{
public bool IsForEditor(IDataEditor dataEditor) => dataEditor.Alias.Equals(Constants.PropertyEditorAlias);

public IDataValueReference GetDataValueReference() => this;

public IEnumerable<UmbracoEntityReference> GetReferences(object value)
{
var entities = JsonConvert.DeserializeObject<IEnumerable<MeganavEntity>>(value?.ToString());

return GetReferences(entities);
}

private IEnumerable<UmbracoEntityReference> GetReferences(IEnumerable<MeganavEntity> entities)
{
foreach (var entity in entities)
{
if (entity.Children != null)
{
var references = GetReferences(entity.Children);

foreach (var reference in references)
{
yield return reference;
}
}

if (entity.Udi != null)
{
yield return new UmbracoEntityReference(entity.Udi);
}
}
}
}
}

0 comments on commit 50152c2

Please sign in to comment.