# How to access theme component setting from another theme component

**URL:** https://meta.discourse.org/t/how-to-access-theme-component-setting-from-another-theme-component/290303
**Category:** Development
**Created:** [January 2, 2024, 6:58pm UTC](https://meta.discourse.org/t/how-to-access-theme-component-setting-from-another-theme-component/290303 "2024-01-02T18:58:23Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [January 2, 2024, 6:58pm UTC](https://meta.discourse.org/t/how-to-access-theme-component-setting-from-another-theme-component/290303/1 "2024-01-02T18:58:23Z")

</div>

Is there a way to access one theme component’s settings from another theme component? It seems like `settings` contains settings only for the theme component that it’s in.

If it’s not possible to access one theme component’s settings from another theme copmonent what is the best way to do that?

- have a single theme component?
- move settings into the primary theme (but are those available? I’ve not tested that yet).

---

<div class="post-metadata">

### Author: ![Arkshine](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/arkshine/32/298682_2.png) [@Arkshine](https://meta.discourse.org/u/Arkshine)
#### Post date: [January 3, 2024, 4:47pm UTC](https://meta.discourse.org/t/how-to-access-theme-component-setting-from-another-theme-component/290303/2 "2024-01-03T16:47:49Z")

</div>

> **This is technically feasible, but as David's stated below, please don't use it**
>
> I took a look because it’s quite an interesting question! 👍
> 
> There is a place in [`discourse/lib/theme-settings-store`](https://github.com/discourse/discourse/blob/main/app/assets/javascripts/discourse/app/lib/theme-settings-store.js) where the theme settings are stored and exported functions such as `getObjectForTheme` or `getSetting`. can be used.
> 
> You can retrieve the value of a setting for any theme component as long as you have its ID.  
> However, I couldn’t find an easy way to retrieve the translation ID ↔ name mapping.
> 
> That said, you could do the following trick:
> 
> ```js
> // Retrieves from <link> tags the theme name <-> id mapping for the active theme components
> const themesComponents = Array.from(
> document.querySelectorAll("link[data-theme-name]")
> ).reduce((acc, link) => {
> acc[link.dataset.themeName] = parseInt(link.dataset.themeId, 10);
> return acc;
> }, {});
> 
> // Gets the theme id based on its component name
> const themeId = themesComponents["discotoc"];
> 
> // Gets all the settings
> const themeSettings = getObjectForTheme(themeId);
> 
> // Gets a specific setting
> const themeSetting = getSetting(themeId, "anchor_icon");
> 
> ```
> 
> ![image](https://global.discourse-cdn.com/meta/original/4X/1/0/5/10571cc71dadb5bbcd18a1b919d0faeab8612d86.png)
> 
> ![image](https://global.discourse-cdn.com/meta/original/4X/2/8/9/28982b72887d791f54017b374fa19f50619ca287.png)
> 
> ![image](https://global.discourse-cdn.com/meta/original/4X/4/3/0/430079c5de97d84a2d103a248d9fb911cca984d1.png)
> 
> I’m unsure that’s the best reliable way; some helpers would be nice if it does not exist.

---

<div class="post-metadata">

### Author: ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)
#### Post date: [January 3, 2024, 4:56pm UTC](https://meta.discourse.org/t/how-to-access-theme-component-setting-from-another-theme-component/290303/3 "2024-01-03T16:56:22Z")

</div>

Settings from different themes/theme-components are deliberately isolated. Having interdependencies between themes/theme-components makes them very difficult to develop and support.

As @Arkshine demonstrated, it is technically possible to hack your way to find the data, but I would **STRONGLY** recommend against it. The strategy will break if an admin changes the name of a theme in the admin panel, and is also quite likely to randomly break with future core changes.

---

<div class="post-metadata">

### Author: ![Arkshine](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/arkshine/32/298682_2.png) [@Arkshine](https://meta.discourse.org/u/Arkshine)
#### Post date: [January 3, 2024, 5:09pm UTC](https://meta.discourse.org/t/how-to-access-theme-component-setting-from-another-theme-component/290303/4 "2024-01-03T17:09:51Z")

</div>

My bad, I realized that the code I provided may not be reliable. I should really be careful about what I display here. Hacking is fun, but it lasts till it leads to unwanted usage and issues in the future. 😟

---

<div class="post-metadata">

### Author: ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)
#### Post date: [January 3, 2024, 5:15pm UTC](https://meta.discourse.org/t/how-to-access-theme-component-setting-from-another-theme-component/290303/5 "2024-01-03T17:15:08Z")

</div>

Oh no worries at all @Arkshine - it’s always good to experiment. Having the `[details]` note in your post is good to make sure people know the risks they’re taking :chefs_kiss:

If sharing settings like this between plugins and/or theme components becomes a common requirement, then we can certainly work out some more robust options. Perhaps we can use this topic to lay out example use-cases.

My gut feel is that, if a theme is trying to use settings from another theme then… perhaps they should be merged into a single repository?

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [February 2, 2024, 5:16pm UTC](https://meta.discourse.org/t/how-to-access-theme-component-setting-from-another-theme-component/290303/6 "2024-02-02T17:16:04Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
