1
$values = $form_state['values'];
    $images = array();
    foreach ($values as $k => $val)
    if (strpos($k, 'images') !== false && $val !== 0) {
        $images[$k] = $val;
        unset($values[$k]);
    } elseif (strpos($k, 'images') !== false)
        unset($values[$k]);   
    $node = new stdClass();
$node->language = LANGUAGE_NONE;
$node->promote = 0;
$node->uid = 1;
$node->type = 'beaches';
    $node->title = $values['location_name'];
    $node->field_location[$node->language][0]['value']=$values['location'];
    $node->field_location_name[$node->language][0]['value']=$values['location_name'];
    $node->field_beaches_continent[$node->language][0]['value']=$values['continent'];
    $node->field_country[$node->language][0]['value']=$values['country'];
    $node->field_about_location[$node->language][0]['value']=$values['description_location'];
    $node->field_travelling_location[$node->language][0]['value']=$values['travelling_details'];
    $node->field_location_facilities[$node->language][0]['value']=$values['location_facilities'];
    $node->field_beach_tag[$node->language][0]['value']=$values['tags'];
    $node->field_longtitude[$node->language][0]['value']=$values['longtitude'];
    $node->field_latitude[$node->language][0]['value']=$values['latitude'];
    foreach ($images as $image) {
    $file = file_load($image);
    $file->status = FILE_STATUS_PERMANENT;
    $node->field_beaches_images[$node->language][0]['value']=$file->fid;
    file_save($file);
    }
    node_object_prepare($node);
node_save($node);
return $node;

edit submit action:-

global $user;
$values = $form_state['values'];    
$updatenode=node_load($values['beach_id']);

unset($values['submit']);
unset($values['form_build_id']);
unset($values['form_token']);
unset($values['form_id']);
unset($values['op']);

$clid = $values['beach_id'];
unset($values['beach_id']);;
$images = array();
foreach ($values as $k => $val){
    if (strpos($k, 'images') !== false && $val !== 0) {
        $images[$k] = $val;
        unset($values[$k]);
    } elseif (strpos($k, 'images') !== false)
        unset($values[$k]);
}
   if($updatenode->nid==$clid  ){
    $updatenode->title = $values['location_name'];
    $updatenode->field_location[$updatenode->language][0]['value']=$values['location'];
    $updatenode->field_location_name[$updatenode->language][0]['value']=$values['location_name'];
    $updatenode->field_beaches_continent[$updatenode->language][0]['value']=$values['continent'];
    $updatenode->field_country[$updatenode->language][0]['value']=$values['country'];
    $updatenode->field_about_location[$updatenode->language][0]['value']=$values['description_location'];
    $updatenode->field_travelling_location[$updatenode->language][0]['value']=$values['travelling_details'];
    $updatenode->field_location_facilities[$updatenode->language][0]['value']=$values['location_facilities'];
    $updatenode->field_beach_tag[$updatenode->language][0]['value']=$values['tags'];
    $updatenode->field_longtitude[$updatenode->language][0]['value']=$values['longtitude'];
    $updatenode->field_latitude[$updatenode->language][0]['value']=$values['latitude'];        
    foreach ($images as $image) {
    $file = file_load($image);
    $file->status = FILE_STATUS_PERMANENT;
    $updatenode->field_beaches_images[$updatenode->language][]['value'] = $file->fid;
    file_save($file);
    }
    node_object_prepare($updatenode);
node_save($updatenode);
   }        
$form_state['redirect'] = 'beaches/' . $clid;

I want to store multiple images fid in the field "field_beaches_images[$node->language][0]['value']" I have ten images, one image is mandatory user can upload up to 9 images. The problem is if any of the image field is empty, it does not store the other values,the node itself not creating. How can we achieve this (ie) the user can insert 1,2,...9 images. Assume if the user enters only 4 images, then the node itself is not created and generating error.

I'm facing a issue on this. I can able to add the images, but it does not supports updation. I have added the code used for editing the form.

1 Answer 1

1

For background read Why is hook_form_alter() so messy?, but essentially you want something like

foreach ($images as $image) {
  $file = file_load($image);
  $file->status = FILE_STATUS_PERMANENT;
  $node->field_beaches_images[$node->language][]['value'] = $file->fid;
  file_save($file);
}
2
  • Now, all the values are storing in array. I have given the image upload field as text field, should I change that??. Because the image values are not saving in the node...
    – Ajmal
    Aug 2, 2013 at 12:57
  • Oops sorry, I though you'd made a mistake using value instead of fid...I was trying to be clever and failed! I've updated the code in the answer to match what you were originally doing
    – Clive
    Aug 2, 2013 at 12:58

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.