欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

Drupal模块中的"hook_theme"函数和拖拽排序效果

shiping1 的头像

Drupal模块中的"hook_theme"函数和拖拽排序效果

项目中要用到能通过拖拽来排序的效果。在drupal的后台中,有很多地方都是用这种方法排序的,比如block,taxonomy等,这种排序方法直观,方便,容易操作,用户体验度非常好。

刚开始无从下手,于是就看例子,找到taxonomy,慢慢把它的几个函数提取出来,然后写到自己的模块里。在提取了taxonomy_overview_vocabularies(),taxonomy_overview_vocabularies_submit(),theme_taxonomy_overview_vocabularies(),后,想想应该可以了,可重命名后,发顼输出一堆很乱的东西,theme函数根本没有运行。记得以前在那里看过theme_模块名函数是自动执行的。到官方网站上查,也没有得到什么结果,试了半天还是没有头绪。于是,我就想再看看block模块,打开block.module,仔细的从头到尾看,发现一个block_theme()函数,到网上一查,这个正是定义theme的函数,刚才正是没有定义taxonomy_theme()函数,所以theme_taxonomy_overview_vocabularies()才没有被调用。问题找到后,很快就达到了效果。主要是一个API:drupal_add_tabledrag(),另外,要注意定义table的id.
效果如图如示:dragable.jpg.
以下是程序模块的一些代码,有需要的可以参考.

<?php
// Implements hook_menu
function config_menu() {
$items = array();
$items['config'] = array(
'title' => t('Config'),
'description' => t('Config'),
'access callback' => 'config_access',
'page callback' => 'drupal_get_form',
'page arguments' => array('mytaxonomy_overview_vocabularies'),
'type' => MENU_NORMAL_ITEM,
'weight' => -10,
);
return $items;
}

function config_theme() {
return array(
'mytaxonomy_overview_vocabularies' => array(
'arguments' => array('form' => array()),
),
);
}

function mytaxonomy_overview_vocabularies() {

$result = db_query('SELECT * FROM {config} ORDER BY weight');
while ($term = db_fetch_object($result)) {
$results[] = $term;
}
$form = array('#tree' => TRUE);
foreach ($results as $res) {
$form[$res->cid]['#config'] = (array)$res;
$form[$res->cid]['name'] = array('#value' => check_plain($res->name));
$form[$res->cid]['value'] = array('#value' => check_plain($res->value));
$form[$res->cid]['weight'] = array('#type' => 'weight', '#delta' => 10, '#default_value' => $res->weight);
}

// Only make this form include a submit button and weight if more than one
// vocabulary exists.
if (count($results) > 1) {
$form['submit'] = array('#type' => 'submit', '#value' => t('Save'));
}elseif (isset($res)) {
unset($form[$res->cid]['weight']);
}

return $form;
}

function theme_mytaxonomy_overview_vocabularies($form) {

$rows = array();
foreach (element_children($form) as $key) {
if (isset($form[$key]['name'])) {
$vocabulary = &$form[$key];

$row = array();
$row[] = drupal_render($vocabulary['name']);
$row[] = drupal_render($vocabulary['value']);
if (isset($vocabulary['weight'])) {
$vocabulary['weight']['#attributes']['class'] = 'myvocabulary-weight';
$row[] = drupal_render($vocabulary['weight']);
}
$rows[] = array('data' => $row, 'class' => 'draggable');
}
}
if (empty($rows)) {
$rows[] = array(array('data' => t('No vocabularies available.'), 'colspan' => '2'));
}

$header = array(t('Name'), t('Value'));
if (isset($form['submit'])) {
$header[] = t('Weight');
drupal_add_tabledrag('mytaxonomy', 'order', 'sibling', 'myvocabulary-weight');
}

return theme('table', $header, $rows, array('id' => 'mytaxonomy')) . drupal_render($form);
}

// save the change

function mytaxonomy_overview_vocabularies_submit($form, &$form_state) {

foreach ($form_state['values'] as $cid => $vocabulary) {
if (is_numeric($cid) && $form[$cid]['#config']['weight'] != $form_state['values'][$cid]['weight']) {
$form[$cid]['#config']['weight'] = $form_state['values'][$cid]['weight'];
db_query("UPDATE {config} SET weight = '%d' WHERE cid = '%d'", $form_state['values'][$cid]['weight'], $cid);
}
}
drupal_set_message(t('Save sucessful!'));
drupal_goto('config');
}

function config_access() {
return (($GLOBALS['user']->uid == 1));
}
?>


本文永久地址:http://drupalsh.cn/node/10 , 转载请注明出处.
普通分类: