Copied to clipboard

Flag this post as spam?

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


  • Jonathan Crosby 12 posts 33 karma points
    Feb 23, 2014 @ 22:54
    Jonathan Crosby
    0

    Language Dropdown Help Required Umbraco 7.0.3

    Hi there

    I'm struggling to get my language dropdown to work, which had worked prior to switching to RelationServices in 6.1. Anyway, I have now upgraded to 7.0.3 and my code is no longer working.

    The idea behind this code is to switch between the language relations on any level of the tree. If a relation is not available then it should link to the language root.

    @using System.Linq;
    @using Umbraco.Core;
    @using Umbraco.Core.Models;
    @using Umbraco.Core.Services;
    @using umbraco.MacroEngines;
    @inherits umbraco.MacroEngines.DynamicNodeContext

    @{
    var TopLanguageNode = Model.AncestorOrSelf(1);
    DynamicNode contentRoot = new DynamicNode(-1);
    IRelationService rs = ApplicationContext.Current.Services.RelationService;
    IEnumerable<IRelation> relations = rs.GetAllRelations((int)Model.Id).Where(r => r.RelationType.Alias == "relateDocumentOnCopy");

    }
    <form class="custom">
    <select name="langselection" onchange="window.open(this.options[this.selectedIndex].value,'_top')">
    @foreach (DynamicNode LanguageRoots in contentRoot.Descendants("LanguageTopFolder"))
    {
    var curLanguage = LanguageRoots.GetProperty("Language").Value.ToString();
    var curUrl = LanguageRoots.Url;

    foreach(IRelation relation in relations)
    {
    int relationId = relation.ParentId;
    if(relationId == Model.Id)
    {
    relationId = relation.ChildId;
    }
    var TopLangPage = Model.NodeById(relationId).AncestorOrSelf(1);

    if(curLanguage == TopLangPage.Language)
    {
    curUrl = Model.NodeById(relationId).Url;
    }
    }

    if(curLanguage == TopLanguageNode.Language)
    {
    <option value="@curUrl" selected="selected">@curLanguage</option>
    }
    else
    {
    <option value="@curUrl">@curLanguage</option>
    }
    }
    </select>
    </form>

     

    Please help. The Relations and the DynamicNodes are no longer working. Any ideas?

  • Jonathan Crosby 12 posts 33 karma points
    Feb 23, 2014 @ 23:02
    Jonathan Crosby
    0

    Here is a picture of my top level, I have 3 language roots: de (german), fr (French) and it (Italian) and all was related on copy. As I want to be able to manually format and name the languages (not using the default Culture info), I have created a Property "Language" on each root.

     

    The dropdown appears with the text: Undefined

  • Simon Dingley 1474 posts 3431 karma points c-trib
    Feb 24, 2014 @ 09:27
    Simon Dingley
    0

    If I understand what you're trying to achieve I'm not entirely sure whether you even need a relation as you could easily achieve what it looks like you are trying to do using a simple content query?

    That said, have you checked the umbracoRelation table to see if the relations you are expecting to see do actually exist? If it does then can you check that your relations variable contains results? My question would be to then check that you have the parentId and childId are not the reverse of what you are checking against e.g. your parent is not in fact the child and vice versa.

    Simon

  • Jonathan Crosby 12 posts 33 karma points
    Feb 25, 2014 @ 00:07
    Jonathan Crosby
    0

    Hi Simon

    Thanks for your support. This helped me narrow down the problem. Yes, there was data in the tables umbracoRelation and umbracoRelationType. Different than in your example I had to use IRelationService and IRelation, has this changed since you documented this?

    Now this line is returning the correct values:
    IEnumerable<IRelation> relations = rs.GetByParentOrChildId((int)Model.Id).Where(r => r.RelationTypeId == 1);

    However it won't work with the Alias name strangely enough, however I can use the Id instead.

    My main diffultity I have now if the following statment is no longer looping through my level 1 nodes. Any ideas?

    @foreach(DynamicNodeLanguageRootsin contentRoot.Descendants("LanguageTopFolder"))

    Best regards

  • Simon Dingley 1474 posts 3431 karma points c-trib
    Feb 25, 2014 @ 10:01
    Simon Dingley
    0

    RelationService and Relation implement IRelationService and IRelation respectively, there should be nothing wrong with either approach. I've had a chance to review again and I can now see the issue with your original code.

    The method signature for .GetAllRelations is as follows:

    .GetAllRelations(params int[] ids)

    You are passing the Id of your Model in but the method requires an array of relation object id's and explains why you get no results. What you actually want to do is get all relations for your object using one of the following methods...

    .GetByParentId(int id) Gets an enumerable list of Relation objects that have the specified ParentId.

    .GetByChildId(int id) Gets an enumerable list of Relation objects that have the specified ChildId.

    .GetByParentOrChildId(int id) Gets an enumerable list of Relation objects that have the specified ParentId or ChildId.

    ...then filter based on the relationtype alias using the Where clause, something like this:

    IEnumerable<IRelation> relations = rs.GetByParentId((int)Model.Id).Where(r => r.RelationType.Alias == "relateDocumentOnCopy");
    

    My main diffultity I have now if the following statment is no longer looping through my level 1 nodes. Any ideas?

    @foreach(DynamicNodeLanguageRootsin contentRoot.Descendants("LanguageTopFolder"))

    You are missing space in the code you posted which should be:

    @foreach(DynamicNode LanguageRoots in contentRoot.Descendants("LanguageTopFolder"))
    

    Also make sure your document type alias is not languageTopFolder (lower case 'l').

    I hope that helps?

    Simon

  • Jonathan Crosby 12 posts 33 karma points
    Feb 25, 2014 @ 10:21
    Jonathan Crosby
    0

    Hi Simon

    Many thanks for your help. My last post contained a copy paste error, in my original post the line of code had the correct spacing, so hence this is not the issue.

    My impression is that the following code no longer works in the current version, but I cannot find an example on how to get it working in the new version:

    DynamicNode contentRoot = new DynamicNode(-1);
    @foreach(DynamicNode LanguageRoots in contentRoot.Descendants("LanguageTopFolder"))
    {
          do something

    }

    Thanks for your help with the relations:
    This works:

    IEnumerable<IRelation> relations = rs.GetByParentId((int)Model.Id).Where(r => r.RelationType.Id== 1);

    yet this doesn't work:

    IEnumerable<IRelation> relations = rs.GetByParentId((int)Model.Id).Where(r => r.RelationType.Alias=="relateDocumentOnCopy");

     

    I have checked in the UmbracoRelationType table and I have 1 entry: Id = 1, Alias = relateDocumentOnCopy

    so I really don't understand why the Alias Lambda expression doesn't work.

    Any ideas on these 2 issues? Just a thought, has there been a change in using double quotes?

    Cheers
    Jonathan

  • Simon Dingley 1474 posts 3431 karma points c-trib
    Feb 25, 2014 @ 11:20
    Simon Dingley
    0

    Have you looked at the contents of contentRoot.Descendants()? Do you see the nodes you expect? Did you also check the casing of the document type alias?

    My suggestion is also to inspect the contents of rs.GetByParentId((int)Model.Id) and check the alias against the node you are expecting to be returned.

    Just a thought, has there been a change in using double quotes?
    

    Double quotes are not an Umbraco thing (specifically) they are used in .Net to declare a string.

    Simon

  • Jonathan Crosby 12 posts 33 karma points
    Feb 25, 2014 @ 21:04
    Jonathan Crosby
    0

    Hi Simon

    Okay I inspected rs.GetByParentId((int)Model.Id) and first I just received timeouts, then suddenly it started working and I was able to see the RelationType.Alias. I have changed it back & it is now working.

    Regarding the other issue, there appears to be a lot of people with the same issue:
    http://our.umbraco.org/forum/developers/api-questions/47125-How-to-get-the-root-node-in-Umbraco-67

    Do you or does anyone else know what is the best way to access the true root? I need to be able to loop for all possible languages and this is generic code I want to use on all websites, hence it must be independant of the tree structure.

    Many thanks
    Jonathan

     

     

  • Simon Dingley 1474 posts 3431 karma points c-trib
    Feb 26, 2014 @ 10:03
    Simon Dingley
    0

    I wasn't aware of any issues getting the content root, I'd need to check on that however you could just as easily get all nodes at level 1 using the .GetByLevel(1) method on the ContentService.

    Please make sure to vote up any answers you find useful and also the final solution :)

  • Jonathan Crosby 12 posts 33 karma points
    Feb 27, 2014 @ 23:58
    Jonathan Crosby
    0

    Hi Simon

    Thanks again for your help. The RelationService is fully functioning with your help.

    The following code appears to be working for me:

    var contentRoot = umbracoHelper.TypedContentAtRoot().ToList();
    @foreach (var LanguageRoots in contentRoot)
    {
    do something
    }

    However I have not managed to figure out why on some pages the language picker works and on others it doesn't. I hope to spend some time this weekend to further analyse this strange behaviour.

    On some pages I get: "Error loading MacroEngine script (file: LanguageSelector.cshtml)". I'll debug some more and try to find out what is going on.

    Cheers
    Jonathan

     

Please Sign in or register to post replies

Write your reply to:

Draft