1

apologize every one , my code was correct , I had a mistake in content type .2 similar content type with 2 similar image field :P

but I let it and don't delete this because maybe be useful in futures

I want to save multi image in multi image field programmatically(I set number of value to 5)

Part of my code

        $node = new stdClass(); // Create a new node object
        $node->type = "prodcut_gift_display"; // Or page, or whatever content type you like
        $node->uid = $user->uid;
        $uri = file_build_uri($geturipath);
        $file = new stdClass;
        $file->uid = $user->uid;
        $file->filename = basename($uri);
        $file->uri = $uri;
        $file->filemime = file_get_mimetype($uri);
        $file->filesize = filesize($mainimg_url);
        $file->status = 1;
        file_save($file);
        $node->field_gift_image['und'][0] =(array) $file;
        /***************************************/
        foreach ($upimg_uri_path as $inx=>$upimgtemp){
        unset($file);
        $uri = file_build_uri($upimgtemp);
        $file = new stdClass;
        $file->uid = $user->uid;
        $file->filename = basename($uri);
        $file->uri = $uri;
        $file->filemime = file_get_mimetype($uri);
//        $file->filesize = filesize($mainimg_url);
        $file->status = 1;
        file_save($file);
        $node->field_gift_image['und'][$inx+1]=(array) $file;
        }

        /***************************************/

Then I try to get drupal_json_output , it seems node and image field with 2 value is ok but only one value saved in field.where is the problem?(I check and sure that my image field is set multi and can get 5 value )

update Also I try

$uri = file_build_uri($geturipath);

        $imgs_paths[]=file_create_url($uri);
        /***************************************/
        foreach ($upimg_uri_path as $inx=>$upimgtemp){
        unset($file);
        $uri = file_build_uri($upimgtemp);
        $imgs_paths[]=file_create_url($uri);
        }

   foreach ($imgs_paths as $i=>$img) {
    $file_info = system_retrieve_file($img, 'public://pictures/', TRUE, FILE_EXISTS_REPLACE); //storing image inside picture folder
    if ($file_info->fid) {//if fid exist then image is saved from URL.
      $node->field_gift_image['und'][$i]['fid'] = $file_info->fid; //assign image
    }
  }
        /***************************************/
        $node->field_product['und'][0]['product_id'] = $my_prodcut->product_id;
        $node->title = arg(1) . '-' . rand(1, 999999);
        $node->language = 'fa';
        $node = node_submit($node);
        node_save($node);

Also my field settings enter image description here

enter image description here

enter image description here

4
  • What is this "multi"? Do you mean you're using Multiupload Imagefield Widget?
    – the
    Aug 26, 2014 at 14:01
  • a image field that can save multi value !!!
    – Yuseferi
    Aug 27, 2014 at 5:35
  • You can try without und if your site is not multilingual. But I see, it should work. make node_save($node); $node = node_load($node->nid); dpm($node) . I do not see if it is saved by wrong way. Aug 27, 2014 at 6:57
  • apologize every one , my code was correct , I had a mistake in content type .2 similar content type with 2 similar image field :P
    – Yuseferi
    Aug 27, 2014 at 7:04

2 Answers 2

1
+50

As per my understanding you are trying to create a new article content type with multiple images. Assuming that you have the path of the images. In Drupal 7 you can use the below code


  $node = new stdClass(); // We create a new node object
  $node->type = "article"; // Or any other content type you want
  $node->title = "Article title";
  $node->language = LANGUAGE_NONE; // Or any language code if Locale module is enabled. More on this below *
  //$node->path = array('alias' => 'your node path'); // Setting a node path
  node_object_prepare($node); // Set some default values.
  $node->uid = 1; // admin id
  // Let's add standard body field
  $node->body[$node->language][0]['value'] = 'This is a body text';
  $node->body[$node->language][0]['summary'] = 'Here goes a summary';
  $node->body[$node->language][0]['format'] = 'filtered_html'; // If field has a format, you need to define it. Here we define a default filtered_html format for a body field
  // Add images
  $image_limit = 5;
  $images = array(//images to attach to node
    '0' => 'http://cf.juggle-images.com/matte/white/280x280/drupal-logo-primary.jpg',
    '1' => 'http://cdn.flaticon.com/png/256/37187.png',
    '2' => 'https://www.drupal.org/files/project-images/logo-128x128.jpg',
    '3' => 'http://guillermocerezo.com/sites/default/files/field/image/drupal-logo_0.jpg',
    '4' => 'http://drupalcrafters.org/sites/default/files/DrupalCraft-logo_0.png'
  );
  for ($i = 0; $i < $image_limit; $i++) {
    $file_info = system_retrieve_file($images[$i], 'public://pictures/', TRUE, FILE_EXISTS_REPLACE); //storing image inside picture folder
    if ($file_info->fid) {//if fid exist then image is saved from URL.
      $node->field_multiple_images[LANGUAGE_NONE][$i]['fid'] = $file_info->fid; //assign image
    }
  }
  $node = node_submit($node); // Prepare node for a submit
  node_save($node); // After this call we'll get a nid
  drupal_set_message('article  saved !!!');
  watchdog('info', 'Article node created');
3
  • I try something like your code , but not work, just only one field worked. update question with that
    – Yuseferi
    Aug 27, 2014 at 6:33
  • Wouldn't Entity Metadata Wrappers make it easier? Aug 27, 2014 at 19:44
  • Both Entity Metadata Wrappers and Node Object uses the methods save() and node_save() corresponding to save the node. Both the functions save the node by passing the argument to hook_node_insert().
    – Inizio
    Aug 28, 2014 at 5:13
1

I guess you need check the image field settings. There is set a mode - Single value, change it to unlimited for an example.

4
  • I try it , also set it to 5 limit !!!
    – Yuseferi
    Aug 26, 2014 at 17:01
  • Do you use multilanguage functionality? Aug 27, 2014 at 5:24
  • I also do not see this line at the end of your code - node_save($node); :) Aug 27, 2014 at 5:34
  • It is there :). but my focus on how file saved
    – Yuseferi
    Aug 27, 2014 at 6:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.