By zetxek on
I have an array where there are some options already loaded (it's a nodereference field that allows referencing only the nodes that are from one content type).
If I print_r($form['field_recurso']); (which is the field containing the select box I want to modify), I get
Array ( [#tree] => 1 [nids] => Array ( [#type] => select [#title] => recurso [#default_value] => Array ( [0] => 0 ) [#multiple] => 0 [#size] => 0 [#options] => Array ( [417] => Canón de vídeo NEC [446] => Canón de vídeo PLUS [447] => Portátil PC [418] => Sala de Xuntas ) [#required] => 1 [#description] => [#post] => Array ( ) [#programmed] => [#tree] => 1 [#parents] => Array ( [0] => field_resource [1] => nids ) [#weight] => 0 [#processed] => [#attributes] => Array ( ) [#input] => 1 [#name] => field_resource[nids] [#id] => edit-field-resource-nids [#value] => Array ( [0] => 0 ) [#sorted] => 1 ) [#weight] => -10 [#post] => Array ( ) [#programmed] => [#parents] => Array ( [0] => field_resource ) [#processed] => [#sorted] => 1 )
I tried several options to change default value, and the value of the array item changes, but it makes no effect.
I tried setting #default_value to a integer (418, 447, the nids of the referenced nodes), to an array (containing the nid and the node title: arrray(418=>Sala de Xuntas), to the node title (Sala de Xuntas)... but I had no luck.
Any clue?
For reference, my whole form looks like this:
<div id="resource"><?php print drupal_render($form['field_resource']); ?></div>
<div id="start"><?php print drupal_render($form['start_date']); ?></div>
<div id="end"><?php print drupal_render($form['end_date']); ?></div>
<div id="description"><?php print drupal_render($form['field_description_0']); ?></div>
<?php
print drupal_render($form['form_token']);
print drupal_render($form['form_id']);
// print drupal_render($form['preview']);
print drupal_render($form['submit']);
print drupal_render($form['delete']);
?>
Comments
Nearly there
The default_value should be an array of the option's keys - so in your above example it should be
or for a single value
Cheers,
Mike
Like books? Check out booktribes the new (Drupal based) community for book lovers
from Computerminds
Mike,
Computerminds offer Drupal development, consulting and training
Thanks for the reply. I
Thanks for the reply.
I tried making
$form['field_resource']['#default_value']=array(418);, but the #default_value didn't got changed. Here is the print_r extract:[#default_value] => Array ( [0] => 0 )I tried too making
$defvalue = array(); $defvalue[] = 418; $form['field_resource']['#default_value']=$defvalue;, but with the same result, the default_value doesn't get changed. It's weird, because if I modify another parameter of the form (#tree or #weigth, for example) they yes get changed, but #default_value does not...I guess I'm having another problem, now setting the value to the form...
Did you sort this one out?
Did you sort this one out? I'm having the same problem except that I am able to set #default_value in the form. The problem is that whatever I set this to has no effect on the dropdown when it is rendered.
Like you I have a select box with the options populated by taxonomy terms, with tids as values and names displayed. I have several of these select boxes in my form and I need to set the default value of each to match the current values in the database.
I have tried:
'#default_value' => $tid
'#default_value' => $name
'#default_value' => array( $tid )
'#default_value' => array( $name )
'#default_value' => array( $tid => $name )
I have also tried setting the value to a literal rather than a variable - nothing works. I have also tried to bypass setting #default_value and attaching the 'selected' attribute directly to the option, but setting attributes on options does not seem to be possible either. Short of re-writing the entire theme engine for select boxes I don't know what I can do.
This is a simple single page form, not a multi-part one. I am using Drupal 5.
Thanks,
Scott
Having the same problems here
Scott - I don't know why, but I can't get this to work either. Your solution would be nice.
I have some VERY similar code on another module which works fine. This one doesn't. I'm in a multi-step form for this one though.
If i turn #multiple to FALSE and then just give one item, it works fine. There seems to be an intermittent bug with this multiple default_value issue.
I am finding that setting
I am finding that setting #tree on the parent fieldset is creating the issue with the default value for the nested radios and checkboxes. Unsetting #tree fixes the issue, but doesn't really solve the problem. Anyone report a bug?
--edit--
I have also found that there is some strange interaction between the weight and tree values. The weight on the fieldset was set to 30. Changing it to 29 or 31 worked, even with tree enabled!