protected void MapToLinkModel(Link link, LinkField linkField, SitecoreFieldConfiguration config) { link.Anchor = linkField.Anchor; link.Class = linkField.Class; link.Style = linkField.GetAttribute("style"); link.Text = linkField.Text; link.Title = linkField.Title; link.Target = linkField.Target; link.Query = HttpUtility.UrlDecode(linkField.QueryString); switch (linkField.LinkType) { case "anchor": link.Url = linkField.Anchor; link.Type = LinkType.Anchor; break; case "external": link.Url = linkField.Url; link.Type = LinkType.External; break; case "mailto": link.Url = linkField.Url; link.Type = LinkType.MailTo; break; case "javascript": link.Url = linkField.Url; link.Type = LinkType.JavaScript; break; case "media": if (linkField.TargetItem == null) { link.Url = string.Empty; } else { global::Sitecore.Data.Items.MediaItem media = new global::Sitecore.Data.Items.MediaItem(linkField.TargetItem); link.Url = SitecoreVersionAbstractions.GetMediaUrl(media); } link.Type = LinkType.Media; link.TargetId = linkField.TargetID.Guid; break; case "internal": var urlOptions = _urlOptionsResolver.CreateUrlOptions(config.UrlOptions); link.Url = linkField.TargetItem == null ? string.Empty : SitecoreVersionAbstractions.GetItemUrl(linkField.TargetItem, urlOptions); link.Type = LinkType.Internal; link.TargetId = linkField.TargetID.Guid; link.Text = linkField.Text.IsNullOrEmpty() ? (linkField.TargetItem == null ? string.Empty : linkField.TargetItem.DisplayName) : linkField.Text; break; default: link = null; break; } }
protected void MapToLinkModel(Link link, InternalLinkField field, SitecoreFieldConfiguration config) { var urlOptions = _urlOptionsResolver.CreateUrlOptions(config.UrlOptions); link.Url = field.TargetItem == null ? string.Empty : SitecoreVersionAbstractions.GetItemUrl(field.TargetItem, urlOptions); link.Type = LinkType.Internal; link.TargetId = field.TargetItem == null ? Guid.Empty : field.TargetItem.ID.Guid; link.Text = field.TargetItem == null ? string.Empty : field.TargetItem.DisplayName; }
/// <summary> /// Sets up the data mapper for a particular property /// </summary> /// <param name="args">The args.</param> public override void Setup(DataMapperResolverArgs args) { var scConfig = args.PropertyConfiguration as SitecoreInfoConfiguration; if (scConfig == null) { throw new NullReferenceException("Configuration has not been set."); } ReadOnly = scConfig.Type != SitecoreInfoType.DisplayName && scConfig.Type != SitecoreInfoType.Name; switch (scConfig.Type) { case SitecoreInfoType.ContentPath: _getValue = (item, getOptions) => item.Paths.ContentPath; break; case SitecoreInfoType.DisplayName: _getValue = (item, getOptions) => item[Global.Fields.DisplayName]; break; case SitecoreInfoType.FullPath: _getValue = (item, getOptions) => item.Paths.FullPath; break; case SitecoreInfoType.Name: _getValue = (item, getOptions) => item.Name; break; case SitecoreInfoType.Key: _getValue = (item, getOptions) => item.Key; break; case SitecoreInfoType.MediaUrl: _getValue = (item, getOptions) => { var mediaUrlOptions = _mediaUrlOptionsResolver.GetMediaUrlOptions(scConfig.MediaUrlOptions); var media = new MediaItem(item); return(SitecoreVersionAbstractions.GetMediaUrl(media, mediaUrlOptions)); }; break; case SitecoreInfoType.Path: _getValue = (item, getOptions) => item.Paths.Path; break; case SitecoreInfoType.TemplateId: _getValue = (item, getOptions) => { if (scConfig.PropertyInfo != null && scConfig.PropertyInfo.PropertyType == typeof(ID)) { return(item.TemplateID); } return(item.TemplateID.Guid); }; break; case SitecoreInfoType.TemplateName: _getValue = (item, getOptions) => item.TemplateName; break; case SitecoreInfoType.Url: _getValue = (item, getOptions) => { var urlOptions = _urlOptionsResolver.CreateUrlOptions(scConfig.UrlOptions, getOptions); if ((scConfig.UrlOptions & SitecoreInfoUrlOptions.UseItemLanguage) == SitecoreInfoUrlOptions.UseItemLanguage) { urlOptions.Language = item.Language; } else { urlOptions.Language = null; } return(SitecoreVersionAbstractions.GetItemUrl(item, urlOptions)); }; break; case SitecoreInfoType.Version: _getValue = (item, getOptions) => { if (scConfig.PropertyInfo != null && scConfig.PropertyInfo.PropertyType == typeof(string)) { return(item.Version.Number.ToString()); } return(item.Version.Number); }; break; case SitecoreInfoType.Language: _getValue = (item, getOptions) => { if (scConfig.PropertyInfo != null && scConfig.PropertyInfo.PropertyType == typeof(string)) { return(item.Language.Name); } return(item.Language); }; break; case SitecoreInfoType.BaseTemplateIds: _getValue = (item, getOptions) => { Template template = TemplateManager.GetTemplate(item.TemplateID, item.Database); if (scConfig.PropertyInfo != null && scConfig.PropertyInfo.PropertyType == typeof(IEnumerable <ID>)) { return(template.GetBaseTemplates().Select(x => x.ID)); } return(template.GetBaseTemplates().Select(x => x.ID.Guid)); }; break; case SitecoreInfoType.ItemUri: _getValue = (item, getOptions) => new ItemUri(item.ID, item.Language, item.Version, item.Database); break; #if (SC82 || SC90 || SC91 || SC92 || SC93 || SC100) case SitecoreInfoType.OriginalLanguage: _getValue = (item, getOptions) => item.OriginalLanguage; break; case SitecoreInfoType.OriginatorId: _getValue = (item, getOptions) => item.OriginatorId; break; #endif default: throw new MapperException("SitecoreInfoType {0} not supported".Formatted(scConfig.Type)); } base.Setup(args); }
protected void MapToLinkField(Link link, LinkField linkField, SitecoreFieldConfiguration config) { var item = linkField.InnerField.Item; switch (link.Type) { case LinkType.Internal: linkField.LinkType = "internal"; if (linkField.TargetID.Guid != link.TargetId) { if (link.TargetId == Guid.Empty) { ItemLink iLink = new ItemLink(item.Database.Name, item.ID, linkField.InnerField.ID, linkField.TargetItem.Database.Name, linkField.TargetID, linkField.TargetItem.Paths.FullPath); linkField.RemoveLink(iLink); } else { ID newId = new ID(link.TargetId); Item target = item.Database.GetItem(newId); if (target != null) { linkField.TargetID = newId; ItemLink nLink = new ItemLink(item.Database.Name, item.ID, linkField.InnerField.ID, target.Database.Name, target.ID, target.Paths.FullPath); linkField.UpdateLink(nLink); linkField.Url = SitecoreVersionAbstractions.GetItemUrl(target); } else { throw new MapperException("No item with ID {0}. Can not update Link linkField".Formatted(newId)); } } } break; case LinkType.Media: linkField.LinkType = "media"; if (linkField.TargetID.Guid != link.TargetId) { if (link.TargetId == Guid.Empty) { ItemLink iLink = new ItemLink(item.Database.Name, item.ID, linkField.InnerField.ID, linkField.TargetItem.Database.Name, linkField.TargetID, linkField.TargetItem.Paths.FullPath); linkField.RemoveLink(iLink); } else { ID newId = new ID(link.TargetId); Item target = item.Database.GetItem(newId); if (target != null) { global::Sitecore.Data.Items.MediaItem media = new global::Sitecore.Data.Items.MediaItem(target); linkField.TargetID = newId; ItemLink nLink = new ItemLink(item.Database.Name, item.ID, linkField.InnerField.ID, target.Database.Name, target.ID, target.Paths.FullPath); linkField.UpdateLink(nLink); var mediaUrl = SitecoreVersionAbstractions.GetMediaUrl(media); linkField.Url = mediaUrl; } else { throw new MapperException("No item with ID {0}. Can not update Link linkField".Formatted(newId)); } } } break; case LinkType.External: linkField.LinkType = "external"; linkField.Url = link.Url; break; case LinkType.Anchor: linkField.LinkType = "anchor"; linkField.Url = link.Anchor; break; case LinkType.MailTo: linkField.LinkType = "mailto"; linkField.Url = link.Url; break; case LinkType.JavaScript: linkField.LinkType = "javascript"; linkField.Url = link.Url; break; } if (!link.Anchor.IsNullOrEmpty()) { linkField.Anchor = link.Anchor; } if (!link.Class.IsNullOrEmpty()) { linkField.Class = link.Class; } if (!link.Text.IsNullOrEmpty()) { linkField.Text = link.Text; } if (!link.Title.IsNullOrEmpty()) { linkField.Title = link.Title; } if (!link.Query.IsNullOrEmpty()) { linkField.QueryString = HttpUtility.UrlEncode(link.Query); } if (!link.Target.IsNullOrEmpty()) { linkField.Target = link.Target; } }