0

I am using Glass Mapper for Umbraco. While trying to model something I have a class like:

[UmbracoType(AutoMap = true)]
    public class ImageWithLink : BaseUmbracoItem
    {
        public virtual Image Image { get; set; }
        public virtual ?? Link { get; set; }
        public virtual string Copy { get; set; }
    }

There does not seem to be a 'Link' data type like there is in the Sitecore implementation. I saw This post ( http://bluetubeinc.com/blog/2014/6/glass-mapper-and-umbraco-7) and they use the 'RelatedLink' data type, but that does not exsist ( i checked in the glass repository).

Do I have to model it my self?

Edit: This is the Related Links property type.

Lee Cook
  • 573
  • 4
  • 18

2 Answers2

0

Assuming that Umbraco hasn't changed in a while and that you mean a link as in a html anchor...

I may be wrong on this one, but from memory, Umbraco has / had no concept of a link when it comes to the return from the api. It is returned as a node id (or list of) and you must use an extension method to return the url from.

Like you, as far as I can see in the Glass code base, it doesn't have an explicit implementation for a 'link' type like we have in Sitecore.

My guess is that you would have to either roll your own using a custom class and the Delegate feature OR use an integer mapping and call the umbraco api method.

I would also guess is that RelatedLink in that example is mapping to another class which would use the UmbracoPropertyTypeMapper in a similar way to what we do in Sitecore between types, not a 'Link' as in anchor.

We are due to look at umbraco again during the V4 process I believe so talk to Mike about adding it as a feature.

Nat
  • 19
  • 3
0

I Found a (rather horrible) solution. Umbraco returns Json, so i had to de-serialize it. You could turn this into a mapper.

[UmbracoType(AutoMap = true)]
    public class BaseUmbracoItem : IUmbracoItem
    {
        public virtual string Links { get; set; }

        public List<UmbracoLink> TypedLink
        {
            get
            {
                return JsonConvert.DeserializeObject<List<UmbracoLink>>(Links);
            }
        }
    }
public class UmbracoLink
    {
        public string link { get; set; }
        public string type { get; set; }
        public string title { get; set; }
        public bool newWindow { get; set; }
        public bool isInternal { get; set; }
    }

Here is a mapper version:

public class UmbracoLinkMapper : AbstractDataMapper
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="UmbracoLinkMapper"/> class.
        /// </summary>
        public UmbracoLinkMapper()
        {
            ReadOnly = true;
        }

        /// <summary>
        /// Maps data from the .Net property value to the CMS value
        /// </summary>
        /// <param name="mappingContext">The mapping context.</param>
        /// <returns>The value to write</returns>
        /// <exception cref="System.NotSupportedException"></exception>
        public override void MapToCms(AbstractDataMappingContext mappingContext)
        {
            throw new NotSupportedException();
        }

        /// <summary>
        /// Maps data from the CMS value to the .Net property value
        /// </summary>
        /// <param name="mappingContext">The mapping context.</param>
        /// <returns>System.Object.</returns>
        public override object MapToProperty(AbstractDataMappingContext mappingContext)
        {
            var scContext = mappingContext as UmbracoDataMappingContext;
            var scConfig = Configuration as UmbracoLinkConfiguration;

            var properties = scContext.Content.Properties.Where(x => x.Alias == Configuration.PropertyInfo.Name.ToLower()).ToList();
            if (properties.Any())
            {
                var property = properties.First().Value as string;
                return JsonConvert.DeserializeObject<List<UmbracoLink>>(property).First();
            }
            return null;
        }

        /// <summary>
        /// Indicates that the data mapper will mapper to and from the property
        /// </summary>
        /// <param name="configuration">The configuration.</param>
        /// <param name="context">The context.</param>
        /// <returns><c>true</c> if this instance can handle the specified configuration; otherwise, <c>false</c>.</returns>
        public override bool CanHandle(AbstractPropertyConfiguration configuration, Context context)
        {
            return configuration is UmbracoLinkConfiguration;
        }
    }

 public class UmbracoLinkConfiguration : AbstractPropertyConfiguration
    {
        public bool IsLazy { get; set; }
        public bool InferType { get; set; }
    }

public class UmbracoLinkAttribute : AbstractPropertyAttribute
    {
        public bool IsLazy { get; set; }
        public bool InferType { get; set; }

        public override AbstractPropertyConfiguration Configure(PropertyInfo propertyInfo)
        {
            var config = new UmbracoLinkConfiguration { IsLazy = IsLazy, InferType = InferType };
            Configure(propertyInfo, config);
            return config;
        }
    }
Lee Cook
  • 573
  • 4
  • 18