0

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;
}
2
  • I don't think this is the answer to your problem, but just a note: is the name of your module zoekproduct or field_zoekproduct? If it's the first one, I believe the name of the function should be zoekproduct_field_widget_form as opposed to field_zoekproduct_field_widget_form.
    – jerdiggity
    Apr 30, 2013 at 2:29
  • I clean up my code as soon as it works ;-) Apr 30, 2013 at 6:39

2 Answers 2

0

One option would be to take advantage of states (see states and/or forms reference).

Here is an example... It's by no means perfect but hopefully it will give you an idea of how states work:

$options_1 = array('Option A', 'Option B', 'Option C');
$options_2 = array('Option D', 'Option E', 'Option F');
$element['overzicht_producten'][] = array(
  '#type' => 'select',
  '#title' => t('Overzicht producten'),
  '#options' => $options_1,
  '#states' => array(
    'visible' => array(
      // To make this select menu visible, type "Visible1" (no quotes) into the "searchfield" box.
      ':input[name="searchfield"]' => array('value' => 'Visible1'),
    ),
  ),
);
$element['overzicht_producten'][] = array(
  '#type' => 'select',
  '#title' => t('Overzicht producten'),
  '#options' => $options_2,
  '#states' => array(
    'visible' => array(
      // To make this select menu visible, type "Visible2" (no quotes) into the "searchfield" box.
      ':input[name="searchfield"]' => array('value' => 'Visible2'),
    ),
  ),
);

Hope that helps... :)

0

So after a loooong puzzle .. I found the answer

You can get the values during creation with doing the following:

$selected =  ($form_state['values']['field_zoekproduct']['und']['0']['searchfield'];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.