Trying to understand custom fields in current version

edited January 2018 in News & Announcements
The documentation says
1. define the field -- I understand where to do that

2. Create the code for the field -- now I'm not understanding what goes where

function my_custom_field($params)
{
... code here
}

$this->form_builder->create_custom('my_custom_field', $params);

Does this go in my model? what I need will be very similar to the code generated for the current image field where the image gets uploaded and the name goes into the database. I'm trying to get the javascript to be for ckfinder, not the default image loader.

Thanks

Comments

  • Here is my ./application/libraries/MY_customer_fields.php

    image_selector() fires up ckfinder and lets you select a file which is returned to populate the field. It will probably need work to get it work for your setup.

    <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); class MY_custom_fields { function image_selector($params) { $form_builder =& $params['instance']; if (!empty($params['value'])) { $params['value'] = trim($params['value'], '#'); } $str = ''; $preview = ''; $asset_folder = ''; if ((!isset($params['display_preview']) OR $params['display_preview'] === TRUE)) { if (empty($params['img_container_styles'])) { $params['img_container_styles'] = 'overflow: auto; height: 200px; width: 400px; margin-top: 5px;'; } if (!isset($params['img_styles'])) { $params['img_styles'] = 'float: left; max-width: 200px;'; } if (isset($params['folder']) OR isset($params['upload_path'])) { if (isset($params['folder'])) { $asset_folder = trim($params['folder'], '/').'/'; $asset_path = $asset_folder.$params['value']; $asset_path = assets_path($asset_path); } else { $asset_folder = assets_server_to_web_path($params['upload_path']).'/'; $asset_path = $asset_folder.$params['value']; } if (!empty($params['replace_values'])) { foreach($params['replace_values'] as $key => $val) { if (is_string($val)) { $asset_path = str_replace('{'.$key.'}', $val, $asset_path); $asset_folder = str_replace('{'.$key.'}', $val, $asset_folder); } } } } $preview = ''; if (!empty($asset_path) AND !empty($params['value'])) { $preview .= ' '; $preview .= '<a href="'.$asset_path.'" target="_blank" class="noclone">'; if (isset($params['is_image']) OR (!isset($params['is_image']) AND is_image_file($asset_path))) { $preview .= '<br><img src="'.$asset_path.'" style="'.$params['img_styles'].'" class="img_bg">'; } else { $preview .= $asset_path; } $preview .= '</a>'; } } $params['after_html'] = '<a href="#" onclick="'.$params['key'].'_Clear();" class="btn_field custom_ico_field"><img src="'.assets_path('_admin/ico_cancel.png').'"/></a><div id="'.$params['key'].'_preview">'.$preview.'</div>'; setcookie('ckf_assets_isauthorized',true,time()+(7200),'/'); //If we get this far then we're in Fuel and are authorised to access Assets $js = '<script src="'.js_path('ckfinder','ckf_assets').'"></script> <script type="text/javascript"> function '.$params['key'].'_browseserver() { var finder = new CKFinder(); finder.basePath = \'./\'; finder.selectActionFunction = '.$params['key'].'_SetFileField; finder.removePlugins = \'basket\'; finder.popup(); } function '.$params['key'].'_SetFileField( fileUrl ) { var f = unescape(encodeURIComponent(fileUrl.replace(jqx.config.assetsImgPath,\'\'))); document.getElementById(\''.$params['key'].'\').value = f; $(\'#'.$params['key'].'_preview\').html(\'<br><img src="\'+fileUrl+\'" style="'.$params['img_styles'].'" class="img_bg">\'); return true; //close finder } function '.$params['key'].'_Clear() { $(\'#'.$params['key'].'\').val(\'\'); $(\'#'.$params['key'].'_preview\').html(\'\'); return false; } </script>'; $form_builder->add_js($js); $params['class'] = (!empty($params['class'])) ? $params['class'].' field_type_image_selector' : 'field_type_image_selector'; $params['attributes'] = 'onclick="'.$params['key'].'_browseserver();"'; $params['type'] = 'text'; return '<div style="border:1px dashed #ccc;border-radius:3px;padding:10px;">'.$form_builder->create_text($params).$params['after_html'].'<div style="clear:both;"></div></div>'; //return $str; } }
Sign In or Register to comment.