From ffb2ea5d5280d4300795494885415a9a6679dcab Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Fri, 6 Aug 2021 19:51:05 +1200 Subject: [PATCH] drupal 9 views fix (alternative) I took a look at https://github.com/civicrm/civicrm-core/pull/20682 and it seemed clear that in it's current state it would fix one CMS (d9) & break another (d7). This is an alternate that will hopefully be non-breaky --- CRM/Admin/Form/Setting/UF.php | 12 +++--------- CRM/Utils/System/Backdrop.php | 12 ++++++++++++ CRM/Utils/System/Base.php | 17 +++++++++++++++++ CRM/Utils/System/Drupal9.php | 21 +++++++++++++++++++++ 4 files changed, 53 insertions(+), 9 deletions(-) create mode 100644 CRM/Utils/System/Drupal9.php diff --git a/CRM/Admin/Form/Setting/UF.php b/CRM/Admin/Form/Setting/UF.php index c59013a07639..b4c83c8413f9 100644 --- a/CRM/Admin/Form/Setting/UF.php +++ b/CRM/Admin/Form/Setting/UF.php @@ -67,8 +67,8 @@ public function buildQuickForm() { $config->dsn != $config->userFrameworkDSN || !empty($drupal_prefix) ) ) { - $dsn = CRM_Utils_SQL::autoSwitchDSN($config->dsn); - $dsnArray = DB::parseDSN($dsn); + + $dsnArray = DB::parseDSN(CRM_Utils_SQL::autoSwitchDSN($config->dsn)); $tableNames = CRM_Core_DAO::getTableNames(); asort($tableNames); $tablePrefixes = '$databases[\'default\'][\'default\'][\'prefix\']= ['; @@ -77,13 +77,7 @@ public function buildQuickForm() { } // add default prefix: the drupal database prefix $tablePrefixes .= "\n 'default' => '$drupal_prefix',"; - $prefix = ""; - if ($config->dsn != $config->userFrameworkDSN) { - $prefix = "`{$dsnArray['database']}`."; - if ($config->userFramework === 'Backdrop') { - $prefix = "{$dsnArray['database']}."; - } - } + $prefix = $config->userSystem->getCMSDatabasePrefix(); foreach ($tableNames as $tableName) { $tablePrefixes .= "\n '" . str_pad($tableName . "'", 41) . " => '{$prefix}',"; } diff --git a/CRM/Utils/System/Backdrop.php b/CRM/Utils/System/Backdrop.php index 3d72779f1569..5739976b9d00 100644 --- a/CRM/Utils/System/Backdrop.php +++ b/CRM/Utils/System/Backdrop.php @@ -1056,4 +1056,16 @@ public function getCMSPermissionsUrlParams() { return ['ufAccessURL' => url('admin/config/people/permissions')]; } + /** + * Get the CMS database as a 'prefix'. + * + * This returns a string that can be prepended to a query to include a CMS table. + * + * However, this string should contain backticks, or not, in accordance with the + * CMS's drupal views expectations, if any. + */ + public function getCMSDatabasePrefix(): string { + return str_replace(parent::getCMSDatabasePrefix(), '`', ''); + } + } diff --git a/CRM/Utils/System/Base.php b/CRM/Utils/System/Base.php index e08bad2c069a..acd0287129eb 100644 --- a/CRM/Utils/System/Base.php +++ b/CRM/Utils/System/Base.php @@ -1051,6 +1051,23 @@ public function getCMSPermissionsUrlParams() { return []; } + /** + * Get the CMS database as a 'prefix'. + * + * This returns a string that can be prepended to a query to include a CMS table. + * + * However, this string should contain backticks, or not, in accordance with the + * CMS's drupal views expectations, if any. + */ + public function getCMSDatabasePrefix(): string { + $crmDatabase = DB::parseDSN(CRM_Core_Config::singleton()->dsn)['database']; + $cmsDatabase = DB::parseDSN(CRM_Core_Config::singleton()->dsn)['database']; + if ($crmDatabase === $cmsDatabase) { + return ''; + } + return "`$cmsDatabase`."; + } + /** * Invalidates the cache of dynamic routes and forces a rebuild. */ diff --git a/CRM/Utils/System/Drupal9.php b/CRM/Utils/System/Drupal9.php new file mode 100644 index 000000000000..0ef86331d3a0 --- /dev/null +++ b/CRM/Utils/System/Drupal9.php @@ -0,0 +1,21 @@ +