private Teaser GetTeaser(IPublishedElement element) { var teaser = new Teaser(element); if (teaser.UseArticleData) { var article = teaser.Link?.Udi != null ? new ArticlePage(Helper.Content(teaser.Link.Udi)) : null; if (article == null) { throw new Exception($"Please make sure that you have a linked article page when using article data."); } } else { teaser.Images = element.HasValue(DocumentTypes.Teaser.Fields.Images) ? _imageService.GetImages( element.Value <IEnumerable <IPublishedContent> >(DocumentTypes.Teaser.Fields.Images)) : null; teaser.TeaserText = element.HasValue(DocumentTypes.Teaser.Fields.TeaserText) ? element.Value <string>(DocumentTypes.Teaser.Fields.TeaserText) : null; teaser.TeaserTitle = element.HasValue(DocumentTypes.Teaser.Fields.TeaserTitle) ? element.Value <string>(DocumentTypes.Teaser.Fields.TeaserTitle) : null; } return(teaser); }
public static IEnumerable <Image> GetImages(this IPublishedElement element, string field, int?width = null, int?height = null, ImageCropMode imageCropMode = ImageCropMode.Crop) { var imageService = (IImageService)DependencyResolver.Current.GetService(typeof(IImageService)); return(element.HasValue(field) ? imageService.GetImages(element.Value <IEnumerable <IPublishedContent> >(field), width, height, imageCropMode) : null); }
// tries to get a value, falling back onto other languages private bool TryGetValueWithLanguageFallback <T>(IPublishedElement content, string alias, string culture, string segment, out T value, bool includeFallbackLanguage) { value = default; if (culture.IsNullOrWhiteSpace()) { return(false); } var visited = new HashSet <int>(); var language = _localizationService.GetLanguageByIsoCode(culture); if (language == null) { return(false); } while (true) { if (language.FallbackLanguageId == null) { return(false); } var language2Id = language.FallbackLanguageId.Value; if (visited.Contains(language2Id)) { return(false); } visited.Add(language2Id); var language2 = _localizationService.GetLanguageById(language2Id); if (language2 == null) { return(false); } var culture2 = language2.IsoCode; if (content.HasValue(alias, culture2, segment)) { value = content.Value <T>(alias, culture2, segment); if (includeFallbackLanguage && culture2 != culture) { value = GetMarkUpForFallbackLanguage(culture2, value); } return(true); } language = language2; } }
public static IEnumerable <T> GetValueAsViewModels <T, U>(this IPublishedElement content, string propAlias, string language = "") where U : IPublishedElement { if (content.HasValue(propAlias)) { var items = content.Value <IEnumerable <U> >(propAlias, culture: string.IsNullOrEmpty(language) ? null : language, fallback: Fallback.ToLanguage); if (items != null && items.Any()) { return(items.Select(c => (T)Activator.CreateInstance(typeof(T), c))); } } return(Enumerable.Empty <T>()); }
public static T GetValueAsViewModel <T>(this IPublishedElement content, string propAlias, string language = "") where T : class { if (content.HasValue(propAlias)) { var element = content.Value <IEnumerable <IPublishedElement> >(propAlias, culture: string.IsNullOrEmpty(language) ? null : language, fallback: Fallback.ToLanguage).FirstOrDefault(); if (element != null) { return((T)Activator.CreateInstance(typeof(T), element)); } } return(null); }
// tries to get a value, falling back onto other languages private bool TryGetValueWithLanguageFallback <T>(IPublishedElement content, string alias, string?culture, string?segment, out T?value) { value = default; if (culture.IsNullOrWhiteSpace()) { return(false); } var visited = new HashSet <int>(); ILanguage?language = culture is not null?_localizationService?.GetLanguageByIsoCode(culture) : null; if (language == null) { return(false); } while (true) { if (language.FallbackLanguageId == null) { return(false); } var language2Id = language.FallbackLanguageId.Value; if (visited.Contains(language2Id)) { return(false); } visited.Add(language2Id); ILanguage?language2 = _localizationService?.GetLanguageById(language2Id); if (language2 == null) { return(false); } var culture2 = language2.IsoCode; if (content.HasValue(alias, culture2, segment)) { value = content.Value <T>(this, alias, culture2, segment); return(true); } language = language2; } }
/// <inheritdoc /> public override object ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object inter, bool preview) { IPublishedElement element = (IPublishedElement)base.ConvertIntermediateToObject(owner, propertyType, referenceCacheLevel, inter, preview); return(element == null || (element.HasValue("uNestingHide") && element.Value <bool>("uNestingHide")) ? null : element); }
public FooterContactArea(IPublishedElement content) : base(content) { Headline = content.HasValue(DocumentTypes.FooterContact.Headline) ? content.Value <string>(DocumentTypes.FooterContact.Headline) : string.Empty; AddressBlock = content.HasValue(DocumentTypes.FooterContact.AddressBlock) ? content.Value <string>(DocumentTypes.FooterContact.AddressBlock) : string.Empty; }
/// <summary> /// Returns value of the property with given <paramref name="propertyName"/> from the <paramref name="source"/>, /// or creates default value by using provided <paramref name="defaultValueFactory"/> if property is not found or value is not assigned to it. /// </summary> /// <typeparam name="T">Expected type of the property value.</typeparam> /// <param name="source">The source.</param> /// <param name="defaultValueFactory">Default value factory that will be used if property or its value are not found.</param> /// <param name="propertyName">Property name.</param> /// <returns>Value of the property with given <paramref name="propertyName"/> or value returned by <paramref name="defaultValueFactory"/> if property or its value are not found.</returns> public static T GetPropertyWithDefaultValue <T>(this IPublishedElement source, Func <T> defaultValueFactory, [CallerMemberName] string propertyName = null) => source.HasValue(propertyName) ? source.Value <T>(propertyName) : defaultValueFactory();
// fixme - .Value() refactoring - in progress // missing variations... /// <summary> /// Returns one of two strings depending on whether the content has a value for a property identified by its alias. /// </summary> /// <param name="content">The content.</param> /// <param name="alias">The property alias.</param> /// <param name="valueIfTrue">The value to return if the content has a value for the property.</param> /// <param name="valueIfFalse">The value to return if the content has no value for the property.</param> /// <returns>Either <paramref name="valueIfTrue"/> or <paramref name="valueIfFalse"/> depending on whether the content /// has a value for the property identified by the alias.</returns> public static IHtmlString IfValue(this IPublishedElement content, string alias, string valueIfTrue, string valueIfFalse = null) { return(content.HasValue(alias) ? new HtmlString(valueIfTrue) : new HtmlString(valueIfFalse ?? string.Empty)); }