Merge pull request #17641 from MegaphoneJon/core-1590
[civicrm-core.git] / setup / plugins / init / Drupal8.civi-setup.php
1 <?php
2 /**
3 * @file
4 *
5 * Determine default settings for Drupal 8.
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 !== 'Drupal8' || !is_callable(['Drupal', 'currentUser'])) {
16 return;
17 }
18
19 \Civi\Setup::log()->info(sprintf('[%s] Handle %s', basename(__FILE__), 'checkAuthorized'));
20 $e->setAuthorized(\Civi\Setup\DrupalUtil::isDrush() || \Drupal::currentUser()->hasPermission('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 !== 'Drupal8' || !is_callable(['Drupal', 'currentUser'])) {
27 return;
28 }
29 \Civi\Setup::log()->info(sprintf('[%s] Handle %s', basename(__FILE__), 'init'));
30
31 $cmsPath = \Drupal::root();
32
33 // Compute settingsPath.
34 $siteDir = \Civi\Setup\DrupalUtil::getDrupalSiteDir($cmsPath);
35 $model->settingsPath = implode(DIRECTORY_SEPARATOR, [$cmsPath, 'sites', $siteDir, 'civicrm.settings.php']);
36
37 if (($loadGenerated = \Drupal\Core\Site\Settings::get('civicrm_load_generated', NULL)) !== NULL) {
38 $model->loadGenerated = $loadGenerated;
39 }
40
41 // Compute DSN.
42 $connectionOptions = \Drupal::database()->getConnectionOptions();
43 $ssl_params = \Civi\Setup\DrupalUtil::guessSslParams($connectionOptions);
44 $model->db = $model->cmsDb = array(
45 'server' => \Civi\Setup\DbUtil::encodeHostPort($connectionOptions['host'], $connectionOptions['port'] ?: NULL),
46 'username' => $connectionOptions['username'],
47 'password' => $connectionOptions['password'],
48 'database' => $connectionOptions['database'],
49 'ssl_params' => empty($ssl_params) ? NULL : $ssl_params,
50 );
51
52 // Compute cmsBaseUrl.
53 if (empty($model->cmsBaseUrl)) {
54 global $base_url, $base_path;
55 $model->cmsBaseUrl = $base_url . $base_path;
56 }
57
58 // Compute general paths
59 $model->paths['civicrm.files']['url'] = implode('/', [$model->cmsBaseUrl, \Drupal\Core\StreamWrapper\PublicStream::basePath(), 'civicrm']);
60 $model->paths['civicrm.files']['path'] = implode(DIRECTORY_SEPARATOR, [_drupal8_civisetup_getPublicFiles(), 'civicrm']);
61
62 // Compute templateCompileDir.
63 $model->templateCompilePath = implode(DIRECTORY_SEPARATOR, [_drupal8_civisetup_getPrivateFiles(), 'civicrm', 'templates_c']);
64
65 // Compute default locale.
66 $langcode = \Drupal::languageManager()->getCurrentLanguage()->getId();
67 $model->lang = \Civi\Setup\LocaleUtil::pickClosest($langcode, $model->getField('lang', 'options'));
68 });
69
70 function _drupal8_civisetup_getPublicFiles() {
71 $filePublicPath = \Drupal\Core\StreamWrapper\PublicStream::basePath();
72
73 if (!$filePublicPath) {
74 throw new \Civi\Setup\Exception\InitException("Failed to identify public files path");
75 }
76 elseif (!CRM_Utils_File::isAbsolute($filePublicPath)) {
77 $filePublicPath = \Drupal::root() . DIRECTORY_SEPARATOR . $filePublicPath;
78 }
79
80 return $filePublicPath;
81 }
82
83 function _drupal8_civisetup_getPrivateFiles() {
84 $filePrivatePath = \Drupal\Core\StreamWrapper\PrivateStream::basePath();
85
86 if (!$filePrivatePath) {
87 $filePrivatePath = _drupal8_civisetup_getPublicFiles();
88 }
89 elseif ($filePrivatePath && !CRM_Utils_File::isAbsolute($filePrivatePath)) {
90 $filePrivatePath = \Drupal::root() . DIRECTORY_SEPARATOR . $filePrivatePath;
91 }
92
93 return $filePrivatePath;
94 }