Copied to clipboard

Flag this post as spam?

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


  • David 29 posts 200 karma points
    Mar 11, 2019 @ 17:36
    David
    0

    Page Header Image

    Could anyone help with the following:

    I have a header which has a background image which is set on the homepage, but you can set the header image per page. If a page doesnt have a header image set, it inherits from the homepage media picker value.

    Here is it working where a page inherits from the homepage.

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
    int imageId = Umbraco.AssignedContentItem.GetPropertyValue<int>("topBannerImage");
    string topBannerImage = imageId > 0 ? Umbraco.Media(imageId).Url : string.Empty;
    
    if (string.IsNullOrWhiteSpace(topBannerImage))
    {
        var homePageImage = Umbraco.AssignedContentItem.Ancestor(1).GetPropertyValue<IPublishedContent>("topBannerImage").Url;
        topBannerImage = homePageImage;
    }
    

    }

    What I would like to add in, is I want it to check the parent page first and see if that parent page has an image. If it does, use it, else use the homepage.

    This is what I have trying to get the parent image:

    @{
    
    int imageId = Umbraco.AssignedContentItem.GetPropertyValue<int>("topBannerImage");
    string topBannerImage = imageId > 0 ? Umbraco.Media(imageId).Url : string.Empty;
    
    if (string.IsNullOrWhiteSpace(topBannerImage))
    {
    
        var homePageImage = Umbraco.AssignedContentItem.Ancestor(1).GetPropertyValue<IPublishedContent>("topBannerImage").Url;
        var parentPageImage = Model.Content.Parent.GetPropertyValue<IPublishedContent>("topBannerImage").Url;
    
        topBannerImage = homePageImage;
        if(parentPageImage == null)
        {
            topBannerImage = homePageImage;
        }
        else
        {
            topBannerImage = parentPageImage;
        }
    
    
    }
    

    }

    The above is not working with getting the parent image. Can anyone see where I am going wrong? Any help on this would be much apprenticed.

  • Bryna 74 posts 260 karma points
    Mar 11, 2019 @ 19:44
    Bryna
    0

    Based on the way you described the functionality, have you thought about going down the path of utilizing MVC Sections?

    The only potential drawback that I immediately see is that you may have to provide a property for multiple doctypes. That will largely depend on how your site is structured. If the image is part of a composite used for every webpage, it might be less of an issue.

    Below is some code which should allow you to use @FallBackimg.src to retrieve a path, but it uses an ImageCropper as opposed to a Media Type:

    ImageCropperValue FallBackimg = (ImageCropperValue)Model.Value(x => x.MyImage , fallback: Fallback.To(Fallback.Ancestors , Fallback.DefaultValue));
    
  • Nigel Wilson 945 posts 2077 karma points
    Mar 11, 2019 @ 20:06
    Nigel Wilson
    100

    Hi David

    When you say it's not working, what error are you getting?

    One possible issue is that for the parent page you are not checking there is an image assigned before trying to get the Url for the image.

    So maybe change this line

    var parentPageImage = Model.Content.Parent.GetPropertyValue<IPublishedContent>("topBannerImage").Url;
    

    to

    IPublishedContent parentPageImage = Model.Content.Parent.GetPropertyValue<IPublishedContent>("topBannerImage")!= null ? Model.Content.Parent.GetPropertyValue<IPublishedContent>("topBannerImage").Url : null;
    

    Hope this helps

    Nigel

  • David 29 posts 200 karma points
    Mar 12, 2019 @ 18:05
    David
    0

    thanks for this nigel. worked like a charm.

    Is there a way to extend this so if the parent doesnt have an image, go up to the next parent and check there? then keep checking the parents until it reaches the root? so if no parents have an image set, use the root image?

  • Nigel Wilson 945 posts 2077 karma points
    Mar 12, 2019 @ 18:51
    Nigel Wilson
    0

    Hi David

    Pleased it solved the issue.

    There is nothing I immediately know of with respect to automatic recursion for an IPublishedContent object.

    You may have to write your own helper method, however someone else might be able to provide some ideas

    Cheers

    Nigel

Please Sign in or register to post replies

Write your reply to:

Draft