Originally I had been using a custom form rather than form_alter, but I had issues of compatibility with CCK.
I have the following code I'm working from:
<?php
function test1_form_alter(&$form, $form_state, $form_id) {
switch ($form_id) {
case 'kg_media_type_node_form':
$form['field_kg_url'][0]['#weight']= -5;
$form['field_kg_url'][0]['#default_value'] = $form_state['values']['field_kg_url'][0]['value'];
$form['field_kg_title']['#size'] = 30;
$form['field_kg_title']['#weight'] = -4;
$form['field_kg_title']['#default_value'] = $form_state['values']['field_kg_title'];
$form['title']['#required'] = false;
$form['populate'] = array(
'#type' => 'submit',
'#value' => 'Retrieve info',
'#validate' => array('test1_populate'),
'#weight' => -4,
);
break;
}
}
//---------------------------------------
function test1_populate($form, &$form_state) {
$kg_url = $form_state['values']['field_kg_url'][0]['value'];
$kg_page = get_kg_page($kg_url);
$kgInfo = scrapeKGInfo($kg_page);
form_set_value($form['field_kg_title'][0]['value'], $kgInfo['title'], $form_state);
form_set_value($form['field_kg_url'][0]['value'], $kgInfo['kg_url'], $form_state);
//previously before using a CCK type the following worked fine:
//form_set_value($form['field_kg_url'], $kgInfo['kg_url'], $form_state);
$form_state['rebuild'] = TRUE;
drupal_set_message(t('KG info retrieved'));
}?>
What I'm trying to do is have a CCK type, add a button which when pressed, takes the value of a field (URL) and after doing some logic, populates the other fields using the data scraped from a page defined by the URL. So I need to do two things which I'm not able to do with CCK type fields:
- set default value
- set the value.
Are there functions I should be calling to set/get the values of CCK type fields?
Additionally once that is solved, for fields which can have multiple values (ie tags) how can I set such a field to have a multiple number of default values?
As far as title field goes, i was a) testing non cck fields (it works fine to change its attributes) and b) later on i'm going to set the title based on retrieved information....
Thanks!
Daz
Comments
Comment #1
dazmcg commentedforgot to mention that by executing lines like
$form['field_kg_url'][0]['#weight']= -5;It then causes the element to drop of the form entirely - I don't really understand why this is the case either, but an understanding of this would probably help with being able to edit/set the elements' attributes!thanks!
Comment #2
karens commentedSee #336355: Unable to add '#attributes' or use '#disabled' = true via hook_form_alter() for some general info about how to alter elements in CCK.