Theme modifiers: A brief introduction

As themes become more ambitious, we’ve been looking for ways to allow them to manipulate core server-side behaviour. While they will never be given the same level as control as plugins, we can provide some predefined hooks for themes to manipulate.

Introducing: theme modifiers :partying_face:

They are specified using the modifiers key in your theme’s about.json file.

For a 100% up-to-date list of modifiers, check the database schema at the bottom of theme_modifier_set.rb, but here’s a quick summary of what we have so far:

  • serialize_topic_excerpts boolean (default false) - always include excerpts when serializing topic lists

  • csp_extensions string array - add directives to the CSP. Works the same as the old “extend_content_security_policy” theme-setting method. But remember, simple <script> src=""> tags are allowed automatically.

  • svg_icons string array - a list of icons which should be included in the icon subset

  • topic thumbnails array of dimensions - request additional resolutions in the topic thumbnail set. Note that they are generated asynchronously, so you must fall-back to the original image if your requested size is not provided. More information available in the commit message

One theme making heavy use of these new hooks is Topic List Thumbnails - check out the code to see how it works.


This document is version controlled - suggest changes on github.

Last edited by @JammyDodger 2024-05-26T12:27:54Z

Check documentPerform check on document:
34 Likes

David, probably a bit lazy of me to ask but is there any way to access this in a plugin:

Themes can request additional thumbnail sizes by using a modifier in their about.json file:

I will be attempting to migrate the TLP plugin to this new schema and it would be good to have the same access to features from a plugin, at least in the meantime.

2 Likes

There isn’t at the moment, but I’ll look into it :eyes:

6 Likes

David, what’s the right approach for BULK recreation of thumbnails?

I’ve just tried utilising on one of my sites and it seems to have processed about 10% of the Topics … then given up (or turned its nose up at the rest). Why I think it’s the former is that the Topics for whom Thumbnails were produced were the latest 10%.

Rebaking posts doesn’t seem to cut it. In fact, I did run a bulk rebake and wonder if that upset it …

I notice that image_url can be populated, but there are no thumbnails.

Any advice, appreciated!

1 Like

That column doesn’t do anything, and will be dropped very soon. image_upload_id is the one you want.

There should be no need for this. I deliberately designed it so that people can install new themes without having to mess around on the console. Thumbnails are generated asynchronously when needed. For example:

  • you add a new theme, which requests new resolutions
  • a user requests a topic, we serve the thumbnails that exist. If any sizes don’t exist, we schedule a sidekiq job.
  • next time someone requests the topic, the correct thumbnails will exist

If the requested thumbnail size is larger than the original, we won’t bother generate the thumbnail.

So the critical thing to bear in mind for this to work is:

There’s an example of this fallback logic in the thumbnail theme component I made - feel free to steal logic from there.

5 Likes

The fallback is serialised as thumbnailsl[0]? Yes, I’m already handling that. (nice implementation btw, very easy to handle)

Is it possible some images are ‘not making the grade’ or fitting the criteria?

The behaviour we have in the TLP plugin will pick up one-box thumbnails. That’s not happening in every case here I think.

For example, if you get time, take a look at these examples:

I don’t think these thumbnails make the cut. The thumbnails are serialized as null

Yes that’s deliberate - we had a number of requests to remove small onebox thumbnails. For example, people were ending up with their github avatar as a topic thumbnail - which is rarely intended

Note that for oneboxes where the image is the actual content (such as instagram/twitter/etc photos), they will be selected.

As for the youtube video, I fixed that yesterday.

5 Likes

Ah great, thanks for confirmation.

That’s odd, build was more recent, but some still seem to be being overlooked.

Yeah, that’s partly why I ended up implementing a thumbnail picker for the situations where the automated choice wasn’t optimal. I may still want to modify that behaviour, but I will try to do so in the plugin.

Thanks for your time David!

2 Likes

OK, I’ve worked it out. After a somewhat fruitless byebug session I could not work out why older YT posts were not getting thumbnails.

Then is dawned on me. It’s because of this:

So I suggest that actually it might be necessary to rebake after setting this to a rather larger number (365?).

I think I’m right in saying that if something is not uploaded locally, it won’t have a thumbnail created? …

4 Likes

:+1: correct, this only works for local uploads… we may need to rethink that “max days old” setting :thinking:

8 Likes

I have a plan, will try and get it implemented this week. One question - do you need the values to be dynamic?

i.e. Will the resolutions be defined at boot? Or at runtime (e.g. via site settings)?

The former is easier… but the latter might be possible as well :thinking:

2 Likes

Thanks for taking a look.

I just need a fixed way, exactly like the theme component.

A site setting would be nice though.

I will add for full disclosure: id like to migrate away from the plugin so parity with theme component solution would be more than enough.

2 Likes

@merefield here you go:

https://github.com/discourse/discourse/commit/725e38f9d7c42975c9d75afe0a1f79499693667b

Hopefully the commit message explains how it works, but let me know if you have any questions

7 Likes

Excellent. Just added it to TLP and looks like it’s working! Thanks for your help!

4 Likes

4 posts were split to a new topic: Getting thumbnails from json endpoints

Can we make it work for images from remote server as well? For example, images from Blogger, Picasa, or Amazon S3?

Because Discourse supports hosting on Amazon S3 for big and large image site, now if everything needs to be hosted on the local server directly then this design methodology seems to be a drawback.

With this update, it’s not an easy fix for my site since we’re using other server to host the images. Now it’s too difficult to move to an affiliated server with many posts, while it’s too big for hosting on local sever.

This is only designed to work on Discourse ‘uploads’. Those can be on S3, or some other service, if you use

We recommend using the download_remote_images site setting to automatically download images which are hotlinked from other sites.

4 Likes

Hi David, anything special one needs to do to ensure Pro icons can be used in a TC?

1 Like

Nothing special, no. It should work just the same as using pro icons elsewhere in Discourse. I guess you are using this plugin to enable pro icons?

If it doesn’t work let me know and I’ll take a look :eyes:

2 Likes

Yep we are. We’ll have another dig. Thanks for response late your eve!

2 Likes