1

I have written a plugin and it listens to different events to run some tasks. Now if the app is in "read-only mode", I do not want some of these events to run:

class MyPlugin extends Plugin
{
    public function init()
    {
        if (<NOT in read-only mode>) {
            Event::on(Plugins::class, Plugins::EVENT_AFTER_INSTALL_PLUGIN, function (PluginEvent $event) {
                // Do stuff when NOT in read-only mode
            });
        }

        // Always do this stuff
        Event::on(...., ..., fn () => ...);
    }
}

What's the value/setting to check for "<NOT in read-only mode>"?

2
  • What do you mean by read-only mode? There's ephemeral mode for when Craft is running on non-persistent file storage, in this case it doesn't write any files, do you mean that?
    – MoritzLost
    Commented Jan 24, 2023 at 9:33
  • I'm getting an error from the environment it is ran on: error: Changes to the project config are not possible while in read-only mode.. I think it has to do with allowing changes by the admin? I guess ephemeral mode is also one to check in the same if-statement.
    – 7ochem
    Commented Jan 24, 2023 at 13:37

1 Answer 1

3

The error message you're getting indicates that the allowAdminChanges configuration option is set to false, which means no configuration changes are allowed. This is the default in a production environment. It's meant to ensure that no unsupervised changes happen in the production environment, so you can always cleanly deploy changes from your development environment.

You usually don't need to do anything to handle this in your plugin. Plugin installations are only allowed in environments where admin changes are allowed. If someone tries to install a plugin in an environment where admin changes are not allowed, getting this error message is the correct behaviour.

However, the installation hook also runs in the production environment, since some operations, like creating database tables, need to happen in all environments where the plugin is installed. But all configuration changes should have already been done in the development environment once the installation hook runs in the production environment. If your installation hook performs any configuration changes, those should indeed only happen when the plugin is installed in a development environment. You can check if admin changes are allowed using the general config service:

if (\Craft::$app->config->general->allowAdminChanges) { }

If you're getting the error message in your development environment, change the configuration for this environment. You want to allow admin changes in development. The starter project uses environment variables for changes that should differ between environments, so you can keep the configuration under version control and still have different settings depending on the environment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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