diff --git a/CHANGELOG-v3.md b/CHANGELOG-v3.md index 417bef4ee35..4e09594e0e0 100644 --- a/CHANGELOG-v3.md +++ b/CHANGELOG-v3.md @@ -1,5 +1,11 @@ # Release Notes for Craft CMS 3.x +## Unreleased + +### Added +- The System Name setting can now be set to an environment variable. ([#3529](https://github.com/craftcms/cms/issues/3529)) +- Added `craft\base\ApplicationTrait::getSystemName()`. + ## 3.1.3 - 2019-01-21 ### Added diff --git a/docs/config/environments.md b/docs/config/environments.md index 916838711ae..ea3b09fb31e 100644 --- a/docs/config/environments.md +++ b/docs/config/environments.md @@ -6,6 +6,8 @@ Some settings should be defined on a per-environment basis. For example, when de Some settings in the Control Panel can be set to environment variables (like the ones defined in your `.env` file): +- General Settings + - **System Name** - Sites - **Base URL** - Asset Volumes diff --git a/src/base/ApplicationTrait.php b/src/base/ApplicationTrait.php index 63225a52f3e..399cce21bb1 100644 --- a/src/base/ApplicationTrait.php +++ b/src/base/ApplicationTrait.php @@ -14,6 +14,7 @@ use craft\db\Query; use craft\db\Table; use craft\errors\DbConnectException; +use craft\errors\SiteNotFoundException; use craft\errors\WrongEditionException; use craft\events\EditionChangeEvent; use craft\helpers\App; @@ -613,6 +614,26 @@ public function saveInfo(Info $info): bool return false; } + /** + * Returns the system name. + * + * @return string + */ + public function getSystemName(): string + { + if (($name = Craft::$app->getProjectConfig()->get('system.name')) !== null) { + return Craft::parseEnv($name); + } + + try { + $name = $this->getSites()->getPrimarySite()->name; + } catch (SiteNotFoundException $e) { + $name = null; + } + + return $name ?: 'Craft'; + } + /** * Returns the Yii framework version. * diff --git a/src/db/Connection.php b/src/db/Connection.php index 9defd919c09..45cd0dc2f50 100644 --- a/src/db/Connection.php +++ b/src/db/Connection.php @@ -555,7 +555,7 @@ private function _getFixedSystemName(): string ->from([Table::INFO]) ->column()[0]; } catch (\Throwable $e) { - return Craft::$app->getProjectConfig()->get('system.name') ?? Craft::$app->getSites()->getPrimarySite()->name; + return Craft::$app->getSystemName(); } } } diff --git a/src/models/Info.php b/src/models/Info.php index 2d3063bb859..ae7a569a95a 100644 --- a/src/models/Info.php +++ b/src/models/Info.php @@ -103,11 +103,11 @@ public function getEdition(): int * Returns the system name. * * @return string - * @deprecated in 3.1. Use `Craft::$app->projectConfig->get('system.name')` instead. + * @deprecated in 3.1. Use `Craft::$app->getSystemName()` instead. */ public function getName(): string { - return Craft::$app->getProjectConfig()->get('system.name'); + return Craft::$app->getSystemName(); } /** diff --git a/src/templates/_layouts/base.html b/src/templates/_layouts/base.html index 754e86150a1..bb60f3d0bd7 100644 --- a/src/templates/_layouts/base.html +++ b/src/templates/_layouts/base.html @@ -1,4 +1,4 @@ -{% set systemName = craft.app.projectConfig.get('system.name')|t('site') -%} +{% set systemName = craft.app.getSystemName()|t('site') -%} {% set docTitle = docTitle is defined ? docTitle : title|striptags -%} {% set bodyClass = (bodyClass is defined ? bodyClass~' ' : '') ~ craft.app.locale.getOrientation() -%} diff --git a/src/templates/settings/general/_index.html b/src/templates/settings/general/_index.html index 14380a362f5..b85500ece30 100644 --- a/src/templates/settings/general/_index.html +++ b/src/templates/settings/general/_index.html @@ -21,10 +21,11 @@ {{ redirectInput('settings') }} - {{ forms.textField({ + {{ forms.autosuggestField({ first: true, label: "System Name"|t('app'), id: 'name', + suggestions: craft.cp.getEnvSuggestions(), name: 'name', value: system.name }) }} diff --git a/src/web/twig/Extension.php b/src/web/twig/Extension.php index ba5a134b8d5..ccac7c40754 100644 --- a/src/web/twig/Extension.php +++ b/src/web/twig/Extension.php @@ -1022,7 +1022,7 @@ public function getGlobals(): array // Only set these things when Craft is installed and not being updated if ($isInstalled && !Craft::$app->getUpdates()->getIsCraftDbMigrationNeeded()) { - $globals['systemName'] = Craft::$app->getProjectConfig()->get('system.name'); + $globals['systemName'] = Craft::$app->getSystemName(); /** @noinspection PhpUnhandledExceptionInspection */ $site = Craft::$app->getSites()->getCurrentSite(); $globals['currentSite'] = $site;