1

i'd like to dynamically change the option labels of a cck form field in the edit node form.

i allready found ou, that i can't do this in hook_form_alter, since the cck fields aren't in the form object at this time. so i added a after_build function. in this function, i'm trying to set the option labels like this:

function _mymodule_after_build($form, &$form_state) {
   $form['field_product_alternativtitle']['value']['#options'][1] = "foo";
   $form['field_product_alternativtitle']['value']['#options'][2] = "bar";
   $form['field_product_alternativtitle']['value']['#options'][3] = "meh";  
   return $form;
}

This, however doesn't change the output of the form in any way. I guess this is logical, since my function gets calles AFTER the BUILD of the form.

so my question is, how can i achieve my goal?

3

I would check a couple of things.

  1. For sanity, make sure that your after_build function is being called by adding a drupal_set_message().

  2. The labels will only change in the Edit form if your values are literally '1', '2' and '3'. Once you save the form, however, drupal will save the original value, not your new label.

    function mymodule_after_build($form, &$form_state) {
      $form['field_some_options']['value']['#options'][1] = 'hiyo';
      return $form;
    }

Select list with dynamic label applied at run time

0

I believe that you also have to set the default value. if your trace out the entire array

 drupal_set_message('Options'.'<pre>'. print_r( $form['field_some_options'], TRUE) .'</pre>');

you will see a parameter called default_value. I believe that you have to set that to your desired option. In my experience, the best thing to do is add a validation function by adding this

function yourmodule_form_validate($form, &$form_state) {
    }

and also adding this

$form['#validate'][] = 'yourmodule_form_validate';

to your hook function

then trace out the array in your validate function as well. This way you can compare the state of the array when the form builds, and after it is submitted to exactly what is changed, you can then st those value in code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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