I think I am about to lose my mind .. how in earths name do I get the value of a field in a widget during creation? I created a custom field widget, and during creation of new content I want to be able to do the following:
I want to create a custom field that I can attach to any content type. This custom field has two elements, a textfield to search for products in MySQL. And a selectbox. The selextbox should get filled with options that match the letters that the user types in the textfield.
I checked field_get_items and a lot more, but I just cannot get it. The code below has elements from a form that I made, so please just overlook those parts. I used DPM($something); everywhere trying to see if I can see the value of $element['searchfield'] anywhere ..
$items = field_get_items('node', $node, 'searchfield'); .. doesnt work, but from what I understand this is only to be used after the node is created .. ie to retrieve the added values, I NEED the values during runtime ...
/**
* Implements hook_field_widget_form().
*/
function field_zoekproduct_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
switch ($instance['widget']['type']) {
case 'field_zoekproduct_field' :
$element['searchfield'] = array(
'#title' => t('Zoek naar een product'),
'#type' => 'textfield',
'#maxlength' => 115,
'#size' => 55,
'#weight' => 0,
'#default_value' => 'zoek...',
'#attributes' => array('onfocus' => "this.value = '';"), //when textbox gets focus the default value will be removed
'#ajax' => array(
'event' => 'change',
'callback' => 'zoekproduct_zoek_getypte_text',
'keypress' => true,
//'effect' => 'fade',
'wrapper' => 'dropdown-second-replace',
),
);
// with the piece of code below in a form module I can get the value of the textfield in $selected and therefor am able to fill my select with options from the database that match $selected .. now How do I do the same with a custom field????
$options_first = zoekproduct_maak_een_eerste_selectie();
$selected = isset($form_state['values']['searchfield']) ? $form_state['values']['searchfield'] : key($options_first);
$element['overzicht_producten'] = array(
'#type' => 'select',
'#title' => t('Overzicht producten'),
'#prefix' => '<div id="dropdown-second-replace">',
'#suffix' => '</div>',
'#size'=>1,
'#validated' => TRUE,
'#options' => zoekproduct_zoek_naar_getypte_text($selected),
'#default_value' => isset($item['overzicht_producten']) ? $item['overzicht_producten'] : '',
'#ajax' => array(
'event' => 'change',
'callback' => 'zoekproductfield_selecteer_product',
'wrapper' => 'find_product',
),
);
break;
}
return $element;
}
zoekproduct_field_widget_form
as opposed tofield_zoekproduct_field_widget_form
.