How to make all new fields created by studio auditable by default?

All new fields created are required to be auditable. The administrator creates the fields by studio. How could I make all new fields auditable by default? Even if the administrator forgets to make the field auditable

Greetings and thanks in advance for any ideas.

I don’t know the answer, but I found code that might be the place to change that in

public/legacy/modules/ModuleBuilder/MB/MBModule.php

I wonder if that is used when you’re working from Studio, or only when working from Module Builder?

Another place to look is the module templates.

public/legacy/include/SugarObjects/templates/basic/vardefs.php

although that seems to set the ‘audited’ property for existing fields, I am not sure where it’s getting the value for new fields. I guess you’ll have to try stepping through the code as it creates fields

Hello, thanks for answering. I tested MBModule.php by putting a breakpoint in the createClasses method. In order to verify this method is invoked when creating a field in studio, I have created a test field but the method is not invoked. Apparently, as its name indicates, it is used in the context of module builder

Regarding public/legacy/include/SugarObjects/templates/basic/vardefs.php I don’t understand how I could use it for this purpose

About

In which file can I intercept the process of creating a field?

I was hoping that createClasses might be the place.

But there is a generic way to find out where to start debugging, which is to follow the request.

Can you please tell what is the URL of the web request that is triggered when you click to create the field?

I think I achieved the expected behavior. In public/legacy/modules/ModuleBuilder/controller.php in the action_SaveField method I can override the value of the audited field in $field. Something like that:

 require_once('modules/DynamicFields/FieldCases.php') ;
 $field = get_widget($_REQUEST [ 'type' ]) ;
 $field->audited = true; // this line
 $_REQUEST [ 'name' ] = trim($_REQUEST [ 'name' ]) ;

This effectively makes the default field auditable. I don’t know if it works with all types of fields, I imagine this would not survive an upgrade of the app

Nice.

Controllers are easy to extend in an upgrade-safe way, I think it’s in the Docs, extension framework

Perfect. I think this would be the solution. Without testing it at the time of writing

Create public/legacy/custom/modules/ModuleBuilder/controller.php

Override action_SaveField method

<?php

if (!defined('sugarEntry') || !sugarEntry) {
    die('Not A Valid Entry Point');
}

require_once('modules/ModuleBuilder/controller.php');

class CustomModuleBuilderController extends ModuleBuilderController
{
    public function action_SaveField()
    {
        require_once('modules/DynamicFields/FieldCases.php');
        $field = get_widget($_REQUEST['type']);
        $field->audited = true; // <--
        $_REQUEST['name'] = trim($_REQUEST['name']);

        parent::action_SaveField();
    }
}

2 Likes