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
Best Answer
-
Kasper Vanilla Staff
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
4
Answers
Had the same "issue" a couple of weeks ago, this should do the trick:
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)
Glad to hear it worked out! I usually store as much stuff as possible in variables just to keep my code super DRY:
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.
Sorry, I'm running 2.0.18.8
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-L84GetAncestors()
is a so-called "static" function (and as such is being called statically using theclass::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.
Hi again Lisa,
I feel like some more basics are in order here, learning a bit about programming as a whole would be a good start. Let's get to it!
First off, randomly editing files, no matter the language, is always a very bad idea. In the above case, take a look at the
$CssClass
-thingy. This is a variable (see how it's prepended with a dollar sign,$
?) that holds information about all the CSS classes we want our discussion to have. It starts on line 5 as being just a string, 'Item', but gradually more classes are being added to the variable using the.=
syntax which tells PHP to append something to the given variable. Later on, you're appending the name of the parent category to this very variable, which will add it as a CSS class to the discussion rather than display it to the user. Not exactly what we wanted.What we want to do, is add the name of the parent category somewhere within these lines of code: https://github.com/vanillaforums/Garden/blob/2.0/applications/vanilla/views/discussions/helper_functions.php#L34-L82. Everything from the opening
<li>
-tag to the closing</li>
-tag is our discussion. We probably want to add it after all the other discussion meta (and as such have it be the last piece of meta-info shown) so adding our code after this line https://github.com/vanillaforums/Garden/blob/2.0/applications/vanilla/views/discussions/helper_functions.php#L78 would be a very good idea. That specialFireEvent()
function ($Sender->FireEvent('DiscussionMeta');
) is actually something we can hook into from a plugin to display the parent category name, but for now we'll just add it directly in the view as it's simpler.Anyways, now we've located where we want our code to go, it's time to reflect on the code itself. This is the function you added in the example you showed:
It's a good start, but it's not quite right yet. Remember how I told you that
GetAncestors()
is a static function? While it does mean that we can easily call it from anywhere and it'll work, we still need to actually feed it some data before it'll work. If you go read through the source of theGetAncestors()
function (https://github.com/vanillaforums/Garden/blob/2.0/applications/vanilla/models/class.categorymodel.php#L389-L444), you'll see that it takes two parameters:$CategoryID
and$CheckPermissions
. In this case, we don't care about$CheckPermissions
as it's default,TRUE
, is exactly what we need. However, we do care about$CategoryID
as the function won't do us any good unless we actually give it a Category ID to work with. In the case of theWriteDiscussion()
function that we're trying to edit, we should be able to get a Category ID from the$Discussion
variable that's fed to function (function WriteDiscussion($Discussion, [...])
):Let's store that in a variable so it's easier to work with:
And now we simply pass that to the
GetAncestors()
function:Bazinga! Let's put that together:
applications/vanilla/views/discussions/helper_functions.php
starting line 75That's all there is to it! If you've got any more questions, feel free to ask away.
// Kasper
Kasper Kronborg Isager (kasperisager) | Freelance Developer @Vanilla | Hit me up: Google Mail or Vanilla Mail | Find me on GitHub
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 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.
Hi Lisa,
No bother at all, the community is here to help!
I'm thrilled to hear that you've learnt a thing or two from playing around with Vanilla! As for the code, I see now that I made a small mistake, one that will hopefully help teach another valuable lesson about PHP.
The word
array
is exactly what we want to see - it means that the "data" we're outputting isn't just a simple string (i.e. the name of the parent category) but instead an array of information about the parent category (maybe name, url, slug, number of discussions etc.). The heck are arrays?An array in PHP is like an "extended" variable that can contain key/value pairs in a multidimensional ("nested") map ("list"). They're invoked using the
array()
function like this:The above information can then be accessed like this:
Which will read: "Jane Doe and John Doe have 2 kids, Jimmy Doe and Jacqueline Doe."
What we need to do with our array, is find out exactly what information it holds. To do so, we need to use one of PHP's many
print
functions, specifically theprint_r()
function - you can read a bit about it here http://php.net/manual/en/function.print-r.php (Excerpt: Prints human-readable information about a variable).First off, let's get the Category ID like before:
Then, we'll store the parent category information array in a variable:
Lastly, we'll spit out the information within that variable so we can take a look at it:
Altogether, out code now looks like this:
What I'll need you to do now, is replace the code from before with the above bit of code and then tell me what information the
$ParentCategory
variable holds. After that, we'll put together the final piece of code for displaying a parent category tag with each discussion.// Kasper
Kasper Kronborg Isager (kasperisager) | Freelance Developer @Vanilla | Hit me up: Google Mail or Vanilla Mail | Find me on GitHub
This should be in the Tutorials @UnderDog please if you can , and @Kasper, you are an excellent Tutor 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)."
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?
I would do that just the way you've described it. Here's the code to look at:
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.
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.
I'm a novice, so let me know if there is anything grossly wrong with this code!
Thanks!