最短的函數
// 語言字串,除了能夠獲取對應語言外,還能夠設置字串變量。能夠是!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 文檔javascript
加載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/756722php
激活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,提交後都會跳轉到相應地址html
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/1537950java
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中。node
若是想只提取部分的field,能夠使用multiple_entity_form module。jquery
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時產生的錯誤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:sql
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;