Is it possible to modify a function in a helper via the plugin or theme component system?

Is there a way to reopen a class and overwrite a function?

The one I need to overwrite is function categoryStripe(color, classes) {} on line 15 from here: javascripts/discourse/helpers/category-link.js.es6.

I believe so. Have you seen Developer’s guide to Discourse Themes

4 Likes

There are a number of ways to do what you’re asking. What’s your aim on overwriting categoryStripe?

3 Likes

I’d like to change it from:

function categoryStripe(color, classes) {
  var style = color ? "style='background-color: #" + color + ";'" : "";
  return "<span class='" + classes + "' " + style + "></span>";
}

to:

function categoryStripe(color, classes) {
  var style = color ? "style='border: 1px solid #" + color + ";'" : "";
  return "<span class='" + "my-class" + classes + "' " + style + "></span>";
}

So changing background colour to a border and adding a class.

What’s the best way to achieve this Justin?


I did read that, however IIRC it only covers the ability to overwrite templates (and widgets) with no mention of helpers, I think?

1 Like

None of that was obvious from your first post. I think that you’ll now get a useful answer that I’m interested to see!

1 Like

Have you considered adding the class via JavaScript or overriding the inline style using !important in the stylesheet? While you can work to override this method, it will break eventually. Adding to the DOM via JS is probably the most stable way to go.

What I was trying to figure out the other day was pretty much that. I wanted to add a custom class to posts in all topics (but not the topic/original post). I would want to be able to turn that custom class on/off by user group and topic created_at, but I thought I might be able to figure that out once I’d figured out how to add the custom class to posts. I didn’t see quite the example I was looking for, but a couple of other things may have distracted me before I found the example.

I don’t think that will work because I need the colour to be fetched from the category settings - so the border colour will vary for each category. (I’ve updated my post above to reflect this.)

Unless that can be fetched via JS (/adding to the DOM)? (If so can you give me an example please?)

I’d have to dig into the specifics to see if what you’re doing is feasible with just JS selection (unfortunately don’t have the time right now).

But you can see adding of a class here: https://github.com/discourse/discourse-knowledge-base-theme/blob/4694d4a8ad788f8884e3341c852d3f77b2c1d103/javascripts/discourse/components/knowledge-base.js.es6#L24

You can also modify inline styles with JS: https://www.kirupa.com/html5/setting_css_styles_using_javascript.htm

4 Likes

Thanks Justin, I’ll take a look.

Or is there a way to overwrite helpers (https://github.com/discourse/discourse/blob/b25d9e96c1414bf78b44221da4d385675843991e/app/assets/javascripts/discourse/helpers/category-link.js.es6) like we can templates/components/widgets etc?

I don’t mind having to keep an eye on that file for future changes if need be :smiley:

You can try creating a file in your theme at javascripts/discourse/helpers/category-link.js.es6 and adding a modified version of that function there. It’s been a while since I’ve had to mess around that far so I can’t guarantee if that’ll work! :smiley:

5 Likes

Yup, that should work.

3 Likes

Thanks both!

Last question (hopefully!) how do I create a file there? :relaxed:

In a plugin that’s easy, create the folder and just copy and paste!

1 Like

Ah!!! Thanks Robert!

I have to pop out now but will try that as soon as I get back in :ok_hand:

1 Like

Demo:

Plugin folder structure:

image

Spot the sneaky edit :male_detective:

image

image

5 Likes

Thanks Robert!

It’s not working tho :sob:

Here’s what I did:

rails g plugin CategoryAlternative

Then put this:

In a helpers folder that I had to create:

Then:

rm -rf tmp
rails s

It’s showing in the AdminCP but no changes reflect on the actual page (changes show if I edit the non-plugin version of the file tho with the exact same edits)

Have I missed something? Do I need to configure anything else?

1 Like

Are you getting any javascript console errors?

Did you include the entire file?

NB I did not use the plugin generator, I just created all that manually.

1 Like

I didn’t at the time of writing (as I thought Justin meant to just include that function) however, although including the whole file got it to work - it wasn’t the same as when I directly edited the file.

For instance it worked on the latest page, but not in the all categories drop down… which did work when I edited the file directly!

So I’ve ended up making the changes I needed via some messy CSS :joy: and I’ve had to make some compromises too - it’s definitely better than not being able to have done it, but ideally I would have preferred something as simple as changing a category style to be as simple as editing a template :sweat:

2 Likes

I’d like to do that as well.
Here’s the path of the file I try to override in my theme component:
custom_loader\javascripts\discourse\helpers\loading-spinner.js
But the js file isn’t loaded. What am I missing?

1 Like