Как программно записать картинку в CCK field

Главные вкладки

Аватар пользователя Mirocow Mirocow 9 сентября 2010 в 19:54
        // Формируется картинка
        if(isset($product_data->Картинка)){
          // Занесение картинки в
          $file = new stdClass();
          $file->filename = (string)$product_data->Картинка;
          $path_source = $path .'/'.$file->filename;          
          if(file_exists($path_source)){
            $node->field_image_cache = array();
            $path_destanation = file_create_path().'/'.$file->filename;          
            if(!file_exists($path_destanation) && file_check_directory(dirname($path_destanation), true)){          
              //$path_destanation = file_destination($path_destanation, FILE_EXISTS_REPLACE);          
              file_copy($path_source, $path_destanation, TRUE);
              if($path_source){  
                $file->filepath = $path_destanation;
                $info = image_get_info($path_destanation);
                $file->filemime = $info['mime_type'];
                $file->filesize = $info['file_size'];
                $file->uid = $user->uid;
                $file->status = FILE_STATUS_PERMANENT;
                $file->timestamp = time();
                drupal_write_record('files', $file);
                $_file = array();
                foreach($file as $name=>$option){
                    $_file[$name] = $option;
                }
                $node->field_image_cache[] = $_file;  
              }
            }else{
              $file->filepath = $path_destanation;
              $info = image_get_info($path_destanation);
              $file->filemime = $info['mime_type'];
              $file->filesize = $info['file_size'];
              $file->uid = $user->uid;
              $file->status = FILE_STATUS_PERMANENT;
              $file->timestamp = time();
              drupal_write_record('files', $file);
              $_file = array();
              foreach($file as $name=>$option){
                  $_file[$name] = $option;
              }
              $node->field_image_cache[] = $_file;
            }
            [user=unlink]unlink[/user]($path .'/'.$file->filename);
          }
        }

Комментарии

Аватар пользователя abarmot abarmot 20 сентября 2010 в 9:05

Зачем столько кода?
Вооружаемся drupal_execute, вот так можно сохранить imagefield поле.

<?php
$imagefield 
content_fields($field_name$content_type);
$imagefield_validators imagefield_widget_upload_validators($imagefield);
if (
$image_file field_file_save_file($image_path$imagefield_validatorsfile_directory_path())) {
  
$form_state['values'][$field_name][] = array('fid' => $image_file['fid']);
}
?>