// 語言字串,除了能夠獲取對應語言外,還能夠設置字串變量。能夠是!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['base_url'] // URL root
GLOBAL 文檔javascript
module_load_include('inc', 'mymodule', 'mymodule.field');
getcwd() DRUPAL_ROOT 把URI(public://)地址轉換爲絕對地址 drupal_realpath('public://xxx.csv'); // 獲得系統路徑 file_create_url('public://xxx.csv'); // 獲得URL
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');
function hook_page_build(&$page) { if(drupal_is_front_page()) { drupal_add_js(libraries_get_path('knockout').'/knockout.js'); } }
https://drupal.org/node/756722php
Drupal.attachBehaviors(document);
drupal_add_library('system', 'ui.tabs');
SELECT * FROM dp_menu_router where path='admin/config/search/path/patterns'
dpm(module_implements('menu'));css
使用drush: drush hook menuhtml
drupal_goto('node/1'); drupal_goto(current_path()); drupal_not_found(); // 跳轉到404頁面 drupal_goto('<front>', array(), 301); // 301跳轉
URI to URL: file_create_url('public://js/gmap_markers.js');java
臨時目錄URI: temporary://node
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'));
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_getjquery
variable_setajax
variable_delsql
format_date($timestamp, $type = 'custom', $format = ''); date_default_timezone_set('PRC'); strtotime('2013-6-5 20:11');
echo drupal_json(array('xxx')); drupal_json_output(array('xxx')); drupal_exit();獲得請求 arg(1); // 獲得URI的第二個值
任何表單,只要在URL上加?destination=xxx,提交後都會跳轉到相應地址json
url('xxx', array('query' => array('destination' => 'yyyy')) ); drupal_goto('user', array('query' => array('destination'=>'user/myorder'))); drupal_goto(drupal_get_destination());
$breadcrumb = array(); $breadcrumb[] = l('Home', 'node'); $breadcrumb[] = l('Our Team', 'team'); $breadcrumb[] = drupal_get_title(); drupal_set_breadcrumb($breadcrumb);
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對象數組
http://drupal.org/node/1537950
$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。
$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能夠互相嵌套,經過render能夠把element轉換爲HTML,而render以前,element只是一個大型數組,通常的數組操做很難區分element部分,因此能夠用element_children:
foreach (element_children($element) as $key) { $sub_element[]= $element[$key]; }
$static = &drupal_static(__FUNCTION__, array());
$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); }
drupal_session_start(); $_SESSION[$key] = $value;
hook_username_alter(); format_username($account); // 顯示用戶名
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); } } }
$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);
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);
(function ($, Drupal, window, document) { Drupal.behaviors.myModule = { attach: function (context) { // .... } }; })(jQuery, Drupal, window, document);
$queue = nodequeue_load($qid); $subqueue = nodequeue_load_subqueue($sqid); $node = node_load($nid); $id = nodequeue_get_content_id($queue, $node); nodequeue_subqueue_add($queue, $subqueue, $id);