Drupal 6 Programmatically Adding Images to FileField

How to programmatically add an image to a file field? I have the url / filepath of the image that I want to upload. I tried the following:

$newNode->field_profile_image[0]['value'] = 'http://www.mysite.com/default.gif';

But that does not work.

I also tried:

$ newNode-> field_profile_image [0] ['value'] = 'sites / default / files / default.gif';

The file does not have to be external to webiste. I am glad that I have a site on this site.

+3
source share
5 answers

You may have to use hook_nodeapi to set this correctly. You will want to change it in the "Paste" section. Make sure you save the node again after adding the required fields.

Drupal , URL- . , , , Brightcove 176, brightcove_remote_image, .

, node. :

////// in NodeAPI /////
    case "insert":
        $node->field_image[0] = _mymod_create_filearray($image_url);
        node_save($node);

, .

///// mymod_create_filearray /////
function _mymod_create_filearray($remote_url){
  if ($file_temp = brightcove_remote_image($remote_url)) {
    $file = new stdClass();
    $file->filename = basename($file_temp);
    $file->filepath = $file_temp;
    $file->filemime = file_get_mimetype($file_temp);
    $file->filesize = filesize($file_temp);

    $file->uid = $uid;
    $file->status = FILE_STATUS_PERMANENT;
    $file->timestamp = time();
    drupal_write_record('files', $file);

    $file = array(
     'fid' => $file->fid,
      'title' => basename($file->filename),
      'filename' => $file->filename,
      'filepath' => $file->filepath,
      'filesize' => $file->filesize,
      'mimetype' => $mime,
      'description' => basename($file->filename),
      'list' => 1,
    );
    return $file;
  }
  else {
    return array();
  }
}

. , .

+3

, , , ( ).

- field_file_save_file() ( hook_nodeapi, presave), ( , jacobangel '_mymod_create_filearray()' , , ).

, - ( /tmp ) "" Drupal, .. URL-, /.

, , Remote File module , - , .

+2

nodeapi, , , . , "" drupal, , . nodeapi , node , , ..

, feeds, . , , .

+1

What you try will not work. Drupal does not have the ability to process deleted files without using a module. AFAIK there is no module that offers an API for downloading deleted files.

0
source

Here is a quick example taken from one of my projects.

  $node = new stdClass;
  $node->title = 'Example Callout';
  $node->type = 'wn_hp_callout';
  // Search examples directory to attach some images.
  $callouts_dir = drupal_get_path('module', 'wn_hp_callout').'/imgs/examples/';
  $callout_imgs = glob($callouts_dir.'*.{jpg,jpeg,png,gif}',GLOB_BRACE);
  // Now add the images and provide imagefield extended additional text.
  foreach($callout_imgs as $img) {
    $img_info = pathinfo($img);
    $field = field_file_save_file($img, array(), file_directory_path() .'/example_callouts/');
    if( !isset($field['data']) ) {
      $field['data'] = array();
    }
    $field['data']['title'] = ucwords(str_replace('_',' ',$img_info['filename']));
    $field['data']['alt'] = 'This is alt text.';
    $node->field_wn_hp_callout_image[] = $field;
   }
   $node = node_submit($node);
   node_save($node);
0
source

Source: https://habr.com/ru/post/1787674/


All Articles