1

This is a followup question to this question which asked about the possibility of per-site plugin config for Craft 3.

I'm a plugin author and have had a feature request for allowing my plugin to have different settings based on the current site. Looking at the above question, it sounds like this used to be possible in Craft 2:

<?php

return [
    'flagName' => [
        'de' => 'Bundesflagge',
        'fr' => 'Tricolore',
    ],
];

This doesn't work in Craft 3, alas (throws an error). I could write something into my plugin's admin area to loop through known sites and output config per-site but this seems like something Craft should be able to do out-of-the-box (especially given the above method used to be valid). Does anyone know if there's a good way to do this in Craft 3?

  • I'm pretty sure that the above only ever applied to the siteUrl and wasn't generalized to work for any setting for Craft 2.x – andrew.welch Jul 12 '18 at 4:45
3

There is no native support for this, you can follow my approach and create a custom table for it to store the settings for each site

Another way is to return a different array depending on the current site in your config file

if(Craft::$app->getSites()->getCurrentSite()->handle === X){
    return ['flagName' => 'Tricolore'];
} else {
    return ['flagName' => 'Bundesflagge'];
}

or

$array = [
    'de' => [
        'flagName' => 'Bundesflagge'
    ],
    'fr' => [
        'flagName' => 'Tricolore'
    ]
];

return $array[Craft::$app->getSites()->getCurrentSite()->handle];
1

if you need site-specific config for the plugin in the control-panel, you have to check for the site-param

<?php
$handle = Craft::$app->request->isCpRequest ?  Craft::$app->request->getParam('site') : Craft::$app->getSites()->getCurrentSite()->handle;
$array = [
  'de' => [
    ...
   ],
  'en' => [
    ...
  ]
];
return $array[$handle];
?>

getCurrentSite() always returns the primary site for cp.

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.