如何在drupal 7中定义模块级变量

问题描述 投票:3回答:2

如何在drupal模块中定义变量,以便我可以从任何函数访问该模块中的那些变量

php drupal variables drupal-7
2个回答
5
投票

尝试variable_set()和variable_get()

像这样设置变量:

// I have assumed the variable name to be "the_name_of_the_variable"
variable_set("the_name_of_the_variable", "the value of the variable");

然后检索类似的值:

$my_variable = variable_get("the_name_of_the_variable", "a default value in case the variable has never been set before");

0
投票

对于Drupal中的每个页面加载,所有.module文件都被加载。但是inc文件不是这种情况。

仅当菜单钩中提及Inc文件或使用module_load_include在模块文件中明确提及它们时才加载它们。>

// Load node.admin.inc from the node module.
  module_load_include('inc', 'node', 'node.admin');

我认为这几乎可以回答您的问题。因此,您应该在这些文件中将变量定义为全局变量。

[您的问题,如果您是指模块中定义的功能范围也应仅限于模块,那么我认为目前没有任何解决方案。但是,至少在模块未执行任何操作时,上述方法将确保不加载变量。

© www.soinside.com 2019 - 2024. All rights reserved.