Changing file upload path from on_before_validate or on_before_save hooks

edited June 2015 in Installation
Hi All

I am accepting multiple file types to one of my models as well as requesting the user to select from a list the category the file should be associated with. Based on the category that the user selects I need the upload path for the file to differ. Inside the form fields method I don't know what the user has/will select and therefore have to default the upload path. Is there a way to change the upload path of a file element from the on_before_validate or on_before_save hooks?

Thanks
Tim.

Comments

  • edited 6:08AM
    You can try setting a placeholder value for the upload_path or folder value. The placeholder should be surrounded by "{" and "}" and should contain a value that your model returns using the "find_one_array" method. The following will upload the file to the assets/images/{category_id} where {category_id} is the current record your editing's value:
    $fields['my_file'] = array('type' => 'file', 'folder' => 'images/{category_id}/');
  • edited 6:08AM
    Thanks David (I think that is your name). I got it working using the placeholder concept. I have one outstanding issue now though as a result of changing my code to use the placeholder. The display preview doesn't work on the edit view because it hasn't replaced the placeholder with the value for the preview URL. I am setting the placeholder in the form_fields method exactly like you documented above, and then replace the value in the on_before_post hook method like:

    $_POST['category_id'] = $_POST['item_type'];

    Would you be able to help me on this?

    Thanks T.
  • edited 6:08AM
    When you say the display preview, are you referring to the "View" button at the top in the admin? IF so, you can create a "preview_path" method on your model. It get's passed an array of the data values for that particular record and the preview_path parameter if specified in your MY_fuel_modules.php file.
  • edited 6:08AM
    Sorry for the confusion, I actually mean the display preview of the individual file element i.e. this code in the form_fields method:

    $upload_path = assets_server_path('/{category_id}/', 'media'); --Note the placeholder here

    $fields['item_file_name'] = array('type' => 'file', 'upload_path' => $upload_path, 'overwrite' => TRUE, 'display_overwrite' => TRUE, 'display_preview' => TRUE, 'multiple' => FALSE);


    When I go to the edit screen the image preview tries to display the image with this URL:
    assets/media/%7Bcategory_id%7D//Screen_Shot_2015-04-20_at_210340.png --Note the %7Bcategory_id%7D part should have been replaced with the category.
  • edited 6:08AM
    There is a "replace_values" parameter you can pass to the field. Often times it is just the $values array but be careful with that because on a new record, the $values array is empty.
    $fields['item_file_name'] = array('type' => 'file', 'upload_path' => $upload_path, 'overwrite' => TRUE, 'display_overwrite' => TRUE, 'display_preview' => TRUE, 'multiple' => FALSE, 'replace_values' => $values);
    Also, what version are you using. We just rolled out 1.3 this week and it had several improvements in that arena and may fix your problem.
  • edited 6:08AM
    Thanks David.. upgrade to 1.3 and replace_values did the trick!
Sign In or Register to comment.