Copied to clipboard

Flag this post as spam?

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


  • Frans de Jong 548 posts 1840 karma points MVP 3x c-trib
    Nov 21, 2014 @ 13:20
    Frans de Jong
    0

    Shared content between languages

    Hi all,

    I'm running into a problem with a multi language site and I can't find a solution for it.

    In the content tab I want the stucture like this:

    Home EN
      |
      |-- news
      |
    Home NL
      |
      |--news
      |
    News Items
      |
      |--news item
      |--news item 2
      |--news item 3

    The news item content has to be visible on the news page.

    The goal is to put all news items in one place and with and make them visible on the languages that the user picks with a datatype. The problem isn't the datatype but I cant find how to get to the news items using a partial view macro.
    CurrentPage.AncestorOrSelf(1) gives me the Rootnode but I can't find a way to go beond that.

    I hope the problem is clear. If not feel free to ask more questions.

    Thanks a lot

     

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Nov 21, 2014 @ 14:03
    Alex Skrypnyk
    0

    Hi Frans,

    Interesting structure for the site. How will you manage multilingual issue on the news item ?

    Try to solve your problem using UmbracoHelper and id of the NewsItems node.

    var newsItemsParent = helper.TypedContent(1212); newsItemsParent.Children();

    Thanks

  • jivan thapa 194 posts 681 karma points
    Nov 23, 2014 @ 10:06
    jivan thapa
    0

    This may help you.

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @{
    
        const string newsItemsAlias = "Alias of News Items Document Type";
    
        // find 'News Items' node 
       // var newsItems = Umbraco.TypedMediaAtRoot().FirstOrDefault(x => x.DocumentTypeAlias.Equals(newsItemsAlias));
    
        var newsItems = Umbraco.TypedContentAtRoot().FirstOrDefault(x => x.DocumentTypeAlias.Equals(newsItemsAlias));
    
        if (newsItems != null)
        {
            foreach (var newsItem in newsItems.Children)
            {
                @newsItem.Name
            }
        }
    }
    
  • Frans de Jong 548 posts 1840 karma points MVP 3x c-trib
    Nov 24, 2014 @ 09:30
    Frans de Jong
    0

    Goodmorning,

    @jivan, I tried your method. I like the fact that it doesn't use a hardcoded id so the customer can't screw it up. The variable however stays empty. I don't get why...

    @ Alex, You have put me in the right direction for the solution. I used Umbraco.TypedContent(id); About the reason for the site structure. our customer doesn't want to translate every news item in every language. He want's to choose what language shows witch news item.

    So I have a working solution now but I'm still interested in a way to search on documtentypealias.

  • jivan thapa 194 posts 681 karma points
    Nov 24, 2014 @ 10:08
    jivan thapa
    0

    @Frans sorry, it was my fault.

    var newsItems = Umbraco.TypedMediaAtRoot().FirstOrDefault(x => x.DocumentTypeAlias.Equals(newsItemsAlias));
    

    should be

     var newsItems = Umbraco.TypedContentAtRoot().FirstOrDefault(x => x.DocumentTypeAlias.Equals(newsItemsAlias));
    

    , since its the content. I am stupid.

  • Frans de Jong 548 posts 1840 karma points MVP 3x c-trib
    Nov 24, 2014 @ 10:11
    Frans de Jong
    0

    Hi Jivan,

    You are most cirtainly not stupid! You've brought me the perfect solution. I have read over it multiple times and I didn't see it too.
    Thanks a lot!

    Kind regards,

    Frans

  • Frans de Jong 548 posts 1840 karma points MVP 3x c-trib
    Dec 12, 2014 @ 12:21
    Frans de Jong
    0

    What I have now is this:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @{
        var root = CurrentPage.AncestorOrSelf(1);
        const string newsAlias = "News";
        string rootLanguage = root.language;
        int catId = -1;
        Int32.TryParse(Request.QueryString["Category"], out catId);
        @* find 'News Items' node*@
        var news = Umbraco.TypedContentAtRoot().FirstOrDefault(x => x.DocumentTypeAlias.Equals(newsAlias));
        var newsItems = news.DescendantOrSelf("NewsItems");
        string leftRight = "left";
        HashSet<int> CurrentLanguageNewsItems = new HashSet<int>{};
        HashSet<string> CurrentLanguageCategories = new HashSet<string>{};
    
        if (newsItems != null)
        {
            foreach (var newsItem in newsItems.Children.Where("visible"))
            {
                int currentNodeId = newsItem.Id;
                var currentNodeContent = Umbraco.Content(currentNodeId);
                if(Array.IndexOf(currentNodeContent.showInLanguage.Split(','), @rootLanguage) >= 0)
                {
                    CurrentLanguageNewsItems.Add(@currentNodeId);
                    foreach(var category in currentNodeContent.category.Split(','))
                    {
                        CurrentLanguageCategories.Add(@category);
                    }
                }  
            }
        }   
    
        @* build news category navigation*@
        string currentClass = "class=current";
        <div class="subNav">
            <ul id="labelMenu" class="clear">
                <li @if(@catId < 1000 ){@currentClass}><a href="/news">All news</a></li>
                @foreach (string langCategory in CurrentLanguageCategories)
                {   
                var currentLangCat = Umbraco.Content(@langCategory);
                    <li @if(@catId == @currentLangCat.Id){@currentClass}>
                        <a href="/[email protected]">
                            @switch (@rootLanguage)
                            { 
                                case "EN":
                                @currentLangCat.EN
                                break;
                                case "NL":
                                @currentLangCat.NL
                                break;
                            }   
                        </a>
    
                    </li>
                }
            </ul>
        </div>
    
        @* Get news items with correct language and correct catergory*@
        <div class="newsItems">
            @if (newsItems != null)
            {
                foreach (int newsItem in CurrentLanguageNewsItems)
                {
                    int currentNodeId = newsItem;
                    var currentNodeContent = Umbraco.Content(currentNodeId);
                    if( (catId < 1000) || ( (catId >= 1000) && (Array.IndexOf(currentNodeContent.category.Split(','), @catId.ToString()) >= 0) ) ){
                        <section class="section contentBlock @leftRight clear">
                            <div class="contentBlockText pad">
                                <ul class="itemLabels clear">
                                    @foreach (string itemCategories in currentNodeContent.category.Split(',')){
                                        <li>
                                            @switch (@rootLanguage)
                                            { 
                                                case "EN":
                                                @Umbraco.Content(@itemCategories).EN
                                                break;
                                                case "NL":
                                                @Umbraco.Content(@itemCategories).NL
                                                break;
                                            }   
                                        </li>
                                    }
                                </ul>
                                <h2>@currentNodeContent.newsTitle</h2>
                                <div class="textIntro clear">
                                    @currentNodeContent.newsText
                                </div>
                                @if(@currentNodeContent.extendedText.ToString() != "")
                                {
                                    <div class="extendedText clear">
                                        @currentNodeContent.extendedText
                                        <a href="#" class="readMore medium large">Read more</a>
                                    </div>
                                }   
                            </div>
                            @if((@currentNodeContent.newsImage).ToString() != ""){
                            }
                        </section>
                        switch (@leftRight){
                            case "left":
                            leftRight = "right";
                            break;
                            case "right":
                            leftRight = "left";
                            break;
                        }
                    }
                }       
            }
        </div>  
    }
    

    As you'll notice I need a to get to the content with the Umbraco helper. This seems awkward and gives me heeps of trouble when getting the images. Is there a way to simplify this?

    Another thing that's bothering me is the two switches I have to use. The rootLanguage string in the beginning gives me the country code. I wanted to make it work like @currentLanguageCat.@rootLanguage But that of course doesn't work.... I'd like to be able to leave the script alone if I have to add a language.

    Bare in mind, I'm a front-end developer and have been actively working with Umbraco for the past year. Before that I had no MVC C# experience whatsoever. If you see things that are not as neat as can be, please feel free to comment.

    Thanks

Please Sign in or register to post replies

Write your reply to:

Draft