drupal7 常用API
来源:互联网 发布:lamp python 编辑:程序博客网 时间:2024/06/15 13:03
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);// nodefield_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
- drupal7 常用API
- drupal7 常用API
- drupal7 常用的数据读取API
- Drupal7 API Field API
- Drupal7 API Hooks
- Drupal7 API Menu system
- Drupal7 API Form generation
- Drupal7 API File interface
- Drupal7 API Search interface
- Drupal7 API 全局变量
- Drupal7 常用配置
- Drupal7 API Database abstraction layer
- Drupal7 API Node access rights
- Drupal7 API Default theme implementations
- 常用API
- 常用API
- 常用API
- 常用API
- 在AngularJs中使用监听(addEventListener)
- 简单模拟___A+B
- RTSP/RTP学习
- VMware vMotion简介
- ScrollView内嵌套Gallery和ListView时,Gallery发生偏移
- drupal7 常用API
- c#初学-多线程中lock Monitor锁
- MyEclipse Debug 快捷键
- Undefined function 'adb' for input arguments of type 'double'.
- Cocoapods使用指令及问题汇总
- 汽车美容的趋势
- JMeter 如何把上一个请求的结果作为下一个请求的参数 —— 使用正则提取器
- Servlet工作流程Demo
- DIV的概念和定位