使用hooku update在Drupal中更新和设置变量的例程

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用hooku update在Drupal中更新和设置变量的例程相关的知识,希望对你有一定的参考价值。

Drupal allows hook_update_N to run any sort of code. Most of the time, however, people only use this to alter the database scheme.
But you can use it for much more. Like setting variables, creating nodes, importing views and so on.
This code shows how to set a series of variables in Drupal.

When you deploy this code, running update.php will set all the variables as in the listing.

I typically catch this in a module called 'projectname_meta'. So this function would go in projectname_meta.install, and will be called from projectname_meta_install and projectname_meta_update_5002, for example.
  1. /**
  2.  * Sets a variable range of variables to LUX-values.
  3.  *
  4.  * @return
  5.  * An array of name=>value pairs containing the var and its value.
  6.  */
  7. function _projectname_meta_set_variables() {
  8. $variables = array(
  9. 'site_name' => 'My New Site!',
  10. 'site_offline' => FALSE,
  11. );
  12.  
  13. foreach($variables as $key => $value) {
  14. variable_set($key, $value);
  15. $statii[] = "$key => $value";
  16. }
  17. return array('success' => TRUE, 'query' => implode('<br /> ', $statii));
  18. }

以上是关于使用hooku update在Drupal中更新和设置变量的例程的主要内容,如果未能解决你的问题,请参考以下文章

Drupal 6.x中的hooku viewsu postu render

PHP 使用hook_update在Drupal中更新和设置变量的例程

Drupal 7 |使用db_update更新多行

在Drupal7表单中使用#ajax更新多个字段

Drupal如何更新模块数据库

如何在Drupal 7中使用entity_save函数插入数据?