Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • phaedra 5 posts 88 karma points
    Apr 10, 2016 @ 19:49
    phaedra
    0

    altFieldAlias in GetPropertyValue

    Hi - I'm trying to use an altFieldAlias inside GetPropertyValue, but it doesn't seem to be falling back properly. The primary alias works fine, and the secondary alias for the altFieldAlias works in a secondary loop, so maybe its my syntax? Here's the edited down code:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage<ContentModels.WShome>
    @using ContentModels = Umbraco.Web.PublishedContentModels;  
    
    var homeLayout = CurrentPage.homepageLayout;
    var promoSpots = 0;
    var sortedPosts = CurrentPage.Site().FirstChild("wSHome").Children("article").Where("Visible").OrderBy("Promoted desc, CreateDate desc");
    
    @foreach (var post in sortedPosts.Take(promoSpots))
    { 
        <img src="@Umbraco.Media(post.GetPropertyValue("promotionImage", altFieldAlias: "articlePreviewImage")).GetCropUrl("thumb")" alt="@Umbraco.Media(post.GetPropertyValue("promotionImage", altFieldAlias: "articlePreviewImage")).altText" />
    }
    
    @foreach (var post in sortedPosts.Skip(promoSpots).Take(6))
    {
        <img src="@Umbraco.Media(post.GetPropertyValue("articlePreviewImage")).GetCropUrl("thumb")" alt="@Umbraco.Media(post.GetPropertyValue("articlePreviewImage")).altText" />
    }
    

    I'm new to Umbraco so thanks in advance for any help or tips!

  • Dennis Adolfi 1082 posts 6450 karma points MVP 6x c-trib
    Apr 11, 2016 @ 13:51
    Dennis Adolfi
    1

    Hi phaedra.

    To help you, i just need a little more info. What datatype is promotionImage and articlePreviewImage? Image Cropper, Media Picker?

    Looking at the IPublishedContent properties i cant see that the GetPropertyValue(string) method has a altFieldAlias parameter. Maybe you are confusing it with the UmbracoHelper method @Umbraco.Field(string, altFieldAlias = string)? You can probobly use the GetPropertyValue(string) by first checking if promotionImage has value with the method @post.HasValue(string propertyAlias).

    IPublishedContent Properties: https://our.umbraco.org/documentation/Reference/Querying/IPublishedContent/Properties

    Umbraco Helper: https://our.umbraco.org/documentation/reference/Templating/Mvc/views

    Have i understood you correct that you´d like the first loop to render the posts images for the property promotionImage IF it has a value, otherwise fallback and render the image for property articlePreviewImage?

    With a little more info i´d be happy to help and probobly be able to show some code samples.

    All the best / Dennis

  • phaedra 5 posts 88 karma points
    Apr 11, 2016 @ 19:10
    phaedra
    1

    Hi Dennis - thanks so much for your response! I think you're correct that I'm trying to use GetPropertyValue() like @Umbraco.Field's altFieldAlias helper, so that won't probably work.

    As for the datatype, they are both using Media Picker.

    Have i understood you correct that you´d like the first loop to render the posts images for the property promotionImage IF it has a value, otherwise fallback and render the image for property articlePreviewImage?

    Yes! Exactly..

    So I think I want to do something like this:

            @foreach (var post in sortedPosts.Take(promoSpots))
            {
                var postImg = post.promotionImage;
                if (post.HasValue("promotionImage") == false)
                {
                    postImg = post.articlePreviewImage;
                }
                <div class="post">
                    <img src="@Umbraco.Media(post.GetPropertyValue(postImg)).GetCropUrl("thumb")" alt="@Umbraco.Media(post.GetPropertyValue("promotionImage", altFieldAlias: "articlePreviewImage")).altText" />
                    <p class="intro">@post.GetPropertyValue("headline")</p>
                </div>
            }
    

    But I'm getting an ambiguous call error..

    The call is ambiguous between the following methods or properties: 'Umbraco.Web.UmbracoHelper.Media(params int[])' and 'Umbraco.Web.UmbracoHelper.Media(params string[])'

    Any thoughts? And thanks again!!

  • Dennis Adolfi 1082 posts 6450 karma points MVP 6x c-trib
    Apr 11, 2016 @ 19:25
    Dennis Adolfi
    100

    You get a ambiguous call becaus it's possible to pass both a string and a int to the Umbraco.Media() method. And you are trying to pass in a type object.

    Since you are using a mediapicker you won't have to use GetPropertyValue() since postImg will be a number.

    Try:

    Umbraco.Media(postImg.ToString())

  • phaedra 5 posts 88 karma points
    Apr 11, 2016 @ 20:19
    phaedra
    1

    That totally worked & so much cleaner - thank you!!

            @foreach (var post in sortedPosts.Take(promoSpots))
            {
                var postImg = post.promotionImage;
                if (post.HasValue("promotionImage") == false)
                {
                    postImg = post.articlePreviewImage;
                }
                <div class="post">
                    <img src="@Umbraco.Media(postImg.ToString()).GetCropUrl("thumb")" alt="@Umbraco.Media(postImg.ToString()).altText" />
                    <p class="intro">@post.headline</p>
                </div>
            }
    

    One last question: when you said

    "Looking at the IPublishedContent properties"

    where are you looking to find out the properties? I'm having trouble finding the documentation.

  • Dennis Adolfi 1082 posts 6450 karma points MVP 6x c-trib
    Apr 11, 2016 @ 21:17
    Dennis Adolfi
    1

    Awesome to hear that it worked out for you!! :) Happy I could help!!

    Here is the documentation for IPublishedContent: https://our.umbraco.org/documentation/reference/querying/ipublishedcontent/

    All the best! / Dennis

  • phaedra 5 posts 88 karma points
    Apr 11, 2016 @ 21:45
    phaedra
    1

    Awesome - so helpful! Thanks again!

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies