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
    Apr 12, 2019 @ 07:53
    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 80 posts 456 karma points c-trib
    Apr 12, 2019 @ 10:12
    Corné Strijkert
    1

    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
    Apr 12, 2019 @ 13:05
    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 80 posts 456 karma points c-trib
    Apr 12, 2019 @ 14:15
    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
    Apr 16, 2019 @ 09:16
    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 422 posts 2334 karma points MVP 4x hq c-trib
    Apr 16, 2019 @ 09:38
    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 80 posts 456 karma points c-trib
    Apr 16, 2019 @ 10:15
    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 422 posts 2334 karma points MVP 4x hq c-trib
    Apr 16, 2019 @ 10:35
    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 80 posts 456 karma points c-trib
    Apr 16, 2019 @ 10:52
    Corné Strijkert
    1

    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
    Apr 16, 2019 @ 11:18
    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 17 posts 131 karma points
    Apr 16, 2019 @ 16:00
    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>
    
  • k 256 posts 654 karma points
    Jan 24, 2020 @ 07:45
    k
    0

    Hello,

    I am using the same code as above but I am getting an empty image.

    Can someone please help.

    Thanks,

    Kusum

Please Sign in or register to post replies

Write your reply to:

Draft