在ajax_回调中传递参数:未填充返回数组
我试图用选择框中第一个选定内容类型名称的所有日期字段填充第二个选择框。我使用ajax\u callback通过$form\u state获取所选值。我遇到了错误,我无法确定原因。有人能帮忙吗 这是我的自定义模块代码在ajax_回调中传递参数:未填充返回数组,ajax,drupal,drupal-7,Ajax,Drupal,Drupal 7,我试图用选择框中第一个选定内容类型名称的所有日期字段填充第二个选择框。我使用ajax\u callback通过$form\u state获取所选值。我遇到了错误,我无法确定原因。有人能帮忙吗 这是我的自定义模块代码 function mymodule_settings($form, &$form_state){ $content_type_options = array(); $result = db_query("SELECT * FROM {node_type}");
function mymodule_settings($form, &$form_state){
$content_type_options = array();
$result = db_query("SELECT * FROM {node_type}");
foreach($result as $record){
$content_type_options[$record->type] = $record->name;
}
$form = array();
$form['content_type'] = array(
'#title' => t('Content Types'),
'#description' => t('Select a content type.'),
'#type' => 'select',
'#options' => $content_type_options,
'#ajax' => array(
'event' => 'change',
'wrapper' => 'reg-start-date',
'callback' => 'mymodule_datefields_ajax_callback',
'method' => 'replace',
),
);
$form['checkboxes_fieldset'] = array(
'#title' => t("Start Date"),
'#prefix' => '<div id="reg-start-date">',
'#suffix' => '</div>',
'#type' => 'select',
'#description' => t('Select the date field'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
return $form;
}
function mymodule_datefields_ajax_callback($form, $form_state) {
$fieldname = $form_state['triggering_element']['#value'];
$field_query = db_query("SELECT fc.field_name FROM {field_config} fc, {field_config_instance} fci
WHERE fc.field_name = fci.field_name
AND fc.type = 'datetime'
AND fci.bundle = '".$fieldname."'");
$datefield_options = array();
foreach($field_query as $record){
$datefield_options = $record;
}
return $datefield_options;
//dpm($form_state, 'AJAX $form_state');
}
AJAX回调应该返回表单元素的呈现数组,该表单元素替换CSS ID设置为AJAX[wrapper]的标记的内容。AJAX回调返回的是一个对象数组,这不是渲染API所期望的。渲染API使用数组。这就是为什么会出现一个错误,指出对象不能用作数组 请参见使用AJAX的表单生成器示例;特别是,请参见其AJAX回调 简而言之,mymodule_datefields_ajax_回调的代码应该如下所示
function mymodule_datefields_ajax_callback($form, $form_state) {
return $form['checkboxes_fieldset'];
}
表单生成器应使用以下代码
function mymodule_settings($form, &$form_state){
$content_type_options = array();
$result = db_query("SELECT * FROM {node_type}");
foreach ($result as $record) {
$content_type_options[$record->type] = $record->name;
}
// $form is already passed as argument; you don't need to initialize it to an empty array.
// $form = array();
$form['content_type'] = array(
'#title' => t('Content Types'),
'#description' => t('Select a content type.'),
'#type' => 'select',
'#options' => $content_type_options,
'#ajax' => array(
'event' => 'change',
'wrapper' => 'reg-start-date',
'callback' => 'mymodule_datefields_ajax_callback',
'method' => 'replace',
),
);
// An AJAX request call to the form builder function has been done.
if (!empty($form_state['values']['content_type'])) {
// Use $form_state['values']['content_type'] to get the option list.
// Set the value of $date_options with that list.
$field_query = db_query("query to execute");
$date_options = array();
foreach ($field_query as $record) {
$date_options[$record->field_name] = $record->field_name;
}
}
else {
$date_options = array();
}
$form['checkboxes_fieldset'] = array(
'#title' => t("Start Date"),
'#prefix' => '<div id="reg-start-date">',
'#suffix' => '</div>',
'#type' => 'select',
'#options' => $date_options,
'#description' => t('Select the date'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
return $form;
}
$date\u options是一个数组,其格式值=>string\u to\u show;这与您用于$content\u type\u选项的格式相同。检查该行。您正试图以数组数据的形式访问对象类型数据。但此查询仅返回名称。它可能会成为stdClass?只需输入发生错误的行?让我编辑问题,使其变得更清晰我浏览了示例,我以前在谷歌搜索时也遇到过这个问题,但问题是我不知道如何正确安排这个查询的结果,以呈现API所期望的格式返回。你能给我推荐这个吗?对不起,这个天真的问题。。
function mymodule_settings($form, &$form_state){
$content_type_options = array();
$result = db_query("SELECT * FROM {node_type}");
foreach ($result as $record) {
$content_type_options[$record->type] = $record->name;
}
// $form is already passed as argument; you don't need to initialize it to an empty array.
// $form = array();
$form['content_type'] = array(
'#title' => t('Content Types'),
'#description' => t('Select a content type.'),
'#type' => 'select',
'#options' => $content_type_options,
'#ajax' => array(
'event' => 'change',
'wrapper' => 'reg-start-date',
'callback' => 'mymodule_datefields_ajax_callback',
'method' => 'replace',
),
);
// An AJAX request call to the form builder function has been done.
if (!empty($form_state['values']['content_type'])) {
// Use $form_state['values']['content_type'] to get the option list.
// Set the value of $date_options with that list.
$field_query = db_query("query to execute");
$date_options = array();
foreach ($field_query as $record) {
$date_options[$record->field_name] = $record->field_name;
}
}
else {
$date_options = array();
}
$form['checkboxes_fieldset'] = array(
'#title' => t("Start Date"),
'#prefix' => '<div id="reg-start-date">',
'#suffix' => '</div>',
'#type' => 'select',
'#options' => $date_options,
'#description' => t('Select the date'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
return $form;
}