Drupal 6以设置形式创建CRUD网格

时间:2012-06-27 08:57:10

标签: drupal drupal-6 themes

我对Drupal很新,我想创建一个自定义网格,其中包含一些可编辑的列,其他一些列则是复选框等。

我正在使用theme()函数创建表并在设置中呈现它。由于我找不到任何方法来访问表单/设置变量,在主题函数中,我在drupal数据库中创建一个包含gid值的自定义表,然后我渲染这些行。出于测试目的,我获取'变量'表行。这是迄今为止的代码:

 $form['module_settings']['profile_grid'] = array(
        '#type' => 'item',
        '#title' => t('Profile Mapping'),
        '#theme' => 'profile_mapping_grid'
    );


function theme_profile_mapping_grid($sender) {
    $header = array('', t('name'), t('value'));
    $result = db_query('SELECT v.name, v.value from {variable} v');
    while ($pair = db_fetch_object($result)) { 
        $format = array(
            '#type' => 'textfield',
            '#size' => 30,
            '#value' => $pair->name
        );
        $hhfield = array(
            '#type' => 'textfield',
            '#size' => 30,
            '#value' => $pair->value
        );
        $row = array('');
        $row[] = drupal_render($format);
        $row[] = drupal_render($hhfield);
        $rows[] = array('data' => $row);    
    }

    $output = theme('table', $header, $rows, array('id' => 'gridErrors'));
    return $output;
}

网格生成正确,但我遇到了问题。我无法在文本字段中设置'name'属性,以便稍后在提交操作上收集它的值。 此外,我不确定这是否是创建设置网格的最佳方式。

任何想法,意见等都非常受欢迎。

2 个答案:

答案 0 :(得分:0)

您正在生成主题函数中的字段,但它不起作用,因为提交函数只会对您的表单函数生成的字段起作用。

正确的方法是生成表单函数中的所有字段(基本上是您现在在主题函数中执行的操作)并使用drupal_render在主题中呈现它们。

  

由于我无法找到任何方法来访问表单/设置变量,在主题函数

表单作为参数传递给主题函数,在你的情况下是$ sender,这就是你访问它们的方式。

这是一些未经测试的代码,但你明白了。请记住,调用$output .= drupal_render($form);非常重要,因为它会呈现Form API所需的隐藏字段,例如form_id和CSRF保护。没有这个电话,提交将无效。

function mymodule_grid_form($form_state) {
    $form = array();
    $form['variables'] = array();

    $result = db_query('SELECT v.name, v.value from {variable} v');
    while ($pair = db_fetch_object($result)) { 
        $form['variables'][$pair->name][$pair->name . '_name'] = array(
            '#type' => 'textfield',
            '#size' => 30,
            '#value' => $pair->name
        );
        $form['variables'][$pair->name][$pair->name . '_value'] = array(
            '#type' => 'textfield',
            '#size' => 30,
            '#value' => $pair->value
        );
    }

    return $form;
}

function theme_mymodule_grid_form($form) {
    $header = array('', t('name'), t('value'));

    foreach ($form['variables'] as $variable => $values) {
        $row = array('');
        $row[] = drupal_render($values[$variable . '_name']);
        $row[] = drupal_render($values[$variable . '_value']);
        $rows[] = array('data' => $row); 
    }

    $output = theme('table', $header, $rows, array('id' => 'gridErrors'));
    $output .= drupal_render($form);
    return $output;
}

function mymodule_theme() {
  return array(
    'mymodule_grid_form' => array(
      'arguments' => array('form' => NULL),
    ),
  );
}

答案 1 :(得分:0)

根据Drupal 6 Form API Quickstart Guide

  
      
  1. 不要对用户可以更改的任何表单元素使用'#value'属性。请改用“#default_value”属性。不要把$ form_state ['values'](或$ _POST)的值放在这里! FormsAPI将为您处理;只把这个字段的原始值放在这里。
  2.   

您必须使用'#default_value' => $pair->name而不是'#value' => $pair->value

以下是带有设置表的示例模块。

安装文件:

<?php
// $Id$

/**
 * @file
 * The your_module module install file, which handles the install/uninstall tasks.
 *
 */

function your_module_install() {
  // Create tables.
  drupal_install_schema('your_module');
}

/**
 * Implementation of hook_schema().
 */
function your_module_schema() {

/* Settings table */
$schema['your_module_settings'] = array(
    'description' => 'Stores module settings.',
    'fields' => array(
        'record_id' => array(
            'description' => 'The primary identifier for a record.',
            'type' => 'serial', 
            'unsigned' => TRUE, 
            'not null' => TRUE),
        'item_code' => array(
            'type' => 'varchar', 
            'length' => '255', 
            'not null' => TRUE),
        'setting_one' => array(
            'type' => 'varchar', 
            'length' => '255', 
            'not null' => TRUE),
        'setting_two' => array(
            'type' => 'varchar', 
            'length' => '255')),
    'unique keys' => array(
        'record_id' => array('record_id')),
    'primary key' => array('record_id')
);

  return $schema;
}

/**
 * Implementation of hook_uninstall().
 */
function your_module_uninstall() {
  // Remove tables.
  drupal_uninstall_schema('your_module');
}

模块文件:

<?php

/**
 * Implementation of hook_help()
 */
function your_module_help($path, $arg) 
{
  switch ($path) 
  {
    case 'admin/help#your_module':
      return '<p>'. t('Module description.') .'</p>';
  }
}

/**
 * Implementation of hook_menu()
 */
function your_module_menu()
{
  $items = array();

  /* module settings page */
  $items['admin/settings/your_module'] = array(
    'description' => 'Administer your_module module settings.',
    'title' => 'Some title',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('your_module_admin_settings'),
    'access arguments' => array('access administration pages'),
    'type' => MENU_NORMAL_ITEM,
    );

  return $items;
}

/**
 * Implementation of hook_theme().
 */
function your_module_theme() {
  return array(
    'your_module_products_settings' => array(
      'arguments' => array('form' => NULL),
    ),
  );
}

/**
 * Settings form
 */
function your_module_admin_settings(&$form_state)
{
  $form['your_module'] = array(
      '#type' => 'fieldset',
      '#title' => t('Settings'),
      '#description' => t('Settings description.'),
    );

  $form['your_module']['products_settings_table'] = array(
      '#theme' => 'your_module_products_settings',
      '#tree' => TRUE,
    );

  $form['your_module']['products_settings_table']['products_settings'] = array();

  $result = db_query('SELECT record_id, item_code, setting_one, setting_two  FROM {your_module_settings} ORDER BY item_code');

  while ($product_setting = db_fetch_object($result)) {
    $form['your_module']['products_settings_table']['products_settings'][$product_setting->record_id] = array();

    $form['your_module']['products_settings_table']['products_settings'][$product_setting->record_id]['item_code'] = array(
      '#type' => 'textfield',
      '#default_value' => $product_setting->item_code,
      '#size' => 8,
      '#maxlength' => 16,
    );

    $form['your_module']['products_settings_table']['products_settings'][$product_setting->record_id]['setting_one'] = array(
      '#type' => 'textfield',
      '#default_value' => $product_setting->setting_one,
      '#size' => 16,
      '#maxlength' => 16,
    );

    $form['your_module']['products_settings_table']['products_settings'][$product_setting->record_id]['setting_two'] = array(
      '#type' => 'textfield',
      '#default_value' => $product_setting->setting_two,
      '#size' => 40,
      '#maxlength' => 255,
    );
  }

  /* "add new row" fields */
  $form['your_module']['products_settings_table']['products_settings_new'] = array();

  /* new item_code */
  $form['your_module']['products_settings_table']['products_settings_new']['item_code'] = array(
    '#type' => 'textfield',
    '#size' => 8,
    '#maxlength' => 16,
    '#description' => t('description'),
  );

  /* new setting_one */
  $form['your_module']['products_settings_table']['products_settings_new']['setting_one'] = array(
    '#type' => 'textfield',
    '#size' => 16,
    '#maxlength' => 16,
    '#description' => t('description'),
  );

  /* setting_two */
  $form['your_module']['products_settings_table']['products_settings_new']['setting_two'] = array(
    '#type' => 'textfield',
    '#size' => 40,
    '#maxlength' => 255,
    '#description' => t('description'),
  );

  /* Submit button */
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save Settings'),
    '#name' => 'SubmitButton',
  );

  return $form;
}

/**
 * Custom theme function for a table of products settings
 */
function theme_your_module_products_settings($form) {
  $header = array(t('Item Code'), t('Setting One'), t('Setting Two'));
  $rows = array();

  /* saved rows */
  foreach (element_children($form['products_settings']) as $key) {
    $row = array();
    $row[] = drupal_render($form['products_settings'][$key]['item_code']);
    $row[] = drupal_render($form['products_settings'][$key]['setting_one']);
    $row[] = drupal_render($form['products_settings'][$key]['setting_two']);
    $rows[] = $row;
  }

  /* new row to add */
  $row = array();
  $row[] = drupal_render($form['products_settings_new']['item_code']);
  $row[] = drupal_render($form['products_settings_new']['setting_one']);
  $row[] = drupal_render($form['products_settings_new']['setting_two']);
  $rows[] = $row;

  $output = theme('table', $header, $rows);
  return $output;
}

/**
 * Submission function for your_module_admin_settings form.
 */
function your_module_admin_settings_submit($form, &$form_state) {

  /* processing changes */
  if (isset($form_state['values']['products_settings_table']['products_settings'])) {
    foreach ($form_state['values']['products_settings_table']['products_settings'] as $record_id => $data) {
      if (empty($data['item_code']) && empty($data['setting_one']) && empty($data['setting_two'])) {
        /* delete saved row if all fields are empty */
        db_query("DELETE FROM {your_module_settings} WHERE record_id=%d", $record_id);

        drupal_set_message(t('Deleted.'), 'status');
      }
      else {
        /* update */
        db_query("UPDATE {your_module_settings} SET item_code='%s', setting_one='%s', setting_two='%s' WHERE record_id=%d",
          $data['item_code'], $data['setting_one'], $data['setting_two'], $record_id);
      }
    }
  }

  /* adding new row */
  $item_code = $form_state['values']['products_settings_table']['products_settings_new']['item_code'];
  $setting_one = $form_state['values']['products_settings_table']['products_settings_new']['setting_one'];
  $setting_two = $form_state['values']['products_settings_table']['products_settings_new']['setting_two'];

  if (!empty($item_code) && !empty($setting_one) && !empty($setting_two)) {

    db_query("INSERT INTO {your_module_settings} (item_code, setting_one, setting_two) VALUES ('%s', '%s', '%s')",
        $item_code, $setting_one, $setting_two);

    drupal_set_message(t('Added new setting.'), 'status');
  }

  drupal_set_message(t('Settings updated.'), 'status');
}