drupal7中的自动填充字段

时间:2016-12-08 11:48:06

标签: drupal-7 autofill entityreference

我正在尝试从选择列表中选择项目后自动填充文本字段。我的意思是:首先我希望用户从选择列表中选择一个项目,然后会有另外3个文本字段,我想根据选择的内容给出不同的值。

1 个答案:

答案 0 :(得分:0)

您需要使用Drupal" Ajax框架"。请在hook_form_alter函数中准备好字段。

function hook_form_alter(&$form, &$form_state, $form_id) {
  if (isset($form['type']) && $form['type']['#value'] . '_node_settings' == $form_id) {
    $form['select_field'] = array(
      '#ajax' => array(
        'callback' => '_mymodule_ajax_example_simplest_callback',
        'wrapper' => 'replace_textfield_div',
       ),
    );

    // This entire form element will be replaced with an updated value.
    $form['textfield_to_autofill'] = array(
      '#prefix' => '<div id="replace_textfield_div">',
      '#suffix' => '</div>',
    );
  }
}
function _mymodule_ajax_example_simplest_callback(&$form, $form_state) {
  // The form has already been submitted and updated. We can return the replaced
  // item as it is.
  $commands = array();
  if($form_state['values']['select_field'][LANGUAGE_NONE][0]['value'] == "some_value"){
    $form['textfield_to_autofill'][LANGUAGE_NONE][0]['value']['#value'] = "some_value";
    $commands[] = ajax_command_replace("#replace_textfield_div", render($form['textfield_to_autofill']));
  }
  $page = array('#type' => 'ajax', '#commands' => $commands);
  ajax_deliver($page);
}

此处帮助链接ajax framework