最短的函数

// 语言字串,除了可以获取对应语言外,还可以设置字串变量。可以是!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

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(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的更多相关文章

  1. 转:drupal常用api

    drupal常用api   最短的函数 // 语言字串,除了可以获取对应语言外,还可以设置字串变量.可以是!var, @var或 %var,%var就添加元素外层.@var会过滤HTML,!var会原 ...

  2. html5 canvas常用api总结(一)

    1.监听浏览器加载事件. window.addEventListener("load",eventWindowLoaded,false); load事件在html页面加载结束时发生 ...

  3. compass General 常用api学习[Sass和compass学习笔记]

    compass 中一些常用api 包括一些浏览器hack @import "compass/utilities/general" Clearfix Clearfix 是用来清除浮动 ...

  4. java基础3.0:Java常用API

    本篇介绍Java基础中常用API使用,当然只是简单介绍,围绕重要知识点引入,巩固开发知识,深入了解每个API的使用,查看JavaAPI文档是必不可少的. 一.java.lang包下的API Java常 ...

  5. C++ 中超类化和子类化常用API

    在windows平台上,使用C++实现子类化和超类化常用的API并不多,由于这些API函数的详解和使用方法,网上一大把.本文仅作为笔记,简单的记录一下. 子类化:SetWindowLong,GetWi ...

  6. node.js整理 02文件操作-常用API

    NodeJS不仅能做网络编程,而且能够操作文件. 拷贝 小文件拷贝 var fs = require('fs'); function copy(src, dst) { fs.writeFileSync ...

  7. js的常用api

    JavaScript常用API总结 原创 2016-10-02 story JavaScript 下面是我整理的一些JavaScript常用的API清单. 目录 元素查找 class操作 节点操作 属 ...

  8. JS操作DOM常用API总结

    <JS高程>中的DOM部分写的有些繁琐,还没勇气整理,直到看到了这篇博文 Javascript操作DOM常用API总结,顿时有了一种居高临下,一览全局的感觉.不过有时间还是得自己把书里面的 ...

  9. Drupal常用开发工具(一)——Devel模块

    进行 Drupal 开发时有许多模块和工具可供使用,其中最常用的两项便是 Devel 及 Drupal for Firebug.本文和<Drupal常用开发工具(二)——Drupal for F ...

随机推荐

  1. D3.js (v3)+react框架 基础部分之认识选择集和如何绘制一个矢量图

    首先需要下载安装d3.js  :  yarn add d3 然后在组建中引入 :  import * as d3 from 'd3' 然后定义一个方法,在componentDidMount()这个钩子 ...

  2. 7. Bagging & Random Forest

    通过前面集成学习的介绍我们知道,欲得到泛化性能强的集成学习器,集成中个体学习器应尽量相互独立:虽然“独立”在现实任务中无法做到,但可以设法使基学习器尽可能具有较大差异. 1. Bagging 自助采样 ...

  3. 03-创建高可用 etcd 集群

    本文档记录自己的学习历程! 创建高可用 etcd 集群 kuberntes 系统使用 etcd 存储所有数据,本文档介绍部署一个三节点高可用 etcd 集群的步骤,这三个节点使用以下机器: 192.1 ...

  4. tkinter的grid布局中合并单元格

    rowspan  合并多行,比如:Label(root,text="table",width=10,height=2,),grid(row=0,column=0,rowspan=2 ...

  5. SQLServer——SQLServer链接外部数据源

    学习链接:https://www.cnblogs.com/licin/p/6244169.html 一.新建ODBC数据源 1.打开控制面板→管理工具→ODBC数据源→系统DSN 2.添加新系统数据源 ...

  6. JAVA 利用Dom4j实现英语六级词汇查询 含演示地址

    要求 必备知识 基本了解JAVA编程知识,DOM基础. 开发环境 MyEclipse10 演示地址 演示地址     通过前面几天的学习,现在基本掌握了JAVA操作DOM方面的知识,现在来一个小DEM ...

  7. Hadoop2源码分析-序列化篇

    1.概述 上一篇我们了解了MapReduce的相关流程,包含MapReduce V2的重构思路,新的设计架构,与MapReduce V1的区别等内容,今天我们在来学习下在Hadoop V2中的序列化的 ...

  8. Tensorflow应用之LSTM

    学习RNN时原理理解起来不难,但是用TensorFlow去实现时被它各种数据的shape弄得晕头转向.现在就结合一个情感分析的案例来了解一下LSTM的操作流程. 一.深度学习在自然语言处理中的应用 自 ...

  9. PTA (Advanced Level) 1009 Product of Polynomials

    1009 Product of Polynomials This time, you are supposed to find A×B where A and B are two polynomial ...

  10. java8 Stream使用案例

    1. 原理 Stream 不是集合元素,它不是数据结构并不保存数据,它是有关算法和计算的,它更像一个高级版本的 Iterator. 原始版本的 Iterator,用户只能显式地一个一个遍历元素并对其执 ...