Drupal 7 - 提交未在主题表上触发的功能

时间:2013-11-19 04:01:46

标签: drupal drupal-7

我希望能够从两个选择列表中选择项目,然后提交(这会将我带到另一个表单。

以下是我的代码,但是提交函数(submit_function)似乎没有被触发(它只是刷新表单)。我做错了什么?

function myfunction($form, &$form_state)
{
    $output = drupal_render($element['form_id']);
    $output .= drupal_render($element['form_build_id']);
    $output .= drupal_render($element['form_token']);

    $header = array(
    'columnone' => t('Column 1'),
    'columntwo'     => t('Column 2'),
    'columnthree'    => t('Column 3'),
    );

    $rows = array(
        array(
          'columnone' => array(
                                'data' => array(
                                    '#type' => 'select',
                                    '#options' => columnoneoptions(),
                                ),
                          ),
          'columntwo'     => array(
                                'data' => array(
                                    '#type' => 'select',
                                    '#options' => columntwooptions(),
                                ),
                          ),
          'columnthree'    =>array(
                                    'data' => array(
                                            '#type' => 'submit',
                                            '#value' => t('Add'),
                                            '#weight' => 45,
                                            '#submit' => array('submit_function'),
                                    )
                                ),
            ),
      );

    $output['table'] = array(
      '#theme' => 'table',
      '#header' => $header,
      '#rows' => $rows,
      '#weight' => 2,
    );


    return $output; 
}

1 个答案:

答案 0 :(得分:1)

这很正常,您将表单元素与静态可渲染元素混合在一起。 drupal_get_form函数需要表单构建。

您需要创建的是自定义表单主题表,它在表单构建中输出主题。以下是一些示例代码,可以帮助您进一步:

function YOURMODULE_theme()
{
    // define a table form theme
    return array(
        'YOURMODULE_table_form' => array(
            'render element' => 'element'
        ),
    );
}

function theme_YOURMODULE_table_form($vars)
{
    $element = $vars['element'];

    $form_keys = false;
    $rows = array();

    // loop through each row form elements
    foreach (element_children($element) as $key) {
        if (!$form_keys) {
            $form_keys = array();

            // retrieve the form keys for each row
            foreach (element_children($element[$key]) as $f_key) {
                $form_keys[$f_key] = isset($element[$key][$f_key]['#title'])
                    ? $element[$key][$f_key]['#title']
                    : '';
            }
        }

        $row = array();
        foreach ($form_keys as $fieldkey => $fieldname) {
            // render each field in a separate row
            $row[] = array(
                'data' => drupal_render($element[$key][$fieldkey])
            );
        }

        $rows[] = $row;
    }

    // return a build for the table
    return theme('table', array(
        'header' => $vars['element']['#header'],
        'rows' => $rows,
        'empty' => isset($vars['element']['#empty'])
            ? $vars['element']['#header']
            : '',
    ));
}

使用你的逻辑表格:

function myfunction($form, &$form_state)
{
    $form['table'] = array(
        '#theme' => 'YOURMODULE_table_form',
        '#header' => array(
            'columnone' => t('Column 1'),
            'columntwo' => t('Column 2'),
            'columnthree' => t('Column 3'),
        ),
        '#tree' => true,
    );

    $form['table'][0] = array(
        'columnone' => array(
            '#type' => 'select',
            '#options' => columnoneoptions(),
        ),
        'columntwo' => array(
            '#type' => 'select',
            '#options' => columntwooptions(),
        ),
        'columnthree' => array(
            '#type' => 'submit',
            '#value' => t('Add'),
            '#weight' => 45,
            '#submit' => array('submit_function'),
        ),
    );

    return $form;
}