Copied to clipboard

Flag this post as spam?

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


  • Hundebol 167 posts 314 karma points
    1 week ago
    Hundebol
    0

    Fallback on crop url in V8

    Hi umbracians,

    Testing umbraco v8 i would like to show an image on my multilingual website. I have danish and english language. It should be possible to have different images on each language, but if no image is set on danish, it should fallback to the english image. In this case it is a image cropper data type, but could be a image picker as well.

    It works fine if i do this:

    @Model.Value("introImage", fallback: Fallback.ToLanguage)
    

    But what if I want to have the cropped image?

    <img src="@Url.GetCropUrl(Model, propertyAlias: "introImage", height: 550, width: 550)" class="banner-image img-responsive" )" />
    

    How can i implement fallback here? Is it possible?

  • Corné Strijkert 45 posts 260 karma points
    1 week ago
    Corné Strijkert
    0

    Hi Hundebol,

    Try this:

    @Model.Value<IPublishedContent>("introImage", fallback: Fallback.ToLanguage)?.GetCropUrl("cropAlias")
    

    The GetCropUrl() is an extensionmethod on IPublishedContent, so you first can get the fallback IPublishedContent and then use the GetCropUrl() on that IPublishedContent

  • Hundebol 167 posts 314 karma points
    1 week ago
    Hundebol
    0

    Hi Corné,

    Thanks, but that does not seem to work. I get the following: CS1503: Argument 1: cannot convert from 'method group' to 'HelperResult' :(

    Other ideas?

  • Corné Strijkert 45 posts 260 karma points
    1 week ago
    Corné Strijkert
    0

    I'm not on my PC, but try removing the ) at the end of my code example. I think that throws the exception

  • Hundebol 167 posts 314 karma points
    6 days ago
    Hundebol
    0

    I did see the little typo, and had removed that. But no luck. Still the same error. There must be a way :(

  • Andy Butland 355 posts 1888 karma points MVP 2x c-trib
    6 days ago
    Andy Butland
    0

    I couldn't get this working either - have created a PR to add some overloads to the GetCropUrl method that should allow using language and fallbacks. Not sure if a better way already exists, but will perhaps see when the PR is reviewed.

    Andy

  • Corné Strijkert 45 posts 260 karma points
    6 days ago
    Corné Strijkert
    0

    Interesting, I have no errors and it works as expected.

    I'm trying to understand the error you're getting. Can it be you must surround your code with ( and ), like this:

    <img src="@(Model.Value<IPublishedContent>("image", fallback: Fallback.ToLanguage)?.GetCropUrl("card"))" />
    
  • Andy Butland 355 posts 1888 karma points MVP 2x c-trib
    6 days ago
    Andy Butland
    0

    I think this issue is that the field is of type "Image Cropper" (rather than "Media Picker") - and hence you don't get an IPublishedContent back?

  • Corné Strijkert 45 posts 260 karma points
    6 days ago
    Corné Strijkert
    0

    I think you're right, but I don't get any errors when I change my property from Media picker type to Image Cropper type.

    When using Image Cropper directly on content type there is indeed a issue with getting the fallback value, so good job @Andy you made a PR for this.

    (Temporary?) solution maybe for @Hundebol is moving to Media picker instead of the Image Cropper directly. That is how my example was configured.

  • Hundebol 167 posts 314 karma points
    6 days ago
    Hundebol
    0

    Hi Andy and Corné,

    Thanks the effort. Really appreciated. This was driving me nuts, so glad to see that there actually seems to be an issue. I will let this thread be unsolved until we know further about Andy's PR :)

    Thanks for the help so far you guys :)

  • Simon Andrews 11 posts 103 karma points
    6 days ago
    Simon Andrews
    0

    Hi,

    I worked around this by checking the model value first to see if it was null and rendering a constant if it was:

    @{
        Layout = "Master.cshtml";
        var searchResultImage = Model.SearchResultImage;
    }
    
    <div>
        @if (searchResultImage != null)
        {
            <img src="@Url.GetCropUrl(Model, "searchResultImage", "Search Result")" />
        }
        else
        {
            <img src="@Constants.DefaultValues.DefaultSearchResultImage" />
        }
    </div>
    
Please Sign in or register to post replies

Write your reply to:

Draft