drupal常用api
生活随笔
收集整理的這篇文章主要介紹了
drupal常用api
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
最短的函數(shù)
// 語言字串,除了可以獲取對應(yīng)語言外,還可以設(shè)置字串變量。可以是!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 rootGLOBAL 文檔
加載inc文件
module_load_include('inc', 'mymodule', 'mymodule.field');得到ROOT目錄
getcwd() DRUPAL_ROOT 把URI(public://)地址轉(zhuǎn)換為絕對地址 drupal_realpath('public://xxx.csv'); // 得到系統(tǒng)路徑 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');關(guān)于javascript細(xì)節(jié)可以訪問以下鏈接:
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'查實現(xiàn)某個HOOK的所有函數(shù)
- dpm(module_implements('menu'));
- 使用drush: drush hook menu
跳轉(zhuǎn)
drupal_goto('node/1'); drupal_goto(current_path()); drupal_not_found(); // 跳轉(zhuǎn)到404頁面 drupal_goto('<front>', array(), 301); // 301跳轉(zhuǎn)URI
- URI to URL: file_create_url('public://js/gmap_markers.js');
- 臨時目錄URI: temporary://
URL
arg(1); // 提取URL第2個參數(shù),例如node/1,會提取1 $_GET['q']; // 得到當(dāng)前URL路徑 url('node/1'); // 網(wǎng)站相對地址 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數(shù)據(jù)
echo drupal_json(array('xxx')); drupal_json_output(array('xxx')); drupal_exit();得到請求 arg(1); // 得到URI的第二個值跳轉(zhuǎn) destination
任何表單,只要在URL上加?destination=xxx,提交后都會跳轉(zhuǎn)到相應(yīng)地址
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對象數(shù)組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參數(shù)$form_state的原變量,clone的會報錯。執(zhí)行后會填充$form變量,可以附加到當(dāng)前的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時產(chǎn)生的錯誤
/*** 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轉(zhuǎn)換為HTML,而render之前,element只是一個大型數(shù)組,一般的數(shù)組操作很難區(qū)分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); } } }獲取當(dāng)前語言標(biāo)識 (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'), ), ) );常用配置
當(dāng)前主題:$conf['theme_default'] 網(wǎng)站名:$conf['site_name']調(diào)試
dpm($vars); dpq($query);javascript格式
(function ($, Drupal, window, document) {Drupal.behaviors.myModule = {attach: function (context) { // .... } }; })(jQuery, Drupal, window, document);注釋:原文鏈接:http://segmentfault.com/a/1190000000359186;
轉(zhuǎn)載于:https://www.cnblogs.com/amw863/p/4551369.html
總結(jié)
以上是生活随笔為你收集整理的drupal常用api的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 兔罝
- 下一篇: Java层与Jni层的数组传递(转)