I'm attempting to create a simple text widget but am still getting used to the whole process. When I allow unlimited values for the textfield, the default value disappears whenever I click 'Add another item' (i.e. the edit form appears with two textfields (as per the unlimited setting) with the default value in the first box, but when another field is added, the default value disappears and all three fields are now empty).

Here's the code for my hook_widget() inplementation:

<?php
function search_store_widget(&$form, &$form_state, $field, $items, $delta = 0) {
  return array(
    '#type' => 'textfield',
    '#default_value' => !empty($items[$delta]) ? $items[$delta] : '',
  );
}
?>

What am I doing wrong?

Comments

markus_petrux’s picture

Status: Active » Closed (works as designed)

Default values are not applied during "Add more items" operation. These are only applied to the first item when the node add form is initially build.

If you want to apply default values of all "Add more items" operations, then you have to do it yourself starting from hook_form_alter() and probably scanning the form recursively.

Anonymous’s picture

Status: Closed (works as designed) » Active

Thanks, but I think you misunderstood (or I didn't explain properly)...

The default value is loaded in the first field when the form loads as expected. However, when I add another field using the 'Add another item' button, the default value disappears from the first field where it was previously. It should stay in that first field no matter how many other fields you add.

I tested the link module with unlimited values and a default value and it works as expected (the default value stays in the first field when adding other fields), making me think my module should do this too...

markus_petrux’s picture

Oh, sorry. I misundestood your question.

So if it happens with your field module, how does the code for the FAPI element process callback look?

If you look at text.module, it is text_textfield_process() or text_textarea_process(). Maybe your problem is there?

Anonymous’s picture

The site I'm testing on is at work, but the part I think you're talking about is this:

<?php
function example_widget_info() {
  return array(
    'example_widget' => array(
      'label' => t('Example widget'),
      'field types' => array('example'),
    ),
  );
}
?>

(copied from http://www.lullabot.com/articles/creating-custom-cck-fields)

I didn't set the 'multiple values' or 'callbacks: default value' as I thought core could handle this... Is that what you're referring to?

markus_petrux’s picture

If your module is called "search_store", then you probably need to implement search_store_widget_info(), search_store_widget_settings(), search_store_widget() as in the blog post. Just be sure to follow the descriptions of the example.

I would suggest starting with the example as-is. Install it and see if it works. If it doesn't, then I would post in the blog, and maybe Karen can see it faster that here. If it works, then check out the differences between the example and your module.

Anonymous’s picture

Ok, I worked out that the difference between Karen's code and mine was that she was using $element['value'] = array( in hook_widget(), while I was using $element = array(.

If you use $element['value'] and you don't specify #title, the field just appears by itself with no way of knowing what it is.
If you do specify #title however, multiple field instances each get their own title (as opposed to one title for the group of instances).

Without the ['value'] in $element, the titles appear normally - single fields have a title and multiple instances of a field have one title for the whole group.

So I'm still not sure which way I should be doing it...