Copied to clipboard

Flag this post as spam?

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


  • DonZalmrol 220 posts 833 karma points
    Sep 13, 2018 @ 19:20
    DonZalmrol
    0

    2/2 Umbraco tags filter out multiple tags that lead to the same page

    In continuation of the previous that stopped working due to a bug on the forum.

    First topic:

    Hi all,

    So I have my tags up and running for my site and have multiple tags that lead to the same page.

    Example: a project page that talks about a headphone amplifier has the tags "OTL, headphone amplifier, tubes, 12AU7-6CG7, ..." listed as tags.

    URL: https://www.don-zalmrol.be/projects/headphone-amplifier/

    My tag filter works and displays the project under it's tag without displaying any other tag. So now I wish create a "tag" or fixed href to show "all tags".

    Tag page URL : https://www.don-zalmrol.be/tags?tag=12AU7-6CG7

    In a way this works, but it displays the project multiple times because of the multiple tags in my projects.

    So now I am looking for a way to only display 1 project even if it has more then 1 tag.

    You can see this yourself if you go to the all tags page I created: https://www.don-zalmrol.be/tags?tag=all

    You will see everything displayed nicely, but the project(s) appear multiple times because it has more then 1 tag...

    Any ideas or tips?

    Thx!

    *Last topic and continuation * enter image description here

    GitHub link = https://github.com/DonZalmrol/Umbraco

  • DonZalmrol 220 posts 833 karma points
    Sep 13, 2018 @ 19:29
    DonZalmrol
    0

    Hi Jamie,

    Still some issues, but we are getting there. Seems that the all tags view is now showing only 3 projects from the tags and if you click on a tag button itself it does not show you anything with exception of the all tags button.

    enter image description here

  • DonZalmrol 220 posts 833 karma points
    Sep 13, 2018 @ 19:29
    DonZalmrol
    0

    strong text

  • Jamie Freeman 38 posts 151 karma points
    Sep 13, 2018 @ 21:22
    Jamie Freeman
    0

    Back with you now - let me take a quick look

  • Jamie Freeman 38 posts 151 karma points
    Sep 13, 2018 @ 21:30
    Jamie Freeman
    0

    Amended the code again it's all in matching up the variable names (also I have move instantiating the tagList(tags) out of the foreach loop to bring it back into scope. Apologies - writing blind :-)

  • DonZalmrol 220 posts 833 karma points
    Sep 14, 2018 @ 07:36
    DonZalmrol
    0

    No prob, I understand :)

    Getting close! It is fetching projects from my IT Blog but not all of them and nothing from the projects blog.

    enter image description here

  • DonZalmrol 220 posts 833 karma points
    Sep 14, 2018 @ 17:37
    DonZalmrol
    0

    Funny thing is that this if-statement works and lists all the IDs without any issues. Even the duplicates (as nothing filters them out)

    if (tag.Equals("all"))
    {
        foreach (var tagItem in tags)
        {
            @* <p>@tagItem.Id</p> *@
    
            publishedContents = Umbraco.TagQuery.GetContentByTag(tagItem.Text);
    
            foreach (var publishedContent in publishedContents)
            {
                <p>@publishedContent</p>
    
                items[publishedContent.Id] = publishedContent;
            }
        }
    

    When you then list the output of :

    @foreach (var item in publishedContents.OrderByDescending(i => i.Name))
     {
            @item
    }
    

    You only get 3 items back.

    So it seems that this command filters a bit too much.

    items[publishedContent.Id] = publishedContent;
    

    Could it be that it adds all the fetched IDs together and then filter the duplicates out e.g.

    11031104110611061095110510951119111811171099

    Resulting in -> 1031041061061095105109519181709 and so on?

  • Jamie Freeman 38 posts 151 karma points
    Sep 17, 2018 @ 11:32
    Jamie Freeman
    0

    Ok maybe instead of a dictionary - change it to a list of IPublishedContent;

    See if that works.

  • Jamie Freeman 38 posts 151 karma points
    Sep 17, 2018 @ 11:43
    Jamie Freeman
    0
    var items = new List<IPublishedContent>();
    
    foreach (var publishedContent in publishedContents)
    {
      <!-- @publishedContent.Id -->
      items.Add(publishedContent);
    }
    
  • Jamie Freeman 38 posts 151 karma points
    Sep 17, 2018 @ 11:48
    Jamie Freeman
    0

    Swap out this line

    Line 39:                 items[publishedContent] = publishedContent;
    

    For this

    Line 39:                 items.Add(publishedContent);
    

    (Though really you could say)

    if(!items.Contains(publishedContent) {
       items.Add(publishedContent);
    }
    
  • Jamie Freeman 38 posts 151 karma points
    Sep 17, 2018 @ 12:02
    Jamie Freeman
    0

    Laurens - looking at the code again with a colleague change this line

    @foreach (var item in publishedContents.OrderByDescending(i => i.CreateDate))
    

    to use items

    @foreach (var item in items.OrderByDescending(i => i.CreateDate))
    

    the rest can return to how it was previously

    (I'll update github so you can see.)

  • Jamie Freeman 38 posts 151 karma points
    Sep 17, 2018 @ 12:06
    Jamie Freeman
    0

    this line:

    @foreach (var item in items.OrderByDescending(i => i.CreateDate))
    

    should be:

    @foreach (var item in items.OrderByDescending(i => i.Value.CreateDate))
    

    (updated the comment on github)

  • Jamie Freeman 38 posts 151 karma points
    Sep 17, 2018 @ 12:11
    Jamie Freeman
    0

    Updated Git

    @item was a disctionaryItem (KeyValuePair) I've amended the code on github so the item is not an IPublishedContent object (the value of a KeyValuePair)

  • DonZalmrol 220 posts 833 karma points
    Sep 17, 2018 @ 12:12
    DonZalmrol
    0

    Hi Jamie, thx for you help so far!

    Seems I get the following error now

    Compiler Error Message: CS0030: Cannot convert type 'System.Collections.Generic.KeyValuePair<int,Umbraco.Core.Models.IPublishedContent>' to 'System.Collections.Generic.KeyValuePair<string,Umbraco.Core.Models.IPublishedContent>'
    

    On:

        Line 84: 
    Line 85:                 @* Start picture content *@
    Line 86:                 @foreach (KeyValuePair<string,IPublishedContent> kvp in items.OrderByDescending(i => i.Value.CreateDate))
    Line 87:                 {
    Line 88:                    var item = kvp.Value;
    
  • Jamie Freeman 38 posts 151 karma points
    Sep 17, 2018 @ 12:13
    Jamie Freeman
    100
    @foreach (KeyValuePair<int,IPublishedContent> kvp in items.OrderByDescending(i => i.Value.CreateDate))
    

    (I've updated Git :-) )

  • Jamie Freeman 38 posts 151 karma points
    Sep 17, 2018 @ 12:22
    Jamie Freeman
    0

    Laurens - is that all of projects displaying? It looks to be less than the list you had displaying earlier . . .

  • DonZalmrol 220 posts 833 karma points
    Sep 17, 2018 @ 12:22
    DonZalmrol
    0

    It works!

    enter image description here

  • DonZalmrol 220 posts 833 karma points
    Sep 17, 2018 @ 12:23
    DonZalmrol
    0

    I have 11 projects on my personal site. So the output is now correct!

    Many thanks Jamie! :)

  • Jamie Freeman 38 posts 151 karma points
    Sep 17, 2018 @ 12:30
    Jamie Freeman
    1

    Yay !

    No problem - glad it now works. :-)

Please Sign in or register to post replies

Write your reply to:

Draft