Skip to content

Commit

Permalink
Updated LinkPickerLink to look up the current URL in the content or m…
Browse files Browse the repository at this point in the history
…edia cache

As the URL may change over time, an instance of LinkPickerLink will not try to find the current URL of the item it's pointing to. This fixes #16.
  • Loading branch information
abjerner committed Oct 20, 2020
1 parent 5030ec7 commit b614153
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
32 changes: 31 additions & 1 deletion src/Skybrud.LinkPicker/Models/LinkPickerLink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Skybrud.Essentials.Json.Extensions;
using Skybrud.LinkPicker.PropertyEditors;
using Umbraco.Core;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Web;
using Umbraco.Web.Composing;

namespace Skybrud.LinkPicker.Models {

Expand Down Expand Up @@ -36,14 +39,41 @@ public class LinkPickerLink {

#region Constructors

public LinkPickerLink(JObject obj) {
public LinkPickerLink(JObject obj) : this(obj, Current.UmbracoContext, null) { }

internal LinkPickerLink(JObject obj, UmbracoContext context, LinkConfiguration config) {

// TODO: Should "Udi" property be of type Udi?

Id = obj.GetInt32("id");
Udi = obj.GetString("udi");
Name = obj.GetString("name");
Url = obj.GetString("url");
Type = obj.GetEnum("type", LinkPickerType.Url);

GuidUdi udi = string.IsNullOrWhiteSpace(Udi) ? null : GuidUdi.Parse(Udi);

switch (Type) {

// TODO: Update "Name" property as well if permitted by "config"

case LinkPickerType.Content: {
var c = udi == null ? null : context?.Content.GetById(udi);
if (c != null) Url = c.Url;
break;
}

case LinkPickerType.Media: {
var m = udi == null ? null : context?.Media.GetById(udi);
if (m != null) Url = m.Url;
break;
}

}

Target = obj.GetString("target");
Anchor = obj.GetString("anchor");

}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,24 @@
using Skybrud.LinkPicker.Models;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Core.PropertyEditors;
using Umbraco.Web;

namespace Skybrud.LinkPicker.PropertyEditors.ValueConverters {

public class LinkValueConverter : PropertyValueConverterBase {

private readonly IUmbracoContextAccessor _umbracoContextAccessor;

#region Constructors

public LinkValueConverter(IUmbracoContextAccessor umbracoContextAccessor) {
_umbracoContextAccessor = umbracoContextAccessor;
}

#endregion

#region Member methods

public override bool IsConverter(IPublishedPropertyType propertyType) {
return propertyType.EditorAlias == "Skybrud.LinkPicker.Link";
}
Expand All @@ -20,7 +33,7 @@ public override object ConvertSourceToIntermediate(IPublishedElement owner, IPub

JObject obj = JsonUtils.ParseJsonObject(str);

return new LinkPickerLink(obj);
return new LinkPickerLink(obj, _umbracoContextAccessor.UmbracoContext, propertyType.DataType.ConfigurationAs<LinkConfiguration>());

}

Expand All @@ -40,6 +53,8 @@ public override Type GetPropertyValueType(IPublishedPropertyType propertyType) {
return typeof(LinkPickerLink);
}

#endregion

}

}

0 comments on commit b614153

Please sign in to comment.