Specifying custom SiteTree icons

Silverstripe Version: 4.1.0

Question:

I’m in the process of upgrading a website from SilverStripe 3 to version 4.1.0. Currently I specify SiteTree icons for custom page types as follows:

class ProductPage extends Page 
{	
	private static $icon = "mysite/icons/product.png";

Now, the icon seems to only correctly appear in the SiteTree if I keep a copy in two locations! These are:

  1. /mysite/icons
  2. /public/resources/mysite/icons

Missing one of them will cause either the generic Page icon to be displayed, or it is left blank.

Have I got something completely wrong here? I’d assume I’d only need to keep a copy of the icon in one place? Any help you can give would be much appreciated.

Thanks!

If you’re making use of the public folder in SS4.1+ (which is a good idea) then static resources like images need to be served up from that directory. If you want to store them in your mysite/icons directory that’s fine, but you need to expose that directory so it gets symlinked to the resources directory.

Try deleting the /public/resources/mysite/icons directory, then add the following config to your composer.json file, then run the terminal command composer vendor-expose in your project root, which will take care of the symlinking.

    "extra": {
        "expose": [
            "mysite/icons"
        ]
    }

Alternatively (and more simply), you should be able to store your icons at public/icons instead of mysite/icons and reference them relative to the public folder e.g. $icon = "icons/product.png".

More info:

2 Likes

Perfect! Thanks @JonoM. The last, very simple, option worked for me - and now I understand why!

Thanks for your help.