Creating a File Upload Field for a Core Module/Custom Module in Sugar 11.x

22323
0
Share:

As we all know out of the box Sugar has the facility to add a file upload field only in few of the core modules and custom modules of module template type ‘file’. This article will explain you how we can add a file upload field to any of the core modules or any custom module which is not a file type module template.

Our first step is to create a new file type field for our module. Follow the below steps to achieve that.

1) create a new file in the following path

custom/Extension/modules/{YOUR_MODULE}/Ext/Vardefs/sugarfield_{your_field_name}.php

and add the below code.

<?php

$GLOBALS['dictionary']['{YOUR_MODULE_KEY}']['fields']['filename'] = array (

     'name' => 'filename',

     'vname' => 'LBL_FILENAME',

     'type' => 'file',

     'dbType' => 'varchar',

     'len' => '255',

     'reportable' => true,

     'comment' => 'File name associated with the note (attachment)',

     'importable' => false,

);

$GLOBALS['dictionary']['{YOUR_MODULE_KEY}']['fields']['file_mime_type'] = array(

     'name' => 'file_mime_type',

     'vname' => 'LBL_FILE_MIME_TYPE',

     'type' => 'varchar',

     'len' => '100',

     'comment' => 'Attachment MIME type',

     'importable' => false,

);

$GLOBALS['dictionary']['{YOUR_MODULE_KEY}']['fields']['file_url'] = array (

     'name' => 'file_url',

     'vname' => 'LBL_FILE_URL',

     'type' => 'varchar',

     'source' => 'non-db',

     'reportable' => false,

     'comment' => 'Path to file (can be URL)',

     'importable' => false,

);

 

Note:To find {YOUR_MODULE_KEY} navigate to 

cache/modules/{YOUR_MODULE}/{YOUR_MODULE}vardefs.php and look for the key being used for $dictionary array.

2) Define the display label for your new field. For that create a new file in the following path.

custom/Extension/modules/{YOUR_MODULE}/Ext/Language/{your_field_name}.en_us.lang.php

and add the below code.

<?php

$mod_strings['LBL_FILE_MIME_TYPE'] = 'File Mime Type';

$mod_strings['LBL_FILE_URL'] = 'File URL';

$mod_strings['LBL_FILENAME'] = '{YOUR DISPLAY LABEL}';

 

3) Navigate to Admin > Repair > Quick Repair and Rebuild. Once you run Q&R system will ask for the confirmation to execute the SQL query to sync vardef with database. Click on execute button.

Now you have created a new file type field for your module. Navigate to Admin > Studio > {YOUR_MODULE} > Layouts > Record View.

Add your new field into the record view layout by drag and drop and click the Save and Deploy button.

 

But we are not done yet!!

Try to upload a new file from your record view and Save. It worked fine right……?  Now try deleting the file you have uploaded. System will throw an error. To delete the file Sugar is calling the removeFile endpoint defined in clients/base/api/FileApi.php,

This method has a condition like below

if (method_exists($bean, 'deleteAttachment')) {

         if (!$bean->deleteAttachment()) {

               // @TODO Localize this exception message

                throw new SugarApiExceptionRequestMethodFailure('Removal of attachment failed.');

         }

} else {

          // @TODO Localize this exception message

          throw new SugarApiExceptionNoMethod('No method found to remove attachment.');

 }

 

So our next step is to add the ‘deleteAttachment()’ method to your module bean.

As we can’t  modify the core bean file. We are going to extend the bean class of our module.

1) Create a new file in the following path (If you are modifying a core module, if you are modifying a custom module you will find the file already existing )

custom/Extension/application/Ext/Include/{YOUR_MODULE}.php

and add the below code (if you are modifying a custom module, modify the existing value)

<?php

$beanList['{YOUR_MODULE_NAME}'] = 'Custom{YOUR_MODULE_KEY}'
$beanFiles['Custom{YOUR_MODULE_KEY}'] = 'custom/modules/{YOUR_MODULE}/Custom{YOUR_MODULE_KEY}.php';

Note: For core modules you can find the {YOUR_MODULE_KEY} in Include/modules.php file

If your Module is Accounts the above two lines will look like below

<?php

$beanList['Accounts'] = 'CustomAccount';
$beanFiles['CustomAccount'] = 'custom/modules/Accounts/CustomAccount.php';

2) Now you can extend your module’s core class and add the below code in custom/modules/{YOUR_MODULE}/Custom{YOUR_MODULE_KEY}.php

<?php
/*
*The Above code is extending the Core Account bean class add adding deleteAttachment the function.
*Replace Accounts with your module name
*/

require_once 'modules/Accounts/Account.php';

class CustomAccount extends Account
{
    function deleteAttachment($isduplicate = "false")
    {
        if (!$this->ACLAccess('edit')) {
            return false;
        }


        if ($isduplicate == "true") {
            return true;
        }


        // Only attempt to delete the file if there isn't an upload_id. When there is an upload_id, we just clear the
        // file metadata from the record. When there isn't an upload_id, we attempt to delete the file and clear the
        // file metadata.
        if (empty($this->upload_id) && !UploadFile::unlink_file($this->id)) {
            return false;
        }


        $this->filename = '';
        $this->file_ext = '';
        $this->file_mime_type = '';
        $this->file = '';
        $this->file_size = '';
        $this->file_source = '';
        $this->upload_id = '';
        $this->save();
        return true;
    }
}

3) Navigate to Admin > Repair > Quick Repair and Rebuild.

We are done!

This customization is purely upgrade safe and On-demand compatible.

 

Share:

Leave a reply