在哪里声明模块中不同包含文件通用的变量 - Drupal [英] Where to declare variables common to different include files in a module - Drupal

查看:14
本文介绍了在哪里声明模块中不同包含文件通用的变量 - Drupal的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Drupal 模块示例".我已将模块的管理和客户端功能拆分为两个包含文件 example.admin.inc 和 example.pages.inc.我在 example.module 文件中声明了常量 (define()),它们在 pages.inc 文件和 admin.inc 文件中都可以访问.

I have a Drupal Module 'example'. I have split the admin and client side functionality of the module into two include files example.admin.inc and example.pages.inc. I declared constants (define()) in the example.module file, which are accessible in both the pages.inc file and admin.inc file.

但是我怎样才能在一个集中的位置声明普通的 $variables 以便它们可以在 admin.inc 文件和 pages.inc 文件中访问.我试图在example.module 中声明$variable,但在example.admin.inc 和example.pages.inc 中无法访问它.

But how can I declare normal $variables in a centralized location so that they can be accessible in both the admin.inc file and pages.inc file. I tried to declare $variable in example.module, but couldn't access it in the example.admin.inc and example.pages.inc.

推荐答案

有多种可能性,取决于你想要的.

There are multiple possibilities, depending on want you want.

您可以使用 variable_get('your_module_your_key', $default_value);.然后,很容易在 settings.php 中使用 $conf['your_module_your_key'] = 'other value'; 覆盖它.参见 http://api.drupal.org/api/drupal/includes--bootstrap.inc/function/variable_get.

You can use variable_get('your_module_your_key', $default_value);. Then, it is easy to override that either in settings.php with $conf['your_module_your_key'] = 'other value';. See http://api.drupal.org/api/drupal/includes--bootstrap.inc/function/variable_get.

为此构建一个设置表单也很容易,例如参见 http://api.drupal.org/api/drupal/modules--menu--menu.admin.inc/function/menu_configure/7.

It is also easy to build a settings form for that, see for example http://api.drupal.org/api/drupal/modules--menu--menu.admin.inc/function/menu_configure/7.

请注意,您应该始终使用模块名称作为变量的前缀,并且不要将其用于经常更改的变量,因为每次更改都会导致缓存清除对于所有变量.另请注意,all 变量会在每个页面请求中加载,因此不要创建过多的变量或在其中存储大数据结构.它们主要用于配置.

Note that you should always prefix your variables with your module name and that you don't use this for variables that change frequently because every change results in a cache clear for all variables. Also note that all variables are loaded on every page request, so don't create too many of them or store big data structures in it. They are mainly meant for configuration.

您可以编写简单的 get/set 函数,这些函数在单个页面请求期间使用内部静态来交换变量.参见例如 http://api.drupal.org/api/drupal/includes--path.inc/function/drupal_set_title/6.

You can write simple get/set functions which use a static internally to exchange variables during a single page request. See for example http://api.drupal.org/api/drupal/includes--path.inc/function/drupal_set_title/6.

请记住,这些东西只为单个页面请求保留.这是一个示例实现,它允许保存由字符串标识的多个变量,因此其工作方式类似于 variable_get()/set().

Keep in mind that these things are only kept for a single page request. Here is an example implementation which allows to save multiple variables identified by a string and therefor works similar like variable_get()/set().

<?php
function yourmodule_static($key, $value = NULL) {
  static $storage;

  if (isset($value)) {
    $storage[$key] = $value;
  }
  return $storage[$key];
}

// Then, you can use it like this:
your_module_static('key', $value);

// And then in a different function/file:
$value = your_module_static('key');

您也可以将其扩展为按引用返回等等.

You could also extend it to return per reference and so on.

例如要存储来自缓慢的数据库查询或复杂计算的数据,您可以使用缓存系统.http://www.lullabot.com/articles/a-beginners-guide-to-caching-data 看起来像是对此的详细而精彩的解释.

To store for example data from slow database queries or complex calculations, you can use the cache system. http://www.lullabot.com/articles/a-beginners-guide-to-caching-data looks like a detailed and great explanation of that.

请注意,缓存默认存储在数据库中,但可以插入,例如也可以使用 APC 或 Memcached.

Note that the cache is stored in the database by default but is pluggable and can for example also use APC or Memcached.

这篇关于在哪里声明模块中不同包含文件通用的变量 - Drupal的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆