Я думаю, что предпочтительнее отделить ваши настройки от темы. Пользователь темы (т.е. код, который вызывает вашу тему) должен загрузить конфиги и внедрить в шаблон через переменные.
/**
* Implements hook_theme().
*/
function mymodule_theme() {
return [
'mymodule_wrapper' => [
'template' => 'theme/mymodule-wrapper',
'variables' => [
'foo' => NULL,
],
];
}
/**
* Some other module implemented this hook_menu callback for a random path.
*/
function othermodule_random_endpoint() {
$config = variable_get('mymodule_settings', []);
return [
'#theme' => 'mymodule_wrapper',
'#foo' => $config['foo'] ?? NULL,
]
}
Но если вам действительно нужно загрузить переменную непосредственно в файл шаблонаЕсть 2 подхода:
Загрузка настроек в файле тем Direct *
Все файлы тем в Drupal 7 - это файлы php (в соответствии с суффиксом .tpl.php
). Я не рекомендую это, но вы можете сделать это полностью. В вашем файле темы:
<?php
// load the configs here
$config = variable_get('mymodule_settings', []);
?>
<div>
<p>Hello, this is a config value: <?php echo $config['foo']; ?></p>
</div>
Это уродливо, но эффективно.
Использование hook_preprocess_HOOK
Второй подход заключается в реализации hook_preprocess_HOOK .
/**
* Implements hook_theme().
*/
function mymodule_theme() {
return [
'mymodule_wrapper' => [
'template' => 'theme/mymodule-wrapper',
'variables' => [
'foo' => NULL,
],
];
}
/**
* Implements hook_preprocess_HOOK
*/
function mymodule_preprocess_mymodule_wrapper(&$variables) {
if (!isset($variables['config'])) {
$config = variable_get('mymodule_settings', []);
$variables['foo'] = $config['foo'] ?? NULL;
}
}