Passing function parameter to vardef

Hello,
i’ve successfully overwrite a dropdown definition in order to populate its options with data retrieve from database, here is the code:

custom/Extension/modules/Cases/Ext/Vardefs/_dropdown_db.php
$dictionary['Case']['fields']['dropdown_db_c'] = array (
	'name' => 'dropdown_db_c',
	'vname' => 'LBL_DROPDOWN_DB',
	'function' => array(
		'name' => 'getDropdown',
	),
	'type' => 'enum',
	'len' => '100',
	'comment' => 'Test created by Me',
);

Then the called php function is:
custom/Extension/application/Ext/Utils/getDropdown.php

function getDropdown(){
    static $getDropdown = null;
	if(!$getDropdown){
        global $db;
		$query = "SELECT DISTINCT name, value FROM cdz_dropdown where deleted = 0 order by name asc";
		
		$result = $db->query($query, false);
 
		$getDropdown = array();
		$getDropdown[''] = '';
 
		while (($row = $db->fetchByAssoc($result)) != null) {
			$getDropdown[$row['name']] = $row['value'];
        }
        
 
	}
	return $getDropdown;
}

Which is working like a charm.
Now i would pass a parameters in order to be more elastic like this:
custom/Extension/modules/Cases/Ext/Vardefs/_dropdown_db.php

 $dictionary['Case']['fields']['dropdown_db_c'] = array (
	'name' => 'dropdown_db_c',
	'vname' => 'LBL_DROPDOWN_DB',
	'function' => array(
		'name' => 'getDropdown',
		'params' => array (
			0 => 'Context1'
		),
	),
	'type' => 'enum',
	'len' => '100',
	'comment' => 'Test created by Me',
);

and then the function:
custom/Extension/application/Ext/Utils/getDropdown.php

function getDropdown($dropdown_context){
    static $getDropdown = null;
	if(!$getDropdown){
        global $db;
		$query = "SELECT DISTINCT name, value FROM cdz_dropdown where deleted = 0 AND context = '" . $dropdown_context . "'order by name asc";
		
		$result = $db->query($query, false);
 
		$getDropdown = array();
		$getDropdown[''] = '';
 
		while (($row = $db->fetchByAssoc($result)) != null) {
			$getDropdown[$row['name']] = $row['value'];
        }
	}
	return $getDropdown;
}

But this doesn’t work because the first time it pass the param string(when you are in ListView) then the second time the entire Object(In Detail or EditView)
The error is:
PHP Recoverable fatal error: Object of class aCase could not be converted to string in /var/www/html/SuiteCRM-7.10.29/custom/application/Ext/Utils/custom_utils.ext.php

Does anyone already tried this way, or can suggest a best way to do it.
I think it could be the rigth way because working with display() function in view seems to be less solid.

Many Thanks for every reply.
Regards

Just a side-note - you can edit your post and surround the code blocks with this, to get syntax highlighting:

```php
(your code)
```

… and about your actual question, I don’t know the answer but a quick search suggests you need to use the function_params array.

Example here:

Thanks for the hint, however trying adding this:

$dictionary['Case']['fields']['dropdown_db_c'] = array (
	'name' => 'dropdown_db_c',
	'vname' => 'LBL_DROPDOWN_DB',
	'function_name' => 'getDropdown',
	'function_params' => array('Context1'),
	'source' => 'function',
	'type' => 'enum',
	'len' => '100',
	'comment' => 'Test created by Me',
);

The function doesn’t get called anymore…
What i can try also?

Thanks

Check more examples by searching for function_params in the entire codebase.

Also pay attention to where it’s used:

You need to fill function_params_source with one of those two values, and make sure it references a member of the class: $this->$param

Hi,
i check you suggestion and i’ve tried the following:

$dictionary['Case']['fields']['dropdown_db_c'] = array (
	'name' => 'dropdown_db_c',
	'vname' => 'LBL_DROPDOWN_DB',
	'source' => 'function',
	'function_require' => 'custom/include/my_utils.php',
	'function_name' => 'getDropdown',
	'function_class' => 'MyClass',
	'function_params_source' => 'this',
	'type' => 'function',
);

And in this way the function get fired however if the declared type become
‘type’ => ‘function’
instead of
‘type’ => ‘enum’,
the dropdown will not get rendered ad expected, see attached.
If i change to ‘enum’ the function doesn’t get called anymore…

Any suggestion?
Many Thanks

I don’t know if you’ve noticed, but I am making this up as I go along :smiley:

I never did this, I never heard of this. I am merely looking at the code and getting ideas from what is there.

My next idea is this: use 'returns' => 'html' in the function vardef and see if that changes anything.

If that doesn’t work, leave that change in there but make your function return the full dropdown HTML (instead of an array)…

After that, I am afraid I have run out of ideas. Your only option is to dive deep with a debugger, follow the code where it takes you…

Good luck, and tell us how it went

Sure,
I have noticed of it. Anyway thanks for the hint.
I will try your suggestion and i’ll let you know.
My goal is to override a drop down taking option from db and passing to it a parameter.
In this way i can create a custom simple module with a key-value data that will be the dropdown option plus an additional field called scope. The scope field will be passed as parameter in order to filter results in sql query.
In this way i can create multiple dropdowns and put them in every module i want simply passing a different scope as parameter.
So the user can create in a self service manner its own options.

The vardefs is not the only way… if you can suggest me a different better approach you are welcome.

Thanks

Seems that a step forward has been made.
Changing vardefs with the following addition:

$dictionary['Case']['fields']['dropdown_db_c'] = array(
    'function' => array(
        'name' => 'getDropdown_parent',
        'params' => array(
            'param1' => 'Context1',
            'param2' => 'Case',
        ),
        'include'=> 'custom/include/caderize/php/test.php',
    ),
);

I’m able now to pass 2 parameters, making var_dump($parameters) to the $parameters object in backend funcion i can see my params like this:

[“dropdown_db_c”]=> array(26) { [“function”]=> array(3) { [“name”]=> string(18) “getDropdown_parent” [“params”]=> array(2) { [“param1”]=> string(8) “Context1” [“param2”]=> string(4) “Case” } [“include”]=> string(36) “custom/include/caderize/php/test.php” }

However i am still not able to fetch it in backend, i’ve tried:

echo $parameters->params;
echo $parameters->params[0];
echo $parameters->params['param1'];

Still returning NULL or error.

Probably i’m loosing the focus, i don’t know and it should be simpler than this.

What i’m missing?

Many Thanks

I don’'t think you’re going in the right direction…

See the code I pointed to above? That is the code that will interpret what you put in the vardefs. I don’t see it picking up on an array called function with a params sub-array inside. I see it using those other function_xyz arguments that you were trying before.

EDIT: maybe this helps… it’s the only example I found online, but unfortunately it is incomplete…

@rainolf
Look at the post. LogicHook to calculate field . The function in field has several parameters by default. The function can return html.

You can modify function ‘display’ of custom/modules/<module_name>/views/view.edit.php for change list of ‘enum’ type field as an alternate variant.

By the looks of the code, this is for 'source' => 'function' and not for simple cases like

'function' = array(
    'name' => 'getEmailAddressWidget',
    'returns' => 'html')

The code which is actually works with 'function' array in the vardefs are here:

Though parameters of called function is strictly hardcoded.

call_user_func($function, $this->focus, $name, $value, $this->view)