Drupal.civi-setup.php - Don't assume that `$language` is available
[civicrm-core.git] / setup / plugins / init / Drupal.civi-setup.php
1 <?php
2 /**
3 * @file
4 *
5 * Determine default settings for Drupal 7.
6 */
7
8 if (!defined('CIVI_SETUP')) {
9 exit("Installation plugins must only be loaded by the installer.\n");
10 }
11
12 \Civi\Setup::dispatcher()
13 ->addListener('civi.setup.checkAuthorized', function (\Civi\Setup\Event\CheckAuthorizedEvent $e) {
14 $model = $e->getModel();
15 if ($model->cms !== 'Drupal' || !function_exists('user_access')) {
16 return;
17 }
18
19 \Civi\Setup::log()->info(sprintf('[%s] Handle %s', basename(__FILE__), 'checkAuthorized'));
20 $e->setAuthorized(user_access('administer modules'));
21 });
22
23 \Civi\Setup::dispatcher()
24 ->addListener('civi.setup.init', function (\Civi\Setup\Event\InitEvent $e) {
25 $model = $e->getModel();
26 if ($model->cms !== 'Drupal' || !function_exists('user_access')) {
27 return;
28 }
29 \Civi\Setup::log()->info(sprintf('[%s] Handle %s', basename(__FILE__), 'init'));
30
31 // Compute settingsPath.
32 $drupalSystem = new CRM_Utils_System_Drupal();
33 $cmsPath = $drupalSystem->cmsRootPath();
34 $siteDir = \Civi\Setup\DrupalUtil::getDrupalSiteDir($cmsPath);
35 $model->settingsPath = implode(DIRECTORY_SEPARATOR,
36 [$cmsPath, 'sites', $siteDir, 'civicrm.settings.php']);
37
38 // Compute DSN.
39 global $databases;
40 $model->db = $model->cmsDb = array(
41 'server' => \Civi\Setup\DbUtil::encodeHostPort($databases['default']['default']['host'], $databases['default']['default']['port'] ?: NULL),
42 'username' => $databases['default']['default']['username'],
43 'password' => $databases['default']['default']['password'],
44 'database' => $databases['default']['default']['database'],
45 );
46
47 // Compute cmsBaseUrl.
48 global $base_url, $base_path;
49 $model->cmsBaseUrl = $base_url . $base_path;
50
51 // Compute general paths
52 // $model->paths['civicrm.files']['url'] = $filePublicPath;
53 $model->paths['civicrm.files']['path'] = implode(DIRECTORY_SEPARATOR,
54 [_drupal_civisetup_getPublicFiles(), 'civicrm']);
55
56 // Compute templateCompileDir.
57 $model->templateCompilePath = implode(DIRECTORY_SEPARATOR,
58 [_drupal_civisetup_getPrivateFiles(), 'civicrm', 'templates_c']);
59
60 // Compute default locale.
61 global $language;
62 $model->lang = \Civi\Setup\LocaleUtil::pickClosest($language->langcode ?? NULL, $model->getField('lang', 'options'));
63 });
64
65 function _drupal_civisetup_getPublicFiles() {
66 $filePublicPath = variable_get('file_public_path', conf_path() . '/files');
67
68 if (!CRM_Utils_File::isAbsolute($filePublicPath)) {
69 $drupalSystem = new CRM_Utils_System_Drupal();
70 $cmsPath = $drupalSystem->cmsRootPath();
71 $filePublicPath = $cmsPath . DIRECTORY_SEPARATOR . $filePublicPath;
72 }
73
74 return $filePublicPath;
75 }
76
77 function _drupal_civisetup_getPrivateFiles() {
78 $filePrivatePath = variable_get('file_private_path', '');
79
80 if (!$filePrivatePath) {
81 $filePrivatePath = _drupal_civisetup_getPublicFiles();
82 }
83 elseif ($filePrivatePath && !CRM_Utils_File::isAbsolute($filePrivatePath)) {
84 $drupalSystem = new CRM_Utils_System_Drupal();
85 $cmsPath = $drupalSystem->cmsRootPath();
86
87 $filePrivatePath = $cmsPath . DIRECTORY_SEPARATOR . $filePrivatePath;
88 }
89
90 return $filePrivatePath;
91 }