Fix for Fix for Creating imagefield content programatically

  1. function sti_migrate_form_submit($form, &$form_state) {
  2.   $form_state = array();
  3.   module_load_include('inc', 'node', 'node.pages');
  4.   module_load_include('inc', 'filefield', 'field_file');
  5.  
  6.   $node = array('type' => 'tell_your_story');
  7.   $form_state['values']['title'] = 'My node';
  8.   $form_state['values']['field_email'][0]['email'] = 'mai@pmail.com';
  9.   $form_state['values']['locations'][0]['postal_code'] = '01060';
  10.   $form_state['values']['field_story_title'][0]['value'] = "My story's title";
  11.  
  12.   // Make sure this file is editable if you want the validators to resize the image
  13.   // Better use the file_directory_temp() path for the starting image...
  14.   $image_path = "/Users/akahn/Desktop/Untitled-1-not-sharp.jpg";
  15.   $imagefield = content_fields('field_story_image', 'tell_your_story');
  16.   $imagefield_validators = imagefield_widget_upload_validators($imagefield);
  17.   if( $image_file = field_file_save_file($image_path, $imagefield_validators, file_directory_path()) ) {
  18.     $form_state['values']['field_story_image'][0] = array('fid' => $image_file['fid']);
  19.   }
  20.  
  21.   $form_state['values']['body'] = 'This is the body text!';
  22.   $form_state['values']['name'] = 'robo-user';
  23.   $form_state['values']['op'] = t('Save');
  24.   drupal_execute('tell_your_story_node_form', $form_state, $node);
  25. }

Submit Fix

Any tags you'd like to associate with your code, delimitered by commas (example: Views, CCK, Module, etc).
Select the syntax highlighting mode to use.