I just came up with a similar plugin.. You don't even have to duplicate the editarticle.tpl.. You call the plugin, then call the original template. This way, when you upgrade, and the edit article template changes, you don't have to re-copy. The filepicker options I used allows the user to either choose an existing file, or upload one, create a new folder, etc.
As for the square braces, the only thing that needs them is the <input name="m1_customfield[2]"> The rest I remove in the plugin.
Code: Select all
# replace [number] with _number
$field->field = preg_replace('/\[(\d+)\]/', '_$1', $field->field);
# replace [ ascii code %5B with _
$field->field = preg_replace('/%5B/', '_', $field->field);
# replace ] ascii code %5D with nothing
$field->field = preg_replace('/%5D/', '', $field->field);
# then put the name="m1_customfield[2]" back
$field->field = preg_replace('/name="([^"]+)_(\d+)"/', 'name="$1[$2]"', $field->field);
Screenshot:
http://teamwishbone.com/uploads/images/ ... picker.jpg
Works like a charm! Here's the entire procedure:
module_custom/News/templates/editarticle.tpl:
Code: Select all
{add_GBFilePicker_to_news fields=$custom_fields prompt='Image' template_var='edit_template'}
{include file=$edit_template}
The above looks for a custom field (text entry) called Image, and replaces it with the GBFilePicker output.
UDT 'add_GBFilePicker_to_news' :
Code: Select all
global $gCms;
foreach ($params['fields'] as &$field) {
if ($field->prompt == $params['prompt']) {
preg_match_all('/\s+([^=]+)="([^"]*)"/', $field->field, $matches);
if ($matches) {
foreach ($matches[1] as $key => $value) {
$field_params[$value] = $matches[2][$key];
}
if ($field_params) {
$GBFilePicker = $gCms->modules['GBFilePicker']['object'];
$field->field = $GBFilePicker->CreateFilePickerInput(
$GBFilePicker,
'',
$field_params['name'],
$field_params['value'],
array(
'media_type'=>'image',
'mode'=>'browser'
)
);
}
}
}
}
$field->field = preg_replace('/\[(\d+)\]/', '_$1', $field->field);
$field->field = preg_replace('/%5B/', '_', $field->field);
$field->field = preg_replace('/%5D/', '', $field->field);
$field->field = preg_replace('/name="([^"]+)_(\d+)"/', 'name="$1[$2]"', $field->field);
$config = $gCms->getConfig();
$smarty->assign($params['template_var'], $config['root_path'] . '/modules/News/templates/editarticle.tpl');
This is just a rough draft. No support for multiple fields, different sets of options, etc. I'll write up a better one later.. I need some sleep.. Been working on this one all day.
I think that plugin you posted is cleaner.. I think a combination of that plugin, with my calling the original template, plus an extra call to fix the square braces, and we have a fantastic idea!
I also think that we should be able to define different GBFP parameters for different fields.. One for files, another one for images, etc.