0

Problem

I have a custom form which creates a node. It's based off this link and uses the entity metadata wrapper to create the node.

The node created has an image field and you can use the form to upload an image on creation.

How can I set an upload directory for the image that's uploaded? At the moment they just default to my public folder.

Set-Up

There's more to this form, but these are the important parts. I declare a file field.

function postajob_form($form, &$form_state){
   ....
   $form['file'] = array(
     '#type' => 'file',
     '#title' => t('Thumbnail/Advert Picture.'),
     '#size' => 5,
   );
 }

I then submit that file using an entity metadata wrapper.

function postajob_form_submit($form, &$form_state) {

  global $user;

  $values = array(
    'type' => 'job_advert',
    'uid' => $user->uid,
    'status' => 1,
    'comment' => 1,
    'promote' => 0,
  );
  $entity = entity_create('node', $values);

  $ewrapper = entity_metadata_wrapper('node', $entity);

  if (array_key_exists('storage', $form_state)) {
      //if is required incase no image is uploaded.
      $ewrapper->field_req_thumbnail_school->file = $form_state['storage']['file'];
      //field is unset now it's no longer needed.
      unset($form_state['storage']['file']);
   }
}

I tried changing the URI inside the form_state, however this had no effect.

 if (array_key_exists('storage', $form_state)) {
      //altering the uri where the files are uploaded too 
      $imagefilename = $form_state['storage']['file']->filename;
      $form_state['storage']['file']->uri = "public://user-upload/job-advert/$imagefilename";
      //save the file
      $ewrapper->field_req_thumbnail_school->file = $form_state['storage']['file'];

   }

But this didn't effect anything.

2 Answers 2

1

Whenever I use a file field in the submit handler I use:

$validators = array('file_validate_extensions' => array('csv'));
if ($file = file_save_upload('upload', $validators)) {

You can add an optional argument to file_save_upload after $validators which is $destination, take a look at https://api.drupal.org/api/drupal/includes!file.inc/function/file_save_upload/7 for more details

1
  • Cheers. I didn't end up doing it quite like that, but it was pretty similar. There's a good example in the form example that I'd actually used and then forgotten about... Oct 30, 2013 at 16:43
0

Clearly was not having a good day when I posted this question. I already had the code I just needed to change the path.

Taken straight from one of the form examples the following piece of code goes into the form validation function. It validates the file (this shows image validation) and then moves it to the desired path.

function mymodule_form_validate($form, &$form_state) {

   //I upload the file with file_save upload
    $file = file_save_upload('file', array(
      'file_validate_is_image' => array(), // Validates file is really an image.
      'file_validate_extensions' => array('png gif jpg jpeg'), // Validate extensions.
    ));
    // If the file passed validation:
    if ($file) {
      // Move the file, into the Drupal file system
      // This is where you set the path.
      drupal_mkdir("public://user-upload/job-advert/$usermail");
      if ($file = file_move($file, "public:///user-upload/job-advert/$usermail")) {
        // Save the file for use in the submit handler.
        $form_state['storage']['file'] = $file;
      }

    }
  }

Your Answer

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

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