日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

drupal常用api

發布時間:2025/7/14 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

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;

轉載于:https://www.cnblogs.com/amw863/p/4551369.html

總結

以上是生活随笔為你收集整理的drupal常用api的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。