2013-01-21 51 views
1

我試圖在選擇框中填充第一個選定內容類型名稱的所有日期字段的第二個選擇框。我使用ajax_callback通過$ form_state獲取選定的值。我收到錯誤,我無法確定原因。任何人都可以幫忙嗎?在ajax_callback中傳遞參數:返回數組未填充

這是我的自定義模塊代碼。

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'); 
} 

以下是錯誤的,我在彈出的我越來越 -

An AJAX HTTP error occurred. HTTP Result Code: 200 Debugging information follows. Path: /module_dev/?q=system/ajax StatusText: OK ResponseText: Fatal error: Cannot use object of type stdClass as array in /var/www/module_dev/includes/common.inc on line 5786

我通過上線5786的/var/www/module_dev/includes/common.inc去了,這是我在那裏找到的代碼。

function drupal_render(&$elements) { 
    // Early-return nothing if user does not have access. 
    if (empty($elements) || (isset($elements['#access']) && !$elements['#access'])) { 
    return; 
    } 

    // Do not print elements twice. 
    if (!empty($elements['#printed'])) { 
    return; 
    } 

    // Try to fetch the element's markup from cache and return. 
    if (isset($elements['#cache'])) { 
    $cached_output = drupal_render_cache_get($elements); 
    if ($cached_output !== FALSE) { 
     return $cached_output; 
    } 
    } 

    // If #markup is set, ensure #type is set. This allows to specify just #markup 
    // on an element without setting #type. 
    if (isset($elements['#markup']) && !isset($elements['#type'])) { 
    $elements['#type'] = 'markup'; 
    } 

    // If the default values for this element have not been loaded yet, populate 
    // them. 
    if (isset($elements['#type']) && empty($elements['#defaults_loaded'])) { 
    $elements += element_info($elements['#type']); 
    } 

    // Make any final changes to the element before it is rendered. This means 
    // that the $element or the children can be altered or corrected before the 
    // element is rendered into the final text. 
    if (isset($elements['#pre_render'])) { 
    foreach ($elements['#pre_render'] as $function) { 
     if (function_exists($function)) { 
     $elements = $function($elements); 
     } 
    } 
    } 

    // Allow #pre_render to abort rendering. 
    if (!empty($elements['#printed'])) { 
    return; 
    } 

    // Get the children of the element, sorted by weight. 
    $children = element_children($elements, TRUE); 

    // Initialize this element's #children, unless a #pre_render callback already 
    // preset #children. 
    if (!isset($elements['#children'])) { 
    $elements['#children'] = ''; 
    } 
    // Call the element's #theme function if it is set. Then any children of the 
    // element have to be rendered there. 
    if (isset($elements['#theme'])) { 
    $elements['#children'] = theme($elements['#theme'], $elements); 
    } 
    // If #theme was not set and the element has children, render them now. 
    // This is the same process as drupal_render_children() but is inlined 
    // for speed. 
    if ($elements['#children'] == '') { 
    foreach ($children as $key) { 
     $elements['#children'] .= drupal_render($elements[$key]); 
    } 
    } 

    // Let the theme functions in #theme_wrappers add markup around the rendered 
    // children. 
    if (isset($elements['#theme_wrappers'])) { 
    foreach ($elements['#theme_wrappers'] as $theme_wrapper) { 
     $elements['#children'] = theme($theme_wrapper, $elements); 
    } 
    } 

    // Filter the outputted content and make any last changes before the 
    // content is sent to the browser. The changes are made on $content 
    // which allows the output'ed text to be filtered. 
    if (isset($elements['#post_render'])) { 
    foreach ($elements['#post_render'] as $function) { 
     if (function_exists($function)) { 
     $elements['#children'] = $function($elements['#children'], $elements); 
     } 
    } 
    } 

    // Add any JavaScript state information associated with the element. 
    if (!empty($elements['#states'])) { 
    drupal_process_states($elements); 
    } 

    // Add additional libraries, CSS, JavaScript an other custom 
    // attached data associated with this element. 
    if (!empty($elements['#attached'])) { 
    drupal_process_attached($elements); 
    } 

    $prefix = isset($elements['#prefix']) ? $elements['#prefix'] : ''; 
    $suffix = isset($elements['#suffix']) ? $elements['#suffix'] : ''; 
    $output = $prefix . $elements['#children'] . $suffix; 

    // Cache the processed element if #cache is set. 
    if (isset($elements['#cache'])) { 
    drupal_render_cache_set($output, $elements); 
    } 

    $elements['#printed'] = TRUE; 
    return $output; 
} 
+0

檢查該行。您正嘗試以數組數據的形式訪問對象類型數據。 –

+0

但是,這個查詢返回我只是名稱..它可以成爲stdClass? – RajeevK

+0

只要把錯誤發生在哪一行? –

回答

0

你的AJAX回調應該返回是,它取代了<div>標籤,其CSS ID設置爲#ajax [包裝]的內容的表單元素渲染陣列。你的AJAX回調函數返回的是一個對象數組,這不是渲染API所期望的。 (渲染API使用數組。)這就是爲什麼你會得到一個錯誤,指出一個對象不能用作數組。

請參閱ajax_example_simplest()作爲使用AJAX的表單構建器的示例;尤其是看到它的AJAX回調,ajax_example_simplest_callback()

總之,mymodule_datefields_ajax_callback()的代碼應該是下面的代碼。

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_options是具有格式value => string_to_show陣列;它與$content_type_options使用的格式相同。

+0

我經歷了這個例子,我之前通過谷歌搜索得到了這個例子,但事情是我不知道如何安排這個查詢的結果適當的返回呈現API所期望的格式。你能告訴我這個嗎?對不起,天真的問題.. – RajeevK