Answers.Body: <p>The error message you're getting indicates that the <a href="https://craftcms.com/docs/4.x/config/general.html#allowadminchanges" rel="nofollow noreferrer"><code>allowAdminChanges</code> configuration option</a> is set to <code>false</code>, 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.</p>
  <p>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.</p>
  <p>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:</p>
  <pre class="lang-php prettyprint-override"><code>if (\Craft::$app-&gt;config-&gt;general-&gt;allowAdminChanges) { }
  </code></pre>
  <p>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 <a href="https://github.com/craftcms/craft/blob/main/.env.example.dev" rel="nofollow noreferrer">environment variables</a> 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.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-01-24T13:54:43.053Z
Answers.LastActivityDate: 2023-01-24T14:03:56.550Z
Answers.LastEditDate: 2023-01-24T14:03:56.550Z
Answers.UserDisplayName: MoritzLost
Body: <p>I have written a plugin and it listens to different events to run some tasks. Now if the app is in &quot;read-only mode&quot;, I do not want some of these events to run:</p>
  <pre class="lang-php prettyprint-override"><code>class MyPlugin extends Plugin
  {
      public function init()
      {
          if (&lt;NOT in read-only mode&gt;) {
              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 () =&gt; ...);
      }
  }
  </code></pre>
  <p>What's the value/setting to check for &quot;&lt;NOT in read-only mode&gt;&quot;?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-01-24T09:33:05.787Z
Comments.Text: What do you mean by read-only mode? There's [ephemeral mode](https://craftcms.com/docs/4.x/config/#craft-ephemeral) for when Craft is running on non-persistent file storage, in this case it doesn't write any files, do you mean that?
Comments.UserDisplayName: MoritzLost
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-01-24T13:37:02.597Z
Comments.Text: 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.
Comments.UserDisplayName: 7ochem
Tags: <plugin-development><events><craft4>
Title: How to check read-only mode in plugin?
ThreadText: # How to check read-only mode in plugin?
  
  (Asked by: username_0 on 2023-01-24)
  
  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:
  
  ```php
  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>"?
  
  **Tags:** plugin-development, events, craft4
  
  > **Comments:**
  > **username_1**: What do you mean by read-only mode? There's [ephemeral mode](https://craftcms.com/docs/4.x/config/#craft-ephemeral) for when Craft is running on non-persistent file storage, in this case it doesn't write any files, do you mean that?  
  > **username_0**: 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.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2023-01-24. Score: 3 | Accepted answer
  
  The error message you're getting indicates that the [`allowAdminChanges` configuration option](https://craftcms.com/docs/4.x/config/general.html#allowadminchanges) 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:
  
  ```php
  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](https://github.com/craftcms/craft/blob/main/.env.example.dev) 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.
  
  
  ---
  
  