Skip to content

Commit 6eafeec

Browse files
committed
UHF-12723: Add DrupalSettings class
This was previously implemented in hdbt.theme
1 parent 022104a commit 6eafeec

File tree

3 files changed

+77
-9
lines changed

3 files changed

+77
-9
lines changed

helfi_hakuvahti.services.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ services:
99

1010
Drupal\helfi_hakuvahti\HakuvahtiInterface:
1111
class: Drupal\helfi_hakuvahti\Hakuvahti
12+
13+
Drupal\helfi_hakuvahti\DrupalSettings: ~

src/DrupalSettings.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace Drupal\helfi_hakuvahti;
4+
5+
use Drupal\Core\Cache\CacheableMetadata;
6+
use Drupal\Core\Config\ConfigFactoryInterface;
7+
use Drupal\Core\Language\LanguageInterface;
8+
use Drupal\Core\Language\LanguageManagerInterface;
9+
use Drupal\Core\Url;
10+
11+
/**
12+
* Get Hakuvahti settings for frontend.
13+
*/
14+
readonly class DrupalSettings {
15+
16+
/**
17+
* Settings that are exposed to JavaScript.
18+
*/
19+
public const array EXPOSED_SETTINGS = [
20+
'hakuvahti_tos_checkbox_label',
21+
'hakuvahti_tos_link_text',
22+
'hakuvahti_tos_link_url',
23+
'hakuvahti_instructions_link_url',
24+
];
25+
26+
public function __construct(
27+
private ConfigFactoryInterface $configFactory,
28+
private LanguageManagerInterface $languageManager,
29+
) {
30+
}
31+
32+
/**
33+
* Get settings as they should be exposed to JavaScript.
34+
*/
35+
public function applyTo(array &$build): void {
36+
$langcode = $this->languageManager
37+
->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
38+
->getId();
39+
40+
// Attempt to get the translated configuration.
41+
// @todo Why is this needed?
42+
$language = $this->languageManager->getLanguage($langcode);
43+
$originalLanguage = $this->languageManager->getConfigOverrideLanguage();
44+
$this->languageManager->setConfigOverrideLanguage($language);
45+
46+
try {
47+
$cache = new CacheableMetadata();
48+
49+
$settings = $this->configFactory->get('helfi_hakuvahti.settings');
50+
51+
// Do not expose settings if base_url is not configured.
52+
if (empty($settings->get('base_url'))) {
53+
return;
54+
}
55+
56+
$drupalSettings = [];
57+
foreach (self::EXPOSED_SETTINGS as $exposed_setting) {
58+
$drupalSettings[$exposed_setting] = $settings->get($exposed_setting) ?: NULL;
59+
}
60+
61+
$drupalSettings['subscribe_url'] = Url::fromRoute('helfi_hakuvahti.subscribe')->toString();
62+
63+
$build['#attached']['drupalSettings']['hakuvahti'] = $drupalSettings;
64+
65+
$cache->addCacheableDependency($settings);
66+
$cache->addCacheContexts(['languages:language_content']);
67+
$cache->applyTo($build);
68+
}
69+
finally {
70+
// Set the config back to the original language.
71+
$this->languageManager->setConfigOverrideLanguage($originalLanguage);
72+
}
73+
}
74+
75+
}

src/Form/SettingsForm.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,6 @@
1212
*/
1313
class SettingsForm extends ConfigFormBase {
1414

15-
use AutowireTrait;
16-
17-
public function __construct(
18-
ConfigFactoryInterface $configFactory,
19-
TypedConfigManagerInterface $typedConfigManager,
20-
) {
21-
parent::__construct($configFactory, $typedConfigManager);
22-
}
23-
2415
/**
2516
* {@inheritdoc}
2617
*/

0 commit comments

Comments
 (0)