有时候需要在drupal theme 渲染的表格里插入一些表单元素,比如表格里加入上传文件功能。那么如何操作呢?
1.在模块下的yourModule.module 文件下些一个hook
function yourModuleName_theme() {
return array(
'customize_table' => array(
'render element' => 'form',
),
);
}
2.我新建了一个customize_table.inc,构造需要在主题中渲染的表单元素
function customize_table($form, &$form_state) { $form['table_column1'] = array( '#type' => 'value', '#value'=> '表格值' );
$form['table_column2'] = array( '#type' => 'value', '#value'=> '表格值' );
$form['table_file'] = array( '#type' => 'managed_file', '#upload_locaiton'=> 'public://yourModulePath/table' );
}
3.在customize_table.inc文件中注册一个主题渲染tablefunction theme_customize_table($variables) { $form = $variables['form']; $header = array('列1', '列2', '列3'); $rows[] = array( $form['table_column1'], $form['table_column2'], render($form['table_file']), ); $output = theme('table', array('header' => $header, 'rows' => $rows)); $output .= drupal_render_children($form); return $output; }
4.打开方法customize_table对应的链接就能看到渲染的表格中第三列里有一个上传文件