0

Apologies in advance, this question probably has an obvious answer.

I am using a custom form with a 'managed_file' widget named 'image' to upload image files to the public file directory:

$image = file_load($form_state['values']['image']);  
$image->status = FILE_STATUS_PERMANENT;
file_save($image);

I am using File Entity Module to attach to image file entities a caption text field (the machine readable name is 'field_caption'). On the custom file upload form there is a 'textfield' widget named 'caption' whose submitted value I want to store as the value of the image file entity's 'field_caption'.

Certainly, Drupal needs to first realize that the uploaded file is in fact an image file, as other file entites might not have attached a caption text field. That's why in the code above I first save the file before the code is followed by

if (isset($form_state['values']['caption'])) {  
$caption = $form_state['values']['caption'];
$image->field_caption[0]['value'] = $caption;  
}
file_save($image);

However, after submission the caption text field is always left empty.

Did I choose the wrong commands to store a value in field_caption?

0

I posted this on drupal.org (https://drupal.org/node/1820706) and got an answer that works. I have to replace

$image->field_caption[0]['value'] = $caption;
with
$image->field_caption[LANGUAGE_NONE][0]['value'] = $caption;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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