0

I have a form that works great for adding fields, including and image, to a node and saving it.

I want to allow users to edit their fields and change the data, including their image. I figure there is some way to retrieve and display the current image and then allow the user to upload and save/replace the image with a new one. I can't figure out how to show a preview of the existing image on the Edit form. I assume somewhere I should use File_Exists_Replace, but the rest is an overwhelming mystery.

Here is the code I use for the original form:

  $form['logo'] = array(
  '#title' => t('Image'),
  '#type' => 'managed_file',
  '#description' => t('Only image files will be allowed.'),
  '#default_value' => variable_get('logo', ''),
  '#upload_location' => 'public://misc/media',
  '#upload_validators'  => array(
    'file_validate_extensions' => array('png gif jpg jpeg'),
    'file_validate_size' => array(2*1024*1024),
    )
  );

And the submit function:

    // Load the file via file.fid.
    $file = file_load($form_state['values']['logo']);
    // Change status to permanent.
    $file->status = FILE_STATUS_PERMANENT;
    // Save.
    file_save($file);
    // Record that the module (in this example, cformtwo module) is using the file. 
    file_usage_add($file, 'cformtwo', 'node', 1);  
...
    node_submit($node);
    node_save($node);

I have spent days trying to figure out how to do this.

1

Your 'logo' variable will be a file id, referring to a row in the file_managed table, so you can use the file_load() function to load the file object, create an image with the details, and attach it to your form field.

This is a (very) basic example:

$logo = variable_get('logo');

$image = '';
if ($logo) {
  // Load the file object
  $file = file_load($logo);

  // Get a thumbnail of the image (just an example)
  $args = array('style_name' => 'thumbnail', 'path' => $file->uri, 'alt' => 'Alt text');
  $image = theme('image_style', $args);
}

// Prepend it to your field
$form['logo'] = array(
  '#title' => t('Image'),
  '#type' => 'managed_file',
  '#description' => t('Only image files will be allowed.'),
  '#default_value' => $logo,
  '#upload_location' => 'public://misc/media',
  '#upload_validators'  => array(
    'file_validate_extensions' => array('png gif jpg jpeg'),
    'file_validate_size' => array(2*1024*1024),
  ),
  '#prefix' => $image
);
|improve this answer|||||
  • Thanks for the quick response! A few questions: 1) Does all this code go in my form function? 2) Do I need to pass $logo as an argument of some sort so that $logo = variable_get('logo'); has something to work with? – user9685 Oct 19 '12 at 18:00
  • Yep that all goes in the form function. Sorry, I assumed since you were using variable_get you already had the variable saved somewhere. If not, all you need to is call variable_set('logo', $file->fid); in your submit function (after the file is loaded). That will persist it to the database and it can get picked up again in variable_get in your form function. It might be a good idea to prefix the variable name with something (e.g. mymodule_logo), just to avoid potential conflicts with other modules setting variables – Clive Oct 19 '12 at 18:40
  • I actually found the fid in the $node ie. $logo = $node->field_logo[$node->language][0]['fid']; Seems to be working except the image is showing up as a file name, not the image. I will start my research on that. Thanks again for your help! – user9685 Oct 19 '12 at 18:56

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.