转:drupal常用api
drupal常用api
最短的函数
// 语言字串,除了可以获取对应语言外,还可以设置字串变量。可以是!var, @var或 %var,%var就添加元素外层。@var会过滤HTML,!var会原样输出HTML,%var会添加span外层。
t('my name is @name', array('@name' => 'willam'));
// 一个链接
l('User Edit', 'user/1/edit');
判断首页
drupal_is_front_page();
GLOBALS
$GLOBALS['base_url'] // URL root
加载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(libraries_get_path('custom').'/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'));
得到URL alias
drupal_lookup_path('alias',"node/".$node->nid);
drupal_get_path_alias("node/".$node->nid);
路径匹配
drupal_match_path($_GET['q'], 'node/*');
图片
image_style_url('image_style_name', $node->field_image[LANGUAGE_NONE][0]['uri']); // 缩略图片URL
file_create_url($node->field_image[LANGUAGE_NONE][0]['uri']); // 原始图片URL
// 显示缩略图片
$variables = array(
'style_name' => 'image_style_name',
'path' => $node->field_image[LANGUAGE_NONE][0]['uri'],
'alt' => $node->title,
'title' => $node->title,
);
print theme('image_style', $variables);
// 显示原图
$variables = array(
'path' => 'path/to/img.jpg',
'alt' => 'Test alt',
'title' => 'Test title',
'width' => '50%',
'height' => '50%',
'attributes' => array('class' => 'some-img', 'id' => 'my-img'),
);
$img = theme('image', $variables);
// 带链接的图片
l(theme_image_style(array('path' => $variables['node']->field_logo['und']['0']['uri'], 'style_name' => '100x100')), 'node/' . $variables['node']->nid, array('html' => TRUE));
配置值存取
- variable_get
- variable_set
- variable_del
日期格式化
format_date($timestamp, $type = 'custom', $format = '');
date_default_timezone_set('PRC');
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());
自定义breadcrumb
$breadcrumb = array();
$breadcrumb[] = l('Home', 'node');
$breadcrumb[] = l('Our Team', 'team');
$breadcrumb[] = drupal_get_title();
drupal_set_breadcrumb($breadcrumb);
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);
file_scan_directory('public://','/.*\.(png|gif|jpg|jpeg)$/'); // 扫描文件夹,返回file对象数组
Form API File upload
http://drupal.org/node/1537950
entity 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。
node add form
$type = 'news';
module_load_include('inc', 'node', 'node.pages');
$node = (object) array(
'uid' => $GLOBALS['user']->uid,
'name' => $GLOBALS['user']->name ?: '',
'type' => $type,
'language' => LANGUAGE_NONE,
);
$form = drupal_get_form($type . '_node_form', $node);
还需要添加以下HOOK来处理AJAX时产生的错误
/**
* Implementation of hook_form_node_form_alter().
*/
function mymodule_form_node_form_alter(&$form, &$form_state, $form_id){
//ensuring the ajax upload still has all the includes
$files = (isset($form_state['build_info']['files'])) ? $form_state['build_info']['files'] : array();
$files[] = drupal_get_path('module', 'node') . '/node.pages.inc';
$form_state['build_info']['files'] = $files;
}
得到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)) {
$cache_data = $cached->data;
} else {
$cache_data = getData();
cache_set($cache_key, $cache_data);
}
session
drupal_session_start();
$_SESSION[$key] = $value;
修改用户名的HOOK
hook_username_alter();
format_username($account); // 显示用户名
301 redirects
function mytheme_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;
生成用户的识别码
user_pass_rehash($account->pass, $timestamp, $account->login);
增删用户角色
$role_name = 'admin';
$role = user_role_load_by_name($role_name);
user_multiple_role_edit($uids, 'add_role', $role->rid);
user_multiple_role_edit($uids, 'remove_role', $role->rid);
输出一个MENU(1 level)
theme('links', array(
'links' => menu_navigation_links('menu_name'),
'attributes' => array(
'id' => 'footer-menu',
'class' => array('links', 'clearfix'),
),
)
);
常用配置
当前主题:$conf['theme_default']
网站名:$conf['site_name']
调试
dpm($vars);
dpq($query);
javascript格式
(function ($, Drupal, window, document) {
Drupal.behaviors.myModule = {
attach: function (context) {
// ....
}
};
})(jQuery, Drupal, window, document);
注释:原文链接:http://segmentfault.com/a/1190000000359186;
转:drupal常用api的更多相关文章
- drupal常用api
最短的函数 // 语言字串,除了可以获取对应语言外,还可以设置字串变量.可以是!var, @var或 %var,%var就添加元素外层.@var会过滤HTML,!var会原样输出HTML,%var会添 ...
- html5 canvas常用api总结(一)
1.监听浏览器加载事件. window.addEventListener("load",eventWindowLoaded,false); load事件在html页面加载结束时发生 ...
- compass General 常用api学习[Sass和compass学习笔记]
compass 中一些常用api 包括一些浏览器hack @import "compass/utilities/general" Clearfix Clearfix 是用来清除浮动 ...
- java基础3.0:Java常用API
本篇介绍Java基础中常用API使用,当然只是简单介绍,围绕重要知识点引入,巩固开发知识,深入了解每个API的使用,查看JavaAPI文档是必不可少的. 一.java.lang包下的API Java常 ...
- C++ 中超类化和子类化常用API
在windows平台上,使用C++实现子类化和超类化常用的API并不多,由于这些API函数的详解和使用方法,网上一大把.本文仅作为笔记,简单的记录一下. 子类化:SetWindowLong,GetWi ...
- node.js整理 02文件操作-常用API
NodeJS不仅能做网络编程,而且能够操作文件. 拷贝 小文件拷贝 var fs = require('fs'); function copy(src, dst) { fs.writeFileSync ...
- js的常用api
JavaScript常用API总结 原创 2016-10-02 story JavaScript 下面是我整理的一些JavaScript常用的API清单. 目录 元素查找 class操作 节点操作 属 ...
- JS操作DOM常用API总结
<JS高程>中的DOM部分写的有些繁琐,还没勇气整理,直到看到了这篇博文 Javascript操作DOM常用API总结,顿时有了一种居高临下,一览全局的感觉.不过有时间还是得自己把书里面的 ...
- Drupal常用开发工具(一)——Devel模块
进行 Drupal 开发时有许多模块和工具可供使用,其中最常用的两项便是 Devel 及 Drupal for Firebug.本文和<Drupal常用开发工具(二)——Drupal for F ...
随机推荐
- Android热修复——Tinker的集成
前言 做前端开发的都知道,当我们项目做完了以后,都会把应用上传到应用市场上供用户下载使用,比如上传到应用宝啊,应用汇啊,360啊,小米,华为,魅族啊,等等但是,有时候我们会经常遇到一些很扯淡的事情,刚 ...
- 内存管理cpuset,mempolicy[原理]
介绍cpuset,mbind,set_mempolicy在内存管理上的应用 change log :确定先从mempolicy的man 手册翻译开始研究,计划如下 .先从man手册入手,通过实现mem ...
- 【LeetCode】1. 两数之和
题目 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样 ...
- 【sping揭秘】14、@before @AfterThrowing
@before 基础模式,我们先直接看一下就可以了,比较简单,网上一堆... 不是我装逼哈,我学了那么久spring,aop的皮毛也就是网上的那些blog内容,稍微高级点的我也不会,这里跳过基础部分 ...
- 如何在Mac下配置Github和Bitbucket的SSH
--- title: 如何在Mac下配置Github和Bitbucket的SSH date: 2017-12-23 21:10:30 tags: - Mac - Git - Github catego ...
- 如何将页面上的数据导入excel中
网上关于页面数据导入excel的文章很多,但是大部分都是关于 ActiveXObject 对象,可是ActiveXObject 对象是只支持IE的,可我连IE11也测试了,还是无法识别,又查到消息,好 ...
- Tsql2008查询性能优化第一章---APPLY
APPLY运算符涉及以下两个步骤中的一步或两步(取决于APPLY的类型): 1.A1把右表表达式应用于左表的行. 2.A2:添加外部行. Ap ...
- 关于dao层的封装和前端分页的结合(文章有点长,耐心点哦)
任何一个封装讲究的是,实用,多状态.Action: 任何一个Action继承分页有关参数类PageManage,自然考虑的到分页效果,我们必须定义下几个分页的参数.并根据这个参数进行查值. 然 ...
- ACM学习大纲
1 推荐题库 •http://ace.delos.com/usaco/ 美国的OI 题库,如果是刚入门的新手,可以尝试先把它刷通,能够学到几乎全部的基础算法极其优化,全部的题解及标程还有题目翻译可以b ...
- JavaWeb学习 (十五)————JSP指令
一.JSP指令简介 JSP指令(directive)是为JSP引擎而设计的,它们并不直接产生任何可见输出,而只是告诉引擎如何处理JSP页面中的其余部分. 在JSP 2.0规范中共定义了三个指令: pa ...