Discourse.SiteSettings not working after upgrade

Following the advice here Moving away from Discourse.SiteSettings: An upgrading guide:

When I do that I get: TypeError: undefined is not an object (evaluating 'settings.tag_style')

This is in a component I created with a script in the head (none of the other options mentioned by Robin work either).

Is there anything else I can try? Should I just continue using Discourse.SiteSettings?

3 Likes

Can you share your code, include the HTML script tag?

2 Likes

Here ya go Robin:

<script type="text/discourse-plugin" version="0.1">
  const getURL = require('discourse-common/lib/get-url').default;

  function customLinkTagRenderer(tag, params) {
    params = params || {};
    const visibleName = Handlebars.Utils.escapeExpression(tag);
    tag = visibleName.toLowerCase();
    const classes = ["discourse-tag"];
    const tagName = params.tagName || "a";
    let path;
    if (tagName === "a" && !params.noHref) {
      if (params.isPrivateMessage && Discourse.User.current()) {
        const username = params.tagsForUser
          ? params.tagsForUser
          : Discourse.User.current().username;
        path = `/u/${username}/messages/tags/${tag}`;
      } else {
        path = `https://site.com/${tag}`;
      }
    }
    const href = path ? ` href='${getURL(path)}' ` : "";

    if (Discourse.SiteSettings.tag_style || params.style) {
      classes.push(params.style || Discourse.SiteSettings.tag_style);
    }

	  let val =
	    "<" +
	    tagName +
	    href +
	    " data-tag-name=" +
	    tag +
	    " class='" +
	    classes.join(" ") +
	    "'>" +
	    visibleName +
	    "</" +
	    tagName +
	    ">";

    if (params.count) {
      val += " <span class='discourse-tag-count'>x" + params.count + "</span>";
    }

    return val;
  }

  api.replaceTagRenderer(customLinkTagRenderer);
</script>

All it does is change this line:

path = `https://site.com/${tag}`;

(Handlebars.Utils.escapeExpression(tag); is not working either)

1 Like

Aha, I see. settings in that case points at theme settings, which are much more common for themes/plugins.

I’ve updated the documentation with how to access site settings. You can do it like this:

<script type="text/discourse-plugin" version="0.1">
  let siteSettings = api.container.lookup('site-settings:main');
</script>
3 Likes

Thanks Robin - it works :smiley:

Any ideas how to fix Handlebars.Utils.escapeExpression?

You should be able to import our helper:

const { escapeExpression } = require('discourse/lib/utilities');
3 Likes