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

6234
7
Share:
person-apple-laptop-notebook

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 change the beanFiles variable for 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

 $beanFiles[‘{YOUR_MODULE_KEY}’] = ‘custom/modules/{YOUR_MODULE}/{YOUR_MODULE}.php’;

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

 

2) Now you can copy your module’s core bean file from modules/{YOUR_MODLE}/{YOUR_MODULE}.php to

custom/modules/{YOUR_MODULE}/{YOUR_MODULE}.php

 

add the below line at the beginning of the file

require_once(‘include/upload_file.php’);

and add the below function definition at the end of the bean class.

function deleteAttachment($isduplicate=”false”){

       if($this->ACLAccess(‘edit’)){

             if($isduplicate==”true”){

                     return true;

              }

               $removeFile = “upload://{$this->id}”;

        }

        if(SugarAutoloader::fileExists($removeFile)) {

                  if(!UploadFile::unlink($removeFile)) {

                            $GLOBALS[‘log’]->error(“*** Could not unlink() file: [ {$removeFile} ]”);

                   }else{

                               $this->filename = ”;

                               $this->file_mime_type = ”;

                               $this->file = ”;

                               $this->save();

                               return true;

                     }

          }else{

                        $this->filename = ”;

                        $this->file_mime_type = ”;

                        $this->file = ”;

                        $this->save();

                        return true;

            }

            return false;

   }

 

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

We are done!

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

 

Share:

7 comments

  1. nagesh 9 December, 2015 at 13:34 Reply

    hi i have done same as you explained in the blog for upload field for suite crm but when i am going to run Quick Repair and Rebuild it is not not asking any
    “system will ask for the confirmation to execute the SQL query to sync vardef with database. Click on execute button” and i am getting blank page of suitecrm can you help me to solve my problem.

    • Shijin Krishna 10 December, 2015 at 16:17 Reply

      Hi Nagesh,

      Are you trying this for any of the core modules? Could you please check the module key which you are using in step one (Creating the file upload field) ?

      Thanks!

  2. Peter Muchora 29 December, 2015 at 19:05 Reply

    Hi,
    It is also not working for me.

    I am trying to add to the Account module. When I try to rebuild it halt at error (I am closing the php tag though not closed in your example)

    Rebuilding Relationships
    Parse error: syntax error, unexpected ‘Mime’ (T_STRING) in ………………………custom/modules/Accounts/Ext/Language/en_us.lang.ext.php on line 15

    What I have
    ‘filename’,

    ‘vname’ => ‘LBL_FILENAME’,

    ‘type’ => ‘file’,

    ‘dbType’ => ‘varchar’,

    ‘len’ => ‘255’,

    ‘reportable’ => true,

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

    ‘importable’ => false,

    );

    $GLOBALS[‘dictionary’][‘Account’][‘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’][‘Account’][‘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,

    );
    ?>
    ………………………………………………………………………………………………………………………………………………………………………………………………

    • Mohammed Jhosawa 31 December, 2015 at 13:40 Reply

      Hi Peter Muchora,

      You may have used `(grave accent in keyboard) instead of ‘(apostrophe) in your code. If you have copy/paste code from website, please replace it with ‘(apostrophe) in sugarfield_{your_field_name}.php, {your_field_name}.en_us.lang.php, custom/modules/{YOUR_MODULE}/{YOUR_MODULE}.php and deleteAttachment function.

      Thanks.

    • Shijin Krishna 27 January, 2016 at 09:04 Reply

      Hi Vignesh,

      You need to add the delete attachment to your module’s bean file. In the example we are adding the function definition to Cases bean file.

      Thanks!

Leave a reply