Drupal Form API - 抓取数组()键 - > #value

时间:2011-08-25 14:28:47

标签: php drupal-6 drupal-fapi

我有一个hook_form(),我正在尝试将数据提交到数据库 生成表单选项后,会生成类似<option value="foo"> Foo </option>

的内容

我不是想要获得期权的“价值” 我试图抓住<option>之间的部分,这将是'Foo'

我的表格如下:

/*
 * Implentation of hook_form().
 */
function f25_favorites_form() {
  $form['path_options'] = array(
    '#type' => 'value',
    '#value' => array('default' => t('Add a favorite'), 'foo' => t('Foo'), 'bar' => t('Bar')),
  );  

  $form['path'] = array(
    '#type' => 'select',
    '#title' => t('Select Page'),
    '#required' => TRUE,
    '#weight' => '11',
    '#options' => $form['path_options']['#value'],
  );

  $form[submit] = array(
    '#type' => 'submit',
    '#weight' => '1000000',
    '#value' => t('Add')
  );

  return $form;
}

这是form_submit():

/*
 * Write Form data to database
 */
function f25_favorites_form_submit($form, &$form_state){
  global $user;
  $data = array(
    'uid' => $user->uid,
    'path' => $form_state['values']['path'],
    'title' => $title,
    'weight' => $weight,
    'timestamp' => time(),
  );

  drupal_write_record('', $data);
}

我期待什么

我想到有类似的东西:

    'path' => $form_state['values']['path'],

但是应该是这样的:

    'title' => $form_state['values']['path'][#value],

1 个答案:

答案 0 :(得分:1)

根据Drupal docs $ form变量包含来自f25_favorites_form()的数据,所以这应该有效:

'title' => $form['path_options']['#value'][$form_state['values']['path']],

基本上你会选择所选的值(通过$ form_state ['values'] ['path']收到),因为它是$ form ['path_options'] ['#value']数组的一个键,你应该使用它找到文本。