HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Name of parent category

SubJunkSubJunk ✭✭
edited October 2013 in Tutorials

Hi guys :) Is there a variable that contains the name of a parent category while I'm browsing a thread in the child category?

Best Answer

Answers

  • KasperKasper Vanilla Staff
    Answer ✓

    Had the same "issue" a couple of weeks ago, this should do the trick:

    CategoryModel::GetAncestors($CategoryID)
    

    Kasper Kronborg Isager (kasperisager) | Freelance Developer @Vanilla | Hit me up: Google Mail or Vanilla Mail | Find me on GitHub

  • Thanks for the answer, that works for me with one small edit:

    CategoryModel::GetAncestors($this->CategoryID)

  • KasperKasper Vanilla Staff

    Glad to hear it worked out! I usually store as much stuff as possible in variables just to keep my code super DRY:

    $CategoryID = $this->CategoryID;
    

    Kasper Kronborg Isager (kasperisager) | Freelance Developer @Vanilla | Hit me up: Google Mail or Vanilla Mail | Find me on GitHub

  • Hi guys,

    Forgive me for sounding dumb, but which file should I insert the above code into to display parent category on child category pages please?
    Also, would you happen to know how to display parent category as well as child in discussion links? (see attached pic).

    Many thanks,
    Lisa.

    image

  • Sorry, I'm running 2.0.18.8 :)

  • KasperKasper Vanilla Staff

    Hi Lisa,

    It depends on where you want to show the parent category. In your case, you're probably looking for the WriteDiscussion() function which is responsible for outputting the discussion list as seen in the screenshot you posted: https://github.com/vanillaforums/Garden/blob/2.0/applications/vanilla/views/discussions/helper_functions.php#L3-L84

    GetAncestors() is a so-called "static" function (and as such is being called statically using the class::method() syntax; CategoryModel::GetAncestors()) so it'll work anywhere if you've got a valid Category ID available. Simply put it in one of your views, pass in a Category ID and you're done!

    // Kasper

    Kasper Kronborg Isager (kasperisager) | Freelance Developer @Vanilla | Hit me up: Google Mail or Vanilla Mail | Find me on GitHub

  • Hi Kasper,

    Thank you so much for looking at this for me.
    I have the helper_functions.php file from my theme open, and am trying to figure out how to add CategoryModel::GetAncestors();
    to make it work.
    I tried to insert it under $CssClass (see screen print - line 13) and I've also tried inserted it on its own instead - all within WriteDiscussion().
    I'm unsure of where to attach it to my view (would this be the views\discussion\index.php file?

    I know I'm probably being really dumb about this.

    I'm very sorry. I'm fairly competent at finding & editing files, but my knowledge is fairly limited.

    I do appreciate your time on this.

    Lisa.

    image

  • Kasper, you my sir are a superstar!!
    I shall work through this now and let you know how it goes.

    Thank you so much!!!

  • Hi Kasper,

    Firstly, I'm sorry if I'm any bother.

    I have read your comment (and I can even say I've learned some valuable facts from you too - thank you).

    • I can honestly say that building my Vanilla forum has taught me more over the past two months than self-taught web development has over the past ten years! I am certain that it is down to this community being so helpful. I have asked so many questions and had them all answered (and explained). For that I am very grateful.

    I inserted the code into the vanilla helper_functions and my theme's helper_functions too (just in case that was the reason it wasn't working).
    Where the category is displayed in the discussions box now has the word 'array' after it.

    I inserted the code after line 75 up to & including .

    Thank you Kasper.

  • This should be in the Tutorials @UnderDog please if you can :) , and @Kasper, you are an excellent Tutor <3 thanks for this...

  • Kasper, you're just brilliant! Thank you so much!

  • @marthajane77 said in the sixth post in this thread:
    "would you happen to know how to display parent category as well as child in discussion links? (see attached pic)."

    image

    I have the same question. However I don't want to edit core files, I'm writing a plugin.

    So, I'd love to show the category "chain" up there - like instead of "READER'S RIDES" above, it might show "VEHICLES > READER'S RIDES" or whatever.

    The DiscussionsController_DiscussionMeta_Handler function is fired right after the "READER'S RIDES" above. If it were right before then I could just echo the parent category name, but since it's after then I think I have to use css to hide "READER'S RIDES" and then echo parent category and child category.

    But then it all gets a bit hacky.. There's gotta be a simpler way?

  • R_JR_J Admin

    I would do that just the way you've described it. Here's the code to look at:

                $Sender->FireEvent('AfterCountMeta');
    
                if ($Discussion->LastCommentID != '') {
                   echo ' <span class="MItem LastCommentBy">'.sprintf(T('Most recent by %1$s'), UserAnchor($Last)).'</span> ';
                   echo ' <span class="MItem LastCommentDate">'.Gdn_Format::Date($Discussion->LastDate, 'html').'</span>';
                } else {
                   echo ' <span class="MItem LastCommentBy">'.sprintf(T('Started by %1$s'), UserAnchor($First)).'</span> ';
                   echo ' <span class="MItem LastCommentDate">'.Gdn_Format::Date($Discussion->FirstDate, 'html');
    
                   if ($Source = GetValue('Source', $Discussion)) {
                      echo ' '.sprintf(T('via %s'), T($Source.' Source', $Source));
                   }
    
                   echo '</span> ';
                }
    
                if (C('Vanilla.Categories.Use') && $Category)
                   echo Wrap(Anchor(htmlspecialchars($Discussion->Category), CategoryUrl($Discussion->CategoryUrlCode)), 'span', array('class' => 'MItem Category '.$Category['CssClass']));
    
                $Sender->FireEvent('DiscussionMeta');
    

    Another (even more hacky) option would be to hook AfterCountMeta and set 'Vanilla.Categories.Use' to false so that code for the categories output isn't executed. Than also hook DiscussionMeta and insert you code and set 'Vanilla.Categories.Use' to true again.

    But those are the only two options I can think of and to my opinion, your idea is the preferred way.

  • edited March 2015

    Thanks @R_J,

    Thanks for your thoughts. In the end I wanted the least hacky option. So that it's futureproof. So in the end I just echoed the parent category in square brackets after the child category. So, where it used to say "Child" now it says "Child [PARENT]".

    Here is the code for anyone's future reference.

    public function DiscussionsController_DiscussionMeta_Handler($Sender) {
      $CategoryID = $Sender->EventArguments['Discussion']->CategoryID;
      $ParentCatID = reset(CategoryModel::GetAncestors($CategoryID)); // reset() gives us the first value, which is the immediate parent category ID
      echo(' ['.strtoupper($ParentCatID['Name']).']');
    }
    

    I'm a novice, so let me know if there is anything grossly wrong with this code!

    Thanks!

Sign In or Register to comment.