2012-09-14 22 views
6

Tôi đang cố tạo tiện ích tùy chỉnh nhưng khi tôi gửi, Drupal dường như không lưu bất kỳ dữ liệu nào. Khi sử dụng hook_field_attach_submit() để hiển thị dữ liệu tôi đã dán, nó được liệt kê là rỗng.widget trường drupal không lưu dữ liệu đã gửi

Kỳ lạ thay, nếu tôi thay đổi #type thành một trường văn bản duy nhất thay vì một fieldset, nó sẽ chỉ lưu ký tự đầu tiên của chuỗi đã được nhập.

Điều này có vẻ giống như vấn đề xác thực, nhưng tôi không chắc chắn cách móc vào hoặc để gỡ lỗi sự cố. Tôi có thể đi đâu từ đây?

<?php 
function guide_field_widget_info(){ 
    dpm("guide_field_widget_info"); 
    return array(
    'guide_text_textfield' => array(
     'label' => t('test Text field'), 
     'field types' => array('text'), 
     'settings' => array('size' => 60), 
     'behaviors' => array(
     'multiple values' => FIELD_BEHAVIOR_CUSTOM, 
     'default value' => FIELD_BEHAVIOR_DEFAULT, 
    ), 
    ) 
); 
} 


function guide_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) { 
    $field_name = $instance['field_name']; 
    $required = $element['#required']; 
    $item =& $items[$delta]; 


    $element += array(
     '#type' => 'fieldset', 
    '#title' => t('helloooooooo'), 
    ); 
    $required = $element['#required']; 
    $item =& $items[$delta]; 

    $element['nametest'] = array(
     '#title' => t('Name'), 
     '#type' => 'textfield', 
     '#required' => $required, 
     // use #default_value to prepopulate the element 
     // with the current saved value 
     '#default_value' => isset($item['nametest']) ? $item['nametest'] : '', 
    ); 

    $element['checkme'] = array(
     '#title' => t('Check this box or dont'), 
     '#type' => 'checkbox', 
     '#default_value' => isset($item['checkme']) ? $item['checkme'] : '', 
    ); 

//When changing the above code to have a single field, $value is no longer null but will display the first character of the string. I've pasted the code I used to test beloe 
/* 
    $element+= array(
    '#title' => t('Name'), 
    '#type' => 'textfield', 
    '#default_value' => isset($item['nametest']) ? $item['nametest'] : '', 
); 
*/ 

    return $element; 
} 


//hooking this here is required given that after submit, the value is a multidimensional array, whereas the expected value of text is, well, text :-) 

function guide_field_attach_submit($entity_type, $entity, $form, &$form_state){ 
    dpm($form,"guide_field_attach_submit data"); //shows $form[field_test_field][und][0] [value] as being null 
} 
+0

Tiết kiệm ký tự đầu tiên nghe có vẻ như một vấn đề quen thuộc. Xem câu trả lời trước của tôi cho http://stackoverflow.com/questions/6426362/custom-drupal-7-field-only-saves-the-first-character sẽ giúp bạn hay không. – nmc

+0

Không có súc sắc. Tôi đã thử mã được cung cấp và tôi vẫn có kết quả tương tự. – devnill

+0

Bạn có tìm thấy giải pháp cho vấn đề này không? –

Trả lời

2

hook_field_is_empty là bắt buộc và phải được thực hiện như sau:

/** 
    * Implements hook_field_is_empty(). 
    */ 

function MODULENAME_field_is_empty($item, $field) { 
    if ($field['type'] == 'FIELDTYPE') { 
    if (empty($item[$field['type']]['YourField'])) { 
     return (TRUE); 
    } 
    } 
    return (FALSE); 
} 
+2

Nó không phải FIELDNAME_field_is_empty, nhưng HOOK_field_is trống rỗng nơi HOOK là tên của mô-đun của bạn. – sbrattla

Các vấn đề liên quan