drupal7 常用API

drupal7 常用API

最短的函数

// 语言字串,除了可以获取对应语言外,还可以设置字串变量。可以是@var或 %var,%var就添加元素外层
t('my name is @name', array('@name' => 'willam'));

// 一个链接
l('User Edit', 'user/1/edit');

判断首页

drupal_is_front_page();

GLOBALS

$GLOBALS['base_url'] // URL root

GLOBAL 文档

加载inc文件

module_load_include('inc', 'mymodule', 'mymodule.field');

得到ROOT目录

getcwd()
DRUPAL_ROOT
把URI(public://)地址转换为绝对地址
drupal_realpath('public://xxx.csv'); // 得到系统路径
file_create_url('public://xxx.csv'); // 得到URL

加载脚本&CSS

drupal_add_js('misc/collapse.js');
drupal_add_js('misc/collapse.js', 'file');
drupal_add_js(drupal_get_path('module', 'content_glider'). '/srcipt.js');
drupal_add_js('jQuery(document).ready(function () { alert("Hello!"); });', 'inline');
drupal_add_js('jQuery(document).ready(function () { alert("Hello!"); });',
    array('type' => 'inline', 'scope' => 'footer', 'weight' => 5)
);
drupal_add_js('http://example.com/example.js', 'external');
drupal_add_js(array('myModule' => array('key' => 'value')), 'setting');
关于javascript细节可以访问以下链接:

https://drupal.org/node/756722

激活behaviors

Drupal.attachBehaviors(document);

加载jquery ui

drupal_add_library('system', 'ui.tabs');

查某个URL得到程序所在

SELECT * FROM dp_menu_router where path='admin/config/search/path/patterns'

查实现某个HOOK的所有函数

  • dpm(module_implements('menu'));
  • 使用drush: drush hook menu

跳转

drupal_goto('node/1');
drupal_goto(current_path());
drupal_not_found(); // 跳转到404页面
drupal_goto('<front>', array(), 301); // 301跳转

URI

  • URI to URL: file_create_url('public://js/gmap_markers.js');
  • 临时目录URI: temporary://

URL

arg(1); // 提取URL第2个参数,例如node/1,会提取1
$_GET['q']; // 得到当前URL路径
url('node/1'); // 网站相对地址
url('node/1', array('absolute' => true); // 绝对地址
url('<front>', array('query' => 'action=do', 'fragment' => 'top'));

图片缩略地址

image_style_url('image_style_name', $node->field_image[LANGUAGE_NONE][0]['uri']); // 图片URL

配置值存取

  • variable_get
  • variable_set
  • variable_del

日期格式化

format_date($timestamp, $type = 'custom', $format = '');
strtotime('2013-6-5 20:11');

返回JSON数据

echo drupal_json(array('xxx'));
drupal_json_output(array('xxx'));
drupal_exit();得到请求
arg(1); // 得到URI的第二个值

跳转 destination

任何表单,只要在URL上加?destination=xxx,提交后都会跳转到相应地址

url('xxx',
  array('query' => array('destination' => 'yyyy'))
);
drupal_goto('user', array('query' => array('destination'=>'user/myorder')));
drupal_goto(drupal_get_destination());

Log

watchdog('event_type', 'name is :name', array(':name' => $name), WATCHDOG_WARNING);

文件操作

file_load($fid)->uri;
file_move($file, 'public://xxx/');
file_copy($file, 'public://xxx/');
file_delete($file);

Form API File upload

http://drupal.org/node/1537950

提取edit form的form field element,以profile2为例

$form = array();
field_attach_form('profile2', profile2_load_by_user($user, 'general'),$form, $form_state);

// node
field_attach_form('node', $node, $form, $form_state);

注意$form_state必须是form参数$form_state的原变量,clone的会报错。执行后会填充$form变量,可以附加到当前的form中。

如果想只提取部分的field,可以使用multiple_entity_form module。

得到element children

element可以互相嵌套,通过render可以把element转换为HTML,而render之前,element只是一个大型数组,一般的数组操作很难区分element部分,所以可以用element_children:

foreach (element_children($element) as $key) {
    $sub_element[]= $element[$key];
}

单实例

$static = &drupal_static(__FUNCTION__, array());

cache

$cache_key = md5(serialize($values));
if($cached = cache_get($cache_key)) {
  $data = $cached->data;
} else {
  $data = getData();
  cache_set($cache_key, $data);
}

session

drupal_session_start();
$_SESSION[$key] = $value;

修改用户名的HOOK

hook_username_alter();
format_username($account); // 显示用户名

301 redirects

function topaz_financial_preprocess_html(&$variables, $hook){
  if(!drupal_match_path(current_path(), '<front>') && !(user_access("administer users") || drupal_match_path(current_path(), "user\nuser/*"))) {
    if(module_exists('search404')) {
      search404_goto("<front>");
    } else {
      drupal_goto('<front>', array(), 301);
    }
  }
}

获取当前语言标识 (i18n)

$language = i18n_language_interface();
$lang = $language->language;

得到URL alias

drupal_lookup_path('alias',"node/".$node->nid);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值