Hello, I am sure this is a stupid question, however I am having an issue. What I am making is a small simple ubercart attribute option image module. However I have not created a file upload module yet so what I thought would be a very trivial task does not seem to be working!

file_save_upload(); Is always resulting in a failure, the file's temp $source corresponds with the the file input name attribute does it not? Thanks in advance, I looked at both the filefield module and upload module but they are very messy so I was getting a bit lost

<?php

/**
* @file
* Provides image upload fields for attribute options.
* @author Tj Holowaychuk
*/

/* -----------------------------------------------------------------

Hook Implementations

------------------------------------------------------------------ */

/**
* Implementation of hook_form_alter();
*/
function uc_option_image_form_alter($form_id, &$form){
switch($form_id){
case 'uc_object_options_form':
if ($aids = element_children($form['prod_attr'])){
foreach($aids AS $aid){
if ($oids = element_children($form['prod_attr'][$aid]['options'])){
foreach($oids AS $oid){
$form['prod_attr'][$aid]['options'][$oid]['option_image_' . $aid . '_' . $oid] = array(
'#type' => 'file',
'#title' => t('Image'),
'#name' => 'option_image_' . $aid . '_' . $oid,
'#size' => 8,
'#default_value' => '',
);
}
}
}

$form['#submit']['uc_option_image_uc_object_options_form'] = array();
}
break;
}
}

/* -----------------------------------------------------------------

Form Handling

------------------------------------------------------------------ */

/**
* Handle uc_object_options_form submit.
*/
function uc_option_image_uc_object_options_form($form_id, $form_values) {
if ($aids = element_children($form_values['prod_attr'])){
foreach($aids AS $aid){
if ($oids = element_children($form_values['prod_attr'][$aid]['options'])){
foreach($oids AS $oid){
if ($file = file_save_upload('option_image_' . $aid . '_' . $oid, 'option-images')){
// @todo: not uploading?
// dpm($file);
}
else {
drupal_set_message(t('Failed to upload image.'), 'error');
watchdog('uc_option_image', t('Upload Failed.'), WATCHDOG_ERROR);
}
}
}
}
}
}

Comments

tjholowaychuk’s picture

  
/**
* @file 
* Provides image upload fields for attribute options.
* @author Tj Holowaychuk <http://www.350designs.com/>
*/

/* -----------------------------------------------------------------

  Hook Implementations 

------------------------------------------------------------------ */

/**
* Implementation of hook_form_alter();
*/
function uc_option_image_form_alter($form_id, &$form){  
  switch($form_id){
    case 'uc_object_options_form':   
      if ($aids = element_children($form['prod_attr'])){  
        foreach($aids AS $aid){ 
          if ($oids = element_children($form['prod_attr'][$aid]['options'])){ 
            foreach($oids AS $oid){
              $form['prod_attr'][$aid]['options'][$oid]['option_image_' . $aid . '_' . $oid] = array(
                  '#type' => 'file',
                  '#title' => t('Image'),
                  '#name' => 'option_image_' . $aid . '_' . $oid,
                  '#size' => 8,
                  '#default_value' => '',
                );         
            }
          } 
        } 
        
        $form['#submit']['uc_option_image_uc_object_options_form'] = array(); 
      }
      break;
      
    case 'uc_attribute_admin_settings':
      $form['uc_option_image_js'] = array(
          '#type' => 'checkbox',
          '#title' => t('Switch Images'),
          '#description' => t('Use JavaScript to switch attribute option images when selected from a select field if it is available.'),
          '#weight' => 1,
          '#default_value' => variable_get('uc_option_image_js', TRUE),
        );
                                
      $form['buttons']['#weight'] = 5;
      break;
  }
}

/* -----------------------------------------------------------------

  Form Handling 

------------------------------------------------------------------ */

/**
* Handle uc_object_options_form submit.
*/
function uc_option_image_uc_object_options_form($form_id, $form_values) {
  if ($aids = element_children($form_values['prod_attr'])){ 
    foreach($aids AS $aid){ 
      if ($oids = element_children($form_values['prod_attr'][$aid]['options'])){ 
        foreach($oids AS $oid){
          if ($file = file_save_upload('option_image_' . $aid . '_' . $oid, 'option-images')){
            // @todo: not uploading?
            // dpm($file);
          } 
          else {
            drupal_set_message(t('Failed to upload image.'), 'error');
            watchdog('uc_option_image', t('Upload Failed.'), WATCHDOG_ERROR);
          }   
        }                                                             
      } 
    } 
  }    
}

____________________________________________________
Tj Holowaychuk

Vision Media
350designs
Design Inspiration

tjholowaychuk’s picture

Hmm I just did some quick debugging and tested against a filefield vs my module. The filefield of course populates the $_FILES superglobal properly, however my file fields do not even seem to upload the image to the tmp directory as it does not appear in $_FILES at all...
____________________________________________________
Tj Holowaychuk

Vision Media
350designs
Design Inspiration

tjholowaychuk’s picture

hahah... soo... go self..

$form['#attributes'] = array('enctype' => 'multipart/form-data');

I am getting to used to these days of automating markup, I just kind of assumed that when you have a file field Drupal would add the enctype attribute.. silly me...

____________________________________________________
Tj Holowaychuk

Vision Media
350designs
Design Inspiration