I am writing a module whereby I use form_alter to generate a custom form for a CCK type. I am trying to figure out how to change CCK field attributes for the form - say using form_alter. There's a post that describes how to play around with the #disabled attribute (http://drupal.org/node/336355 and http://drupal.org/node/300705#comment-985105), but I cannot use it to alter any other attributes.

In particular I'm trying to work out how to set #default_value - so that I can have a form, linked to a CCK type, with a field that takes a URL, a 'lookup' button which has an action associated with it whereby it goes off to the URL, scrapes information and then populates various fields on the form - by setting the default values of the fields and then sets form_rebuild to true. I can do this with the non-cck fields, but have gotten stuck on how to do this for CCK fields....

After many many hours going around in circles, help would be appreciated ALOT!!

thanks

Comments

dazmcg’s picture

Here's some of the code I'm currently working with:

<?php
function test1_form_alter(&$form, &$form_state, $form_id) {

  if (isset($form['type']) && isset($form['#node'])&&($form_id == 'kg_media_type_node_form')){
    $form['#pre_render'][] = 'my_process_function';  	
  }
}

function my_process_function($form,&$form_state) {
  
  //$form['field_kg_url']['#default_value'] = "well";
  print_r(array_keys($form['field_kg_url']));
  print_r($form['field_kg_url']);
  $form['field_kg_url'][0]['#default_value']['value'] = "well1";
  $form['field_kg_url'][0]['value']['#default_value'] = "well2";
  $form['field_kg_url'][0]['#value']['value'] = "well3";  
  print_r($form['field_kg_url']);
  print_r(array_keys($form_state));
  print_r($form_state);
  
  $form_state['rebuild'] = TRUE;
  echo "\n------------------\n";
  return $form;
?>

I also get these drupal error messages, but i think that's a separate issue:


    * warning: Missing argument 2 for my_process_function(), called in /home/web/active/drupal-6.10/includes/common.inc on line 2820 and defined in /home/web/active/drupal-6.10/sites/all/modules/making/test1/test1.module on line 46.
    * warning: Missing argument 2 for my_process_function(), called in /home/web/active/drupal-6.10/includes/common.inc on line 2820 and defined in /home/web/active/drupal-6.10/sites/all/modules/making/test1/test1.module on line 46.

and the field_kg_url doesn't have any default value when I load the form.....I tried setting all the places in the element I could see a value being held.....also form_state seems to be empty? is that ok?

help much obliged!!!!!!

markus_petrux’s picture

Alter #default_value directly from hook_form_alter() as in this example, should work. Also, make sure your module is loaded after CCK and maybe also after fielgroup by altering the weight of your module in the {system} table.

Use print_r($form['field_myfield']); to figure out the structure of the form element that you wish to modify. Not all fields have the same format.

Doubt is the beginning, not the end of wisdom.

markus_petrux’s picture

If you ever need to use #pre_render, be sure to use the correct prototype.

// This is invoked from drupal_render() in includes/common.inc
// drupal_render() is used by FAPI to render forms, but also by node module to render node view.
function mymodule_pre_render($elements) {
  // Do what you need here to alter the structure is about to be rendered.
  return $elements;
}

http://api.drupal.org/api/function/drupal_render/6

Doubt is the beginning, not the end of wisdom.