Copied to clipboard

Flag this post as spam?

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


  • Ben 112 posts 398 karma points
    Dec 23, 2015 @ 13:46
    Ben
    0

    Ancestor field displaying instead of current page field - Umbraco v7.3.4

    Hi,

    I am using Umbraco v7.3.4 and something odd is happening after my upgrade from v7.2.8. I have an Umbraco field item in one of my templates and it works fine as long as there is something in the field. If the field is empty, it appears to be searching it's ancestors until it finds a value for that field and use that, instead of leaving the area blank.

    Here is the code in the template:

    <aside id="asideRelated">
        <umbraco:Item field="relatedInformation" runat="server" />
    </aside>
    

    The field "relatedInformation" is a RichText Editor.

    This is also happening for Textstring fields. Example code:

                <article>
                <umbraco:Item field="alternatePageTitle" useIfEmpty="pageName" insertTextBefore="&lt;h1&gt;" insertTextAfter="&lt;/h1&gt;" runat="server" />
            </article>
    

    In this case I wanted to use the current page name unless alternatePageTitle had a value.

  • Ben 112 posts 398 karma points
    Dec 23, 2015 @ 14:49
    Ben
    0

    As a temporary solution I wrote the below partial view. If there is a better way to do this, please let me know.

     @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @{
        var pageField = Model.MacroParameters["pageField"].ToString();
        var insertBefore = HttpUtility.HtmlDecode(Model.MacroParameters["insertBefore"].ToString());
        var insertAfter = HttpUtility.HtmlDecode(Model.MacroParameters["insertAfter"].ToString());
        var altPageField = Model.MacroParameters["altPageField"].ToString();
    
        var stringToWriteOut = "";
    
        if(CurrentPage.HasValue(pageField))
        {
            if(!string.IsNullOrEmpty(insertBefore)){
                @Html.Raw(insertBefore)
            }
    
            if(altPageField == "pageName")
            {
                @CurrentPage.Name
            }
            else
            {
                @Html.Raw(CurrentPage.GetPropertyValue(pageField))
            }
    
            if(!string.IsNullOrEmpty(insertAfter)){
                @Html.Raw(insertAfter)
            }       
        }
        else if(!string.IsNullOrEmpty(altPageField))
        {
            if(CurrentPage.HasValue(altPageField)){
                if(!string.IsNullOrEmpty(insertBefore))
                {
                    @Html.Raw(insertBefore)
                }
    
                @Html.Raw(CurrentPage.GetPropertyValue(altPageField))
    
                if(!string.IsNullOrEmpty(insertAfter))
                {
                    @Html.Raw(insertAfter)
                }
            }
            else if(altPageField == "pageName")
            {
                if(!string.IsNullOrEmpty(insertBefore))
                {
                    @Html.Raw(insertBefore)
                }
    
                @CurrentPage.Name
    
                        if(!string.IsNullOrEmpty(insertAfter))
                {
                    @Html.Raw(insertAfter)
                }
            }
        }   
    }
    
  • gary 385 posts 916 karma points
    Dec 24, 2015 @ 08:55
    gary
    0

    Hi Ben

    If I have understood this correctly, you are putting either the CurrentPage.Name or altPageName in an H1 tag ?

    Or am I missing something?

    Regards

    Gary

  • Ben 112 posts 398 karma points
    Dec 24, 2015 @ 12:17
    Ben
    0

    Hi Gary,

    Yes, I was trying to put either the altPageName or CurrentPage.Name inside an H1 tag.

  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Dec 24, 2015 @ 10:34
    Marc Goodson
    1

    Hi Ben

    Inside a macro partial view you can use the syntax:

    @Umbraco.Field("alternatePageTitle", altFieldAlias: "pageName", insertBefore: "&lt;h1&gt;", insertAfter: "&lt;/h1&gt;")
    

    or

    <h1>@Umbraco.Field("alternatePageTitle", altFieldAlias: "pageName")</h1>
    

    to show the value of a field but default to another altField if it is blank...

    you can make this recursive too, in fact to see all the options, if you go into the back office, and open up a macro partial, and press the little 'field button' you get a helpful form to build the syntax...

    enter image description here

  • Ben 112 posts 398 karma points
    Dec 24, 2015 @ 12:18
    Ben
    0

    Thanks Marc! I will try that out.

  • Ben 112 posts 398 karma points
    Dec 24, 2015 @ 13:40
    Ben
    0

    I found that I couldn't put it directly into the template unless I put in an Inline Razor Macro, I think that is because the site uses WebForms and not MVC.

    I have changed my partial view the the following:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @{
        var pageField = Model.MacroParameters["pageField"].ToString();
        var insertBeforeString = HttpUtility.HtmlDecode(Model.MacroParameters["insertBefore"].ToString());
        var insertAfterString = HttpUtility.HtmlDecode(Model.MacroParameters["insertAfter"].ToString());
        var altPageField = Model.MacroParameters["altPageField"].ToString();
    
    
        @Umbraco.Field(pageField, altFieldAlias: altPageField, insertBefore: insertBeforeString, insertAfter: insertAfterString)
    }
    

    It's still a little funny that if I insert a page field normally and the page field is empty that goes up its ancestors until it finds a value.

    <h1><umbraco:Item field="alternatePageTitle" useIfEmpty="pageName" runat="server" /></h1>
    

    Thanks for your help Marc.

Please Sign in or register to post replies

Write your reply to:

Draft