如何在数据表中包含drupal表单元素

时间:2009-11-20 15:20:47

标签: php forms drupal datatable drupal-fapi

我有一个数据表,其中填充了与drupal内容无关的数据(来自第三方系统)。该数据涉及必须被批准/标记为不当的照片。

所以我正在写一个Drupal管理模块,它应该调整这个内容。到目前为止,我已经使用主题('table',...)构建了一个表,每行显示1张照片,以及其他一些元数据。我现在想在表中包含一些表单按钮,并构建一些由这些按钮触发的Ajax操作。

这篇文章看起来很相似http://drupal.org/node/112358,但我担心这不是Drupal 6的做法。

任何人都可以就如何最好地解决这个问题提供一些建议 - 最好使用核心模块/表单覆盖功能。 Drupal版本是6.14。

3 个答案:

答案 0 :(得分:2)

自定义主题功能允许您以完全自定义的方式呈现内容。您还可以为内容创建自定义模板,这可能包括按钮。

hook_theme()将允许您创建自己的内容类型以添加​​到主题功能,以便您可以theme('moderatedimages')

解决方法是将适度按钮的HTML放入表数据中,以便主题表输出。这将节省您必须编写自己的主题功能。

对于AJAX调用,您需要使用hook_menu()和自定义函数构建自己的菜单回调。 (代码片段来自this教程。)

<?php
function mymodule_products_menu() {

  $items = array();

  $items['products/get'] = array(
    'title' => 'mymodule callback',
    'page callback' => 'mymodule_myfunction',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK
  );

  return $items;
}

这将调用函数mymodule_myfunction()。关于此功能需要记住的一件事是您要打印结果并退出。您可能希望使用drupal_json()对响应进行编码。

答案 1 :(得分:1)

为什么使用标准的theme_table()函数?只需创建自己的主题功能,其中包含您需要的表单元素。有关主题函数herehere的更多信息。

答案 2 :(得分:1)

我遇到了同样的问题。我在drupal-directory / modules / menu / menu.admin.inc中找到了解决方案。 Form-API和主题将仅用于使用!

实现: 用scheme-api编写你的查询(我想你已经为你的主题('table',...)做了) - 为每一行创建一个这样的表单: $ form [$ row-&gt; id] ['column_name_1'] = array(...此处列的描述 - &gt;复选框,textfield ...); $ form [$ row-&gt; id] ['column_name_2'] = array(...); - 写你自己的主题 - funktion,女巫使用hook_theme_table(你已经做过了,你只需要将一些单元格改成复选框或其他表单元素。)

我现在写一个提交功能,但它不起作用:-)有关更多信息,请参阅菜单模块。

我的代码:

/**   
  * Form for editing the event types.
  *  
  * @ingroup forms  
  */
  function mymodule_event_type_overview_form() {  
    $vid = variable_get('mymodule_category_vocabulary', '');
    $sql = "SELECT term_data.tid AS tid,
      term_data.name AS tname,
      term_data.vid AS vid,
      term_site_config.color AS color,
      term_site_config.site_enabled AS site_enabled,
      term_site_config.site_shown AS site_shown
      FROM {term_data} term_data
      INNER JOIN {term_site_config} term_site_config 
      ON term_data.tid = term_site_config.tid
      WHERE term_data.vid = %d";

    $result = db_query(db_rewrite_sql($sql), $vid);

  $form = array();   while ($term = db_fetch_object($result)) {
    $form[$term->tid]['tname'] = array(
      '#type' => 'value',
      '#value' => $term->tname,
    );
    $form[$term->tid]['color'] = array(
      '#type' => 'value',
      '#value' => $term->color,
    );
    $form[$term->tid]['enabled'] = array(
      '#type' => 'checkbox',
      '#default_value' => $term->site_enabled,
    );
    $form[$term->tid]['shown'] = array(
      '#type' => 'checkbox',
      '#default_value' => $term->site_shown,
    );

    // Build a list of operations.
    $operations = array();
    $operations['delete'] = l(t('delete'), 'admin/settings/mymodule/eventtype/'. $term->tid .'/delete');
    $operations['edit'] = l(t('edit'), 'admin/settings/mymodule/eventtype/'. $term->tid .'/edit');

    $form[$term->tid]['operations'] = array();
    foreach ($operations as $op => $value) {
      $form[$term->tid]['operations'][$op] = array('#value' => $value);
    }   }
     if (element_children($form)) {
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Save configuration'),
    );
    $form['reset'] = array(
      '#type' => 'submit',
      '#value' => t('Reset to defaults'),
    );
    if (!empty($_POST) && form_get_errors()) {
      drupal_set_message(t('The settings have not been saved because of the errors.'), 'error');
    }   }else {
    $form['empty'] = array('#value' => t('There are no event types yet.'));   }
     return $form; }

/**  
  * Theme the event type overview form into a table.  
  *
  * @ingroup themeable  
  */
function theme_mymodule_event_type_overview_form($form) {

  $header = array(
    t('Event type'),
    t('Color'),
    array('data' => t('Enabled'), 'class' => 'checkbox'),
    array('data' => t('Shown'), 'class' => 'checkbox'),
    array('data' => t('Operations'), 'colspan' => '2'),   );

  $rows = array();   foreach (element_children($form) as $id) {
    $element = &$form[$id];
    if (isset($element['tname'])) {
      $row = array(
        t($element['tname']['#value']),
        t($element['color']['#value']),
        array('data' => drupal_render($element['enabled']), 'class' => 'checkbox'),
        array('data' => drupal_render($element['shown']), 'class' => 'checkbox'),
        array('data' => drupal_render($element['operations']['delete'])),
        array('data' => drupal_render($element['operations']['edit'])),
      );

    $rows[] = $row;   }   }   $output = '';   if ($rows) {
    $output .= theme('table', $header, $rows);   }   
    $output .= drupal_render($form);   
    return $output; 

}

You will get the html-code of the form if you call drupal_get_form('mymodule_event_type_overview_form');

and don't forget co write following function in mymodule.module:

/ **    * hook_theme()的实现。    * / function mymodule_theme(){
        返回数组(           'mymodule_event_type_overview_form'=&gt;阵列(             'arguments'=&gt;阵列(),            )         );        }

玩得开心:-) 卡佳