5

Using the Pixel & Tonic Contact Form plugin, I added a config file (config/contactform.php) to override plugin settings from the CP.

I would like to change some settings if devMode is enabled : recipients from the contact form plugin, to separate my tests addresses from my client addresses.

<?php

$recipients = array(
    'devMode' => '[email protected],[email protected]',
    'prodMode' => '[email protected],[email protected]'
);

if (devMode == true) {
    $recipients = $recipients['devMode'];
} else {
    $recipients = $recipients['prodMode'];
}

return array(
    'toEmail' => ($recipients ?: null),

How can I check if devMode == true? Can I access to this value using craft()->config or something else that?

4 Answers 4

8

It's as simple as

if ( craft()->config->get( 'devMode' ) )
{
 //do stuff
}
9
  • Using this, I get this error : The property « Craft\ConfigService.devMode » is undefined.. Yes, I'm using environment variables, but following the other answer there is no devMode data that I can get from the craft()->config->get('environmentVariables'), while devMode is enabled in my current dev environment.
    – Romain P.
    Commented Jun 29, 2016 at 8:34
  • This is because you're not in the Craft namespace. Included in most files is a namespace Craft; declaration at the top of the file, but in config files this is not usually there. You could either add this namespace declaration to your config file, or use \Craft\craft()->config->devMode instead. Commented Jun 29, 2016 at 8:37
  • namespace Craft; was already specified at the first line of my config/contactform.php file.
    – Romain P.
    Commented Jun 29, 2016 at 8:39
  • Ok, I found the solution : craft()->config->get('devMode') does the job. Thank you !
    – Romain P.
    Commented Jun 29, 2016 at 8:41
  • 2
    Oh I see, I misread the original error. I'm pretty sure you need to use craft()->config->get('devMode') instead Commented Jun 29, 2016 at 8:41
14

For those coming here using Craft 3, it's now Craft::$app->getConfig()->general->devMode.

2

Since Craft 4.0, the recommended method to test if Craft is running in dev mode from the PHP layer is App::devMode():

use craft\helpers\App;

$devMode = App::devMode();

Apart from App::devMode() being faster (and less verbose to type), the big difference between it and Craft::$app->getConfig()->getGeneral()->devMode is that the latter will simply return whatever value the devMode config setting is currently set to, whereas App::devMode() will tell you if Craft is actually running in dev mode or not, by returning the value for the YII_DEBUG constant.

In practice, the values for the devMode config setting and the YII_DEBUG constant will almost always be the same - but not necessarily, and there are edge cases where the nuance between the two can make a difference.

Internally, Craft uses App::devMode() in most cases.

If working in Twig, App::devMode() is not available, but the global devMode variable can be used instead, which will return the same thing (i.e. the YII_DEBUG constant's value):

{% if devMode %}
   We are debuggin'
{% endif %}
1

For those coming here >= Craft 3.5, it's now

Craft::$app->getConfig()->getGeneral()->devMode.

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.