8

When building a plugin, how does one define user editable config files? Would it be better to create a settings UI in the plugin and have the user edit those variables in the CP? Or is there a way to do it properly with a config file?

The reason I want to opt for a config option is so the value can be localized for the specific environment to set different options. I need to add the ability to define a public directory that will be used to store generated images and obviously that path could be different on local and production environments.

11

You can add a config.php file in the root of your plugin's folder (craft/plugins/pluginhandle/config.php) that specifies the default values of your config items by returning an array of key value pairs for config settings and values.

If a user wants to change any of those settings, they can add a pluginhandle.php config file to craft/config/pluginhandle.php that returns an array with the overridden key value pairs.

From your plugin, if you call:

craft()->config->get('key', 'pluginhandle')

then Craft will return the user supplied value, if it exists, and fallback on the plugin supplied default value, if it does not.

Also worth nothing that all of Craft's multi-environment config stuff will work in this context for the plugin, too.

  • That's really helpful to know! – Alec Ritson Aug 18 '14 at 19:29
  • 2
    Is this in the docs? – Luke Holder Feb 7 '15 at 4:26
  • Does multi-enviroment config really work in this context? Does not seem to be working for me. This answer also seems to imply it does not work: craftcms.stackexchange.com/questions/12778/… – Kristian J. Jun 27 '16 at 9:02
  • @KristianJ. It works in the sense you can override your default plugin values (that live in plugins/pluginHandle/config.php) in the multi-environment config part of craft/config/pluginHandle.php. – Brad Bell Jun 27 '16 at 17:44
2

I would use plugin settings to store these kinds of values. Then in your plugin files (controller, services etc.) you could call up the settings and adjust your output as needed.

craft()->plugins->getPlugin('myPlugin')->getSettings()

As for asset paths and your site path, you can keep all of those relative by using the URL helper functions like {{ url('assets/build/css/main.css') }} and UrlHelper::getUrl('/') depending on where you are working.

Here's a related post - How do I reference the settings of my plugin?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.