Drupal 6 表单元素示例模块
在我开发Drupal站点的过程中,我经常被问到是否可以为设计师提供表单元素的示例。这是为了让他们可以使用可能出现在Drupal安装中的所有可能的表单元素来测试他们的设计,并确保我们创建的主题将是健壮的和面向未来的。我经常发现自己忘记了如何创建,比如说,一个多选元素,并且需要一种我可以在上面查找表单元素的备忘单。
为此,我坐下来创建了一个包含所有Drupal表单元素的列表,并将它们打包到一个模块中,这样当路径“form_elements”加载时,将呈现一个包含大量元素的非常大的表单。那里甚至还有一个tabledrag表单组件,在Drupal管理页面中使用了很多。
提交按钮只会将信息发布回表单页面并显示返回的数据。这意味着如果您想查看每个表单元素生成的数据类型,该模块也很有用。
要使用此模块,请创建一个名为form_elements的目录,并将以下内容添加到form_elements.info文件中。
name = Form Elements description = Prints out all of the form elements. core = 6.x
以下代码用于form_elements.module文件。
'Drupal Form Elements', 'page callback' => 'drupal_get_form', 'page arguments' => array('form_elements_form'), 'access callback' => TRUE, ); return $items; } /** * This is form containing every form element type available. */ function form_elements_form() { //复选框 $form['checkbox_element'] = array( '#type' => 'checkbox', '#title' => t('Form elements checkbox'), '#default_value' => FALSE, '#description' => t('A regular checkbox.') ); //多个复选框 $form['checkboxes_element'] = array( '#type' => 'checkboxes', '#title' => t('Form elements checkboxes'), '#options' => range(0, 5), '#default_value' => array(1), '#description' => t('A regular set of checkboxes.') ); //日期元素 $form['date_element'] = array( '#type' => 'date', '#title' => t('Form elements date'), '#description' => t('A regular date.') ); //包含文本字段元素的字段集 $form['fielset_element'] = array( '#type' => 'fieldset', '#title' => t('Form elements fieldset element'), '#collapsible' => TRUE, '#collapsed' => FALSE, ); //fieldset元素内的文本区域 $form['fielset_element']['fielset_element_textfield'] = array( '#type' => 'textfield', '#title' => t('Form elements textfield, in a fieldset'), '#description' => t('A regular textfield, in a fieldset.') ); //一个文件上传元素 $form['file_element'] = array( '#type' => 'file', '#title' => t('Form elements file'), '#description' => t('A regular file.') ); //密码元素 $form['password_element'] = array( '#type' => 'password', '#title' => t('Form elements password'), '#description' => t('A regular password.') ); //确认密码元素 $form['password_confirm_element'] = array( '#type' => 'password_confirm', '#title' => t('Form elements password_confirm'), '#description' => t('A regular password_confirm.') ); //无线电元素 $form['radio_element'] = array( '#type' => 'radio', '#title' => t('Form elements radio'), '#default_value' => TRUE, '#description' => t('A regular radio.') ); //无线电元素 $form['radios_element'] = array( '#type' => 'radios', '#title' => t('Form elements radios'), '#options' => range(0, 5), '#default_value' => 1, '#description' => t('A regular set of radios.') ); //选择元素 $form['select_element'] = array( '#type' => 'select', '#title' => t('Form elements select'), '#options' => range(0, 5), '#default_value' => 1, '#description' => t('A regular select.'), ); //多选择元素。 $form['multiple_select_element'] = array( '#type' => 'select', '#title' => t('Form elements multiple select'), '#options' => range(0, 5), '#default_value' => 1, '#description' => t('A regular multiple select.'), '#multiple' => TRUE ); //一个文本字段元素 $form['textfield_element'] = array( '#type' => 'textfield', '#title' => t('Form elements textfield'), '#description' => t('A regular textfield.') ); //文本区域元素 $form['textarea_element'] = array( '#type' => 'textarea', '#title' => t('Form elements textarea'), '#description' => t('A regular textarea.') ); //一个权重元素(通常在排序元素时使用) $form['weight_element'] = array( '#type' => 'weight', '#title' => t('Form elements weight'), '#description' => t('A regular weight.') ); //tabledrag元素的数据 $rows = array( array( 'id' => 1, 'person' => 'Joe Blogs', 'email' => '[email protected]', 'weight' => 0, ), array( 'id' => 2, 'person' => 'Jane Blogs', 'email' => '[email protected]', 'weight' => 10, ), ); //Tabledrag元素 foreach ($rows as $row) { $form['tabledragrows'][$row['id']]['person_' . $row['id']] = array( '#type' => 'textfield', '#default_value' => $row['person'], '#size' => 20, ); $form['tabledragrows'][$row['id']]['email_' . $row['id']] = array( '#type' => 'textfield', '#default_value' => $row['email'], '#size' => 20, ); //重量形式元素。 $form['tabledragrows'][$row['id']]['weight_' . $row['id']] = array( '#type' => 'weight', '#delta' => 50, '#default_value' => $row['weight'], '#attributes' => array('class' => 'weight'), ); } //一个按钮元素 $form['button_element'] = array( '#type' => 'button', '#value' => t('Button'), ); //一个图片按钮元素 $form['image_button_element'] = array( '#type' => 'image_button', '#value' => t('Image Button'), '#src' => 'misc/druplicon.png' ); //隐藏元素 $form['hidden_element'] = array( '#type' => 'hidden', '#value' => t('A hidden element'), ); //令牌元素 $form['token_element'] = array( '#type' => 'token', '#default_value' => t('A token element'), ); //标记(HTML)元素 $form['markup_element'] = array( '#type' => 'markup', '#value' => '' . t('A markup element') . '
' ); //一个项目元素 $form['item_element'] = array( '#type' => 'item', '#title' => t('An item element'), '#value' => t('An item element value') ); //一个值元素(内部使用) $form['value_element'] = array( '#type' => 'value', '#default_value' => t('A value element') ); //一个提交元素 $form['submit'] = array( '#type' => 'submit', '#value' => t('Submit'), ); return $form; } /** * Implements hook_submit() for the form_elements_form() form. */ function form_elements_form_submit($form, &$form_state) { //排除不必要的元素。 unset($form_state['values']['submit'], $form_state['values']['form_id'], $form_state['values']['op'], $form_state['values']['form_token'], $form_state['values']['form_build_id']); foreach ($form_state['values'] as $key => $value) { variable_set($key, $value); drupal_set_message(t('%name has value %value', array('%name' => $key, '%value' => print_r($value, TRUE)))); } } /** * Implementation of hook_theme(). */ function form_elements_theme() { return array( 'form_elements_form' => array( 'arguments' => array( 'form' => NULL ), ), ); } /** * Theme for form_element_form. Used to create the tabledrag element and then * render the rest of the form. */ function theme_form_elements_form($form) { $table_rows = array(); if (is_array($form['tabledragrows'])) { //循环遍历表数组中的每一“行”"row" in the table array foreach ($form['tabledragrows'] as $id => $row) { //我们只对数字键感兴趣 if (intval($id)) { $this_row = array(); $this_row[] = drupal_render($form['tabledragrows'][$id]['person_' . $id]); $this_row[] = drupal_render($form['tabledragrows'][$id]['email_' . $id]); //将权重字段添加到行 $this_row[] = drupal_render($form['tabledragrows'][$id]['weight_' . $id]); //将行添加到行数组 $table_rows[] = array('data' => $this_row, 'class' => 'draggable'); } } } //确保标题计数与列计数匹配 $header = array( "Person", "Email", "Weight" ); $form['tabledragrows'] = array( '#value' => theme('table', $header, $table_rows, array('id' => 'person-email')) ); $output = drupal_render($form); //调用add_tabledrag添加和设置JavaScript //这里的关键是第一个参数——表ID //和第四个参数,持有权重的表单项的类 drupal_add_tabledrag('person-email', 'order', 'sibling', 'weight'); return $output; }
启用此模块后,您可以导航到/form_elements并查看大型表单。
这个模块的代码也可以在github上找到。