It looks like you're new here. If you want to get involved, click one of these buttons!
when I call form_builder from a controller (mine), I get
$upload_path = assets_server_path('images/plants', 'images');
$fields['photo_image'] = array('type' => 'file', 'upload_path' => $upload_path, 'overwrite' => TRUE);
which is what I want, but it clashes with the documentation which implies that the "images" part ought to get
I then go to edit directly from the dashboard, same model, and I get
value="/home4/fnpsdevo/public_html/assets/images/images/plants"
Comments
If your model has a field ending with "_image" or "_img" then the base_module_model::form_fields() method (which is what your model most likely inherits from), will automatically create the "... OR upload an image" file upload field with the name of "photo_image_upload" in your case. If you do a "print_r($fields);" write after you call the parent::form_fields($values, $related);, you should see it as one of the fields returned. To get rid of it, you can do:
unset($fields['photo_image_upload']);
Or if you want to modify say the upload path, you can do:
$fields['photo_image_upload']['upload_path'] = assets_server_path('plants', 'images');
Also, you should be able to use the "code" tag to print HTML in your comments.
No issue with the admin version having both upload and find something already uploaded
the issue is more like --
if I use forum builder directly, the resulting form sets the upload path to one thing
if I use if from the admin, it tries to upload to a different place
It also looks like the upload does not go to where the documentation says it will go if form-builder is used directly
$upload_path = assets_server_path('images/plants', 'images');
if I understand correctly, then for the above, the image ought to land in
'assets/images/images/plants' regardless of who throws the form
It doesn't -- If I use form_builder -- it lands in
'assets/images/plants'
If admin does it, it lands in
'assets/images'
Neither appears to be correct. Same model is being used. This is purely a matter of who is throwing the form. The relevant part of the model being used by both is, as below, so it doesn't make sense that the uploaded file would land in a different place.
function form_fields($values = array())
{
$fields = parent::form_fields($values);
$upload_path = assets_server_path('images/plants', 'images');
$fields['photo_image'] = array('type' => 'file', 'upload_path' => $upload_path, 'overwrite' => TRUE);
$fields['published']['order'] = 1000;
return $fields;
}
Thanks
No issue with the admin version having both upload and find something already uploaded
the issue is more like --
if I use forum builder directly, the resulting form sets the upload path to one thing
if I use if from the admin, it tries to upload to a different place
It also looks like the upload does not go to where the documentation says it will go if form-builder is used directly
$upload_path = assets_server_path('images/plants', 'images');
if I understand correctly, then for the above, the image ought to land in
'assets/images/images/plants' regardless of who throws the form
It doesn't -- If I use form_builder -- it lands in
'assets/images/plants'
If admin does it, it lands in
'assets/images'
Neither appears to be correct
The actual issue is the following which I inserted into the form_fields function of the news model:
In the news model:
$upload_path = assets_server_path('news', 'images');
$fields['news_item_image'] = array('type' => 'file', 'upload_path' => $upload_path, 'overwrite' => FALSE);
$upload_path = assets_server_path('news', 'pdf');
$fields['news_item_pdf'] = array('type' => 'file', 'accept' => 'pdf, doc, xls, docx, xlsx, txt','upload_path' => $upload_path, 'overwrite' => FALSE);
In the display source of the resulting form generated by admin.
<tr> <td class="label"><label for="news_item_image" id="label_news_item_image">News item image</label></td> <td class="value"><input type="file" name="news_item_image" id="news_item_image" value="" accept="gif,jpg,jpeg,png" /><input type="hidden" name="news_item_image_overwrite" id="news_item_image_overwrite" value="1" /><input type="hidden" name="news_item_image_path" id="news_item_image_path" value="/home4/fnpsdevo/public_html/assets/images/news" /></td> </tr> <tr> <td class="label"><label for="news_item_image_upload" id="label_news_item_image_upload">... OR upload an image</label></td> <td class="value"><input type="file" name="news_item_image_upload" id="news_item_image_upload" value="" accept="gif,jpg,jpeg,png" /><input type="hidden" name="news_item_image_upload_overwrite" id="news_item_image_upload_overwrite" value="1" /><input type="hidden" name="news_item_image_upload_path" id="news_item_image_upload_path" value="/home4/fnpsdevo/public_html/assets/images/" /></td> </tr> <tr> <td class="label"><label for="news_item_pdf" id="label_news_item_pdf">News item pdf</label></td> <td class="value"><input type="file" name="news_item_pdf" id="news_item_pdf" value="" accept="pdf, doc, xls, docx, xlsx, txt" /><input type="hidden" name="news_item_pdf_overwrite" id="news_item_pdf_overwrite" value="1" /><input type="hidden" name="news_item_pdf_path" id="news_item_pdf_path" value="/home4/fnpsdevo/public_html/assets/news" /></td> </tr>
Note: the path for choosing an image from the server is fine. The path for uploading a pdf is fine. The path for uploading an image is missing the "news"
Any clue why?
Thanks for your patience
I had to do the same with the pdf upload in order to not have the image upload "split" the two fields with the pdf upload (explicit ordering might have also done it).
Note that I'm still having to use
$upload_path = assets_server_path('pdf/news', 'pdf');
in a manner contrary to the documentation to get the correct path. The images version,
$upload_path = assets_server_path('news', 'images');
works as expected
So this works:
$upload_path = assets_server_path('pdf/news', 'pdf');
$fields['news_item_pdf'] = array('type' => 'file', 'accept' => 'pdf, doc, xls, docx, xlsx, txt', 'overwrite' => FALSE);
$fields['news_item_pdf_upload_path'] = array('type'=>'hidden', 'value' => $upload_path );
$upload_path = assets_server_path('news', 'images');
$fields['news_item_image'] = array('overwrite' => FALSE);
$fields['news_item_image_upload_path'] = array('type'=>'hidden','value' => $upload_path);
and produces
<tr> <td class="label"><label for="news_item_image_upload" id="label_news_item_image_upload">... OR upload an image</label></td> <td class="value"><input type="file" name="news_item_image_upload" id="news_item_image_upload" value="" accept="gif,jpg,jpeg,png" /><input type="hidden" name="news_item_image_upload_overwrite" id="news_item_image_upload_overwrite" value="1" /><input type="hidden" name="news_item_image_upload_path" id="news_item_image_upload_path" value="/home4/fnpsdevo/public_html/assets/images/" /></td> </tr> <tr> <td class="label"><label for="news_item_pdf" id="label_news_item_pdf">News item pdf</label></td> <td class="value"><input type="file" name="news_item_pdf" id="news_item_pdf" value="" accept="pdf, doc, xls, docx, xlsx, txt" /><input type="hidden" name="news_item_pdf_overwrite" id="news_item_pdf_overwrite" value="1" /></td> </tr> <tr> <td class="label">Published</td> <td class="value"><input type="radio" name="published" id="published_yes" value="yes" checked="checked" /> <label for="published_yes" id="label_published_yes">yes</label> <input type="radio" name="published" id="published_no" value="no" /> <label for="published_no" id="label_published_no">no</label> </td> </tr> <tr> <td></td> <td class="actions"><input type="button" name="" id="" value="Cancel" class="cancel" /><input type="submit" name="Save" id="Save" value="Save" class="submit" /></td> </tr> <tr> <td colspan="2" class="required"><span class="required">*</span> required fields</td> </tr> </table> <input type="hidden" name="id" id="id" value="14" /><input type="hidden" name="news_item_pdf_upload_path" id="news_item_pdf_upload_path" value="/home4/fnpsdevo/public_html/assets/pdf/news" /><input type="hidden" name="news_item_image_upload_path" id="news_item_image_upload_path" value="/home4/fnpsdevo/public_html/assets/images/news" />
I'll be happy to see if I can improve upon this for both consistency and correctness if you can point me to the file(s) with the logic creating this code.
The first place is in the Base_module_model::form_fields() method around line 434 of the fuel/modules/fuel/models/base_module_model.php file is most. It basically looks for any field that ends in "_img" or "_image" and if it does, adds an additional 'file' type field (e.g. news_item_pdf_upload) that Form_builder will generate.
The second place is in Form_builder::create_file() around line 1210. That creates the input fields for the file upload (e.g. news_item_pdf_upload) including the hidden field for the upload path and overwrite.
Try the following:
$upload_path = assets_server_path('news', 'images'); $fields['news_item_image_upload']['upload_path'] = $upload_path; $fields['news_item_image_upload']['overwrite'] = FALSE;
$upload_path = assets_server_path('news', 'images'); $fields['news_item_image'] = array('overwrite' => TRUE, 'upload_path' => $upload_path );
To implement, in Form_builder -- set_fields method becomes
public function set_fields($fields) { #print_r($fields); $i = 1; foreach ($fields as $key => $val) { if (is_string($val)) { $fields[$key] = array('name' => $key, 'value' => $val); } if (empty($val['name'])) $fields[$key]['name'] = $key; // Find and set special fields for "string" image upload if (!empty($fields[$key]['upload_path']) AND !isset($fields[$key]['type'] ) AND substr($key, -5) == 'image' OR substr($key, -3) == 'img') { $fields[$key.'_upload']['upload_path'] = $fields[$key]['upload_path']; } if (!isset($fields[$key]['type'] ) AND substr($key, -5) == 'image' OR substr($key, -3) == 'img') { $fields[$key.'_upload_overwrite']['type'] = 'hidden'; // set default to overwrite $fields[$key.'_upload_overwrite']['value'] = TRUE; if (!empty($fields[$key]['overwrite'])) $fields[$key.'_upload_overwrite']['value'] = $fields[$key]['overwrite']; } if (empty($fields[$key]['order'])) $fields[$key]['order'] = $i; $i++; } $this->_fields = $fields; }
Be sure that Base_module_model isn't forcing a hard-coded overwrite (about line 429).
$fields[$key.'_upload'] = array('order' => $order, 'before_html' => $img, 'label' => '... OR upload an image', 'upload_path' => $upload_path, 'type' => 'file');
The previously recommended code continues to work and my impression is that it would result in fewer surprises and "how to questions"
The pdf asset path is a different issue.
$upload_path = assets_server_path('news', 'pdf');
results in (from print_r($fields) at the very beginning of set_fields function in Form Builder -- first line in the function above that is shown commented out)
[news_item_pdf_upload_path] => Array ( [type] => hidden [value] => /home4/fnpsdevo/public_html/assets/news )
actually getting to the Form_builder. The subroutine, with these exact parameters, when called in other contexts does what it should. This isn't unique to this one instance -- I've been patching this in multiple models -- if I'm understanding the flow through Form_builder, then Form_builder can't be the problem.
Based on the what I learned in doing the above, I now know that the source of the issue is that the directory is hardcoded to "images" in Base_module_model::form_fields() method.
So while at least the structure of this was fresh in my mind, I went looking this morning
... a cure, similar to the above is to alter set_field_values function in Form_builder. Note that this over-rides the Base_module_model only if an upload directory has been specified.
public function set_field_values($values) { if (!empty($this->_fields)) { foreach($this->_fields as $key => $val) { if (isset($values[$key])) { if (empty($val['type'])) { $is_checkbox = FALSE; } else { $is_checkbox = (($val['type'] == 'checkbox') OR ($val['type'] == 'boolean' AND $this->boolean_mode == 'checkbox')); } if (!$is_checkbox) $this->_fields[$key]['value'] = $values[$key]; if (!empty($val['type'])) { if ($is_checkbox) { $this->_fields[$key]['checked'] = ((isset($this->_fields[$key]['value']) AND $values[$key] == $this->_fields[$key]['value']) OR $values[$key] === TRUE OR $values[$key] === 1 OR $values[$key] === 'y' OR $values[$key] === 'yes') ? TRUE : FALSE; } } // special handling for dashboard image uploads goes here // this is setting the before_html tag on the plain old _image field if (empty($val['type']) AND substr($key, -5) == 'image' OR substr($key, -3) == 'img') { { // get the path ... hopefully the upload path is set at this point $fields[$key]['upload_path'] if (!empty($this->_fields[$key]['upload_path']) ) { // the path available is the server upload path -- need to get it to be the image path $img_path = assets_server_to_web_path($this->_fields[$key]['upload_path']); // this is over-riding any default that might have been set with default in base_module_model.php $img = '<div class="img_display"><img src="'.$img_path . '/'.$this->_fields[$key]['value'] .'" style="float: right;"/></div>'; $this->_fields[$key] ['before_html'] = $img; } } } } } } }
I consider all of these to be kludges -- I'm hoping your next release will have a better way.
Still don't know where to even start on the pdf upload path issue.
Cheers