2012-09-14 11 views
6

Estoy tratando de crear un widget personalizado, pero cuando lo envío, Drupal no parece guardar ningún dato. Cuando utilizo hook_field_attach_submit() para mostrar los datos que he pegado, aparece como nulo.drupal widget de campo no guarda los datos enviados

Extrañamente, si cambio el #type para que sea un solo campo de texto en lugar de un conjunto de campos, solo guardará el primer carácter de la cadena que se haya ingresado.

Esto parece un problema de validación, pero no estoy seguro de cómo conectarlo o solucionar el problema. ¿A dónde puedo ir desde aquí?

<?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

Salvar el primer carácter suena como un problema familiar. Vea si mi respuesta anterior para http://stackoverflow.com/questions/6426362/custom-drupal-7-field-only-saves-the-first-character le ayudará. – nmc

+0

Sin dados. Intenté con el código proporcionado y aún obtuve el mismo resultado. – devnill

+0

¿Encontró una solución a este problema? –

Respuesta

2

hook_field_is_empty es obligatorio y tiene que ser igual que implementar siguiente:

/** 
    * 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

No es FIELDNAME_field_is_empty, pero HOOK_field_está vacío donde HOOK es el nombre de su módulo. – sbrattla

Cuestiones relacionadas