【问题标题】:How to include drupal form elements in a data table如何在数据表中包含 drupal 表单元素
【发布时间】:2009-11-20 15:20:47
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: php forms drupal datatable drupal-fapi


    【解决方案1】:

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

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

    一种解决方法是将中间按钮的 HTML 放入表数据中,以便由主题表输出。这样您就不必编写自己的主题函数了。

    对于 AJAX 调用,您需要使用 hook_menu() 和自定义函数构建自己的菜单回调。 (代码sn-ps来自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() 对响应进行编码。

    【讨论】:

      【解决方案2】:

      你为什么使用标准的 theme_table() 函数?只需制作您自己的主题功能,其中包括您需要的表单元素。更多关于主题功能herehere

      【讨论】:

      • 感谢您的回复,但它并没有真正帮助我。我是 drupal 新手,因此需要更多帮助,而不仅仅是文档链接。你能用一些基本步骤和一些伪代码解释你的意思吗?谢谢!
      【解决方案3】:

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

      实施: - 使用 scheme-api 编写您的查询(我认为您已经为您的主题('table',...) 这样做了) - 为每一行创建一个这样的表单: $form[$row->id]['column_name_1'] = array(...here description of the column -> checkbox, textfield...); $form[$row->id]['column_name_2'] = array(...); - 编写您自己的主题功能,使用 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() 的实现。 */ 函数 mymodule_theme() {
      返回数组( 'mymodule_event_type_overview_form' => 数组( '参数' => 数组(), ), ); }

      玩得开心:-) 卡佳

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-05-23
        • 1970-01-01
        • 2016-07-03
        • 2023-04-01
        • 1970-01-01
        • 2023-03-18
        • 2011-02-23
        相关资源
        最近更新 更多